aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/SemestersIndex.php
blob: 24883c19f3dea43ce8452a1f039c5b89a72577fb (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
<?php

namespace JsonApi\Routes;

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

/**
 * List all the semesters.
 */
class SemestersIndex extends JsonApiController
{
    protected $allowedPagingParameters = ['offset', 'limit'];

    protected $allowedFilteringParameters = ['current', 'timestamp'];

    public function __invoke(Request $request, Response $response, $args)
    {
        list($offset, $limit) = $this->getOffsetAndLimit();

        $filtering = $this->getQueryParameters()->getFilteringParameters();

        if (empty($filtering)) {
            $semesters = \Semester::getAll();
        } else {
            if (array_key_exists('current', $filtering)) {
                $semester = \Semester::findCurrent();
            }
            if (isset($filtering['timestamp'])) {
                $semester = \Semester::findByTimestamp($filtering['timestamp']);
            }

            if (!$semester) {
                throw new RecordNotFoundException('Could not find semester.');
            } else {
                $semesters = [$semester];
            }
        }

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