aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/StudyAreas/StudyAreasIndex.php
blob: 786e1609cb266c5e145d6a36ee02741b30f39bbb (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
<?php

namespace JsonApi\Routes\StudyAreas;

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

/**
 * Zeigt einen bestimmten Studienbereich an.
 */
class StudyAreasIndex extends JsonApiController
{
    protected $allowedIncludePaths = [
        'children',
        'courses',
        'institute',
        'parent',
    ];
    protected $allowedPagingParameters = ['offset', 'limit'];

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function __invoke(Request $request, Response $response, $args)
    {
        $root = \StudipStudyArea::getRootArea();
        $studyAreas =  $this->mapTree($root);

        list($offset, $limit) = $this->getOffsetAndLimit();

        return $this->getPaginatedContentResponse(
            array_slice($studyAreas, $offset, $limit),
            count($studyAreas)
        );
    }

    private function mapTree(\StudipStudyArea $node)
    {
        $level = [];
        $child_nodes = $node->getChildNodes();
        foreach ($child_nodes as $child_node) {
            $level[] = $child_node;
            $level = array_merge($level, $this->mapTree($child_node));
        }
        return $level;
    }
}