aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Tree/ChildrenOfTreeNode.php
blob: 313b892cac1b55513b99ce6ff83a4e591b4e138c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php

namespace JsonApi\Routes\Tree;

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use JsonApi\Errors\RecordNotFoundException;
use JsonApi\JsonApiController;

class ChildrenOfTreeNode extends JsonApiController
{
    protected $allowUnrecognizedParams = true;
    protected $allowedFilteringParameters = ['visible'];

    protected $allowedIncludePaths = [
        'children',
        'courses',
        'institute',
        'parent',
    ];

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameters)
     */
    public function __invoke(Request $request, Response $response, $args)
    {
        list($classname, $id) = explode('_', $args['id']);

        $node = $classname::getNode($id);
        if (!$node) {
            throw new RecordNotFoundException();
        }

        $filters = $this->getContextFilters();

        $data = $node->getChildNodes((bool) $filters['visible']);

        return $this->getContentResponse($data);
    }

    private function getContextFilters()
    {
        $defaults = [
            'visible' => false
        ];

        $filtering = $this->getQueryParameters()->getFilteringParameters() ?: [];

        return array_merge($defaults, $filtering);
    }
}