aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/RangeTree/ChildrenOfRangeTreeNode.php
blob: 58fcbf3601b586c7fcb46715b290a1f1050bc114 (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
<?php

namespace JsonApi\Routes\RangeTree;

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

class ChildrenOfRangeTreeNode extends JsonApiController
{
    protected $allowedIncludePaths = [
        'children',
        'courses',
        'institute',
        'parent',
    ];
    protected $allowedPagingParameters = ['offset', 'limit'];

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameters)
     */
    public function __invoke(Request $request, Response $response, $args)
    {
        if (!RangeTreeNode::getNode($args['id'])) {
            throw new RecordNotFoundException();
        }

        [$offset, $limit] = $this->getOffsetAndLimit();
        $total = \RangeTreeNode::countByParent_id($args['id']);
        $children = \RangeTreeNode::findByParent_id(
            $args['id'],
            "LIMIT {$offset}, {$limit}"
        );

        return $this->getPaginatedContentResponse($children, $total);
    }
}