RouteResultArguments.php 3.09 KB
Newer Older
Oliver Bartsch's avatar
Oliver Bartsch committed
1
2
<?php

Oliver Bartsch's avatar
Oliver Bartsch committed
3
declare(strict_types = 1);
Oliver Bartsch's avatar
Oliver Bartsch committed
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

namespace T3o\Ter\Rest;

/*
 * This file is part of TYPO3 CMS-extension "ter", created by Oliver Bartsch.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 */

use T3o\Ter\Rest\RouteArgument\RouteArgumentInterface;
use TYPO3\CMS\Core\Routing\RouteResultInterface;

/**
 * Provide the full route information resolved by the RouteResolver
 * and extended using the RouteConfiguration.
 *
 * @see RouteResolver
 * @see RouteConfiguration
 */
final class RouteResultArguments implements RouteResultInterface
{
    protected string $operationId;
    protected string $requestHandler;
    protected array $middlewares;
    protected array $routeArguments;

    public function __construct(
        string $operationId,
        string $requestHandler,
        array $middlewares,
        array $routeArguments = []
    ) {
        $this->operationId = $operationId;
        $this->requestHandler = $requestHandler;
        $this->middlewares = $middlewares;
        $this->routeArguments = $routeArguments;
    }

    public function getOperationId(): string
    {
        return $this->operationId;
    }

    public function getRequestHandler(): string
    {
        return $this->requestHandler;
    }

    public function getMiddlewares(): array
    {
        return $this->middlewares;
    }

    public function getRouteArguments(): array
    {
        return $this->routeArguments;
    }

    public function addRouteArgument(RouteArgumentInterface $routeArgument): self
    {
        $name = $routeArgument->getName();

        if (!($this->routeArguments[$name] ?? false) instanceof RouteArgumentInterface) {
            $this->routeArguments[$name] = $routeArgument;
        }

        return $this;
    }

    public function hasRouteArgument(string $name): bool
    {
77
        return isset($this->routeArguments[$name]);
Oliver Bartsch's avatar
Oliver Bartsch committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    }

    public function getRouteArgument(string $name): RouteArgumentInterface
    {
        if (!($this->routeArguments[$name] ?? false) instanceof RouteArgumentInterface) {
            throw new \InvalidArgumentException('No router argument found for name: ' . $name, 1600994238);
        }

        return $this->routeArguments[$name];
    }

    public function offsetExists($offset): bool
    {
        return $offset === 'operationId'
            || $offset === 'requestHandler'
            || $this->hasRouteArgument($offset);
    }

    public function offsetGet($offset)
    {
        if ($offset === 'operationId') {
            return $this->getOperationId();
        }
        if ($offset === 'requestHandler') {
            return $this->getRequestHandler();
        }
        return $this->getRouteArgument($offset);
    }

    public function offsetSet($offset, $value): void
    {
        throw new \InvalidArgumentException('RouteResultArguments cannot be modified.', 1600970434);
    }

    public function offsetUnset($offset): void
    {
        throw new \InvalidArgumentException('RouteResultArguments cannot be modified.', 1600970434);
    }
}