• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

contributte / api-router / 6903388818

17 Nov 2023 11:27AM UTC coverage: 88.71% (+0.5%) from 88.235%
6903388818

push

github

f3l1x
Versions: open 6.1.x

275 of 310 relevant lines covered (88.71%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.94
/src/ApiRouteSpec.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\ApiRouter;
4

5
use Contributte\ApiRouter\Exception\ApiRouteWrongPropertyException;
6

7
abstract class ApiRouteSpec
8
{
9

10
        protected ?string $description = null;
11

12
        protected string $path = '/';
13

14
        /** @Enum({"CREATE", "READ", "UPDATE", "DELETE", "OPTIONS"}) */
15
        protected ?string $method = null;
16

17
        /** @var array<string, scalar> */
18
        protected array $parameters = [];
19

20
        /** @var array<string> */
21
        protected array $parameters_infos = ['requirement', 'type', 'description', 'default'];
22

23
        protected int $priority = 0;
24

25
        /** @Enum({"json", "xml"}) */
26
        protected string $format = 'json';
27

28
        /** @var array<mixed>|null */
29
        protected ?array $example = null;
30

31
        protected ?string $section = null;
32

33
        /** @var array<mixed> */
34
        protected array $tags = [];
35

36
        /** @var array<int> */
37
        protected array $response_codes = [];
38

39
        /** @Enum({true, false}) */
40
        protected bool $disable = false;
41

42
        /**
43
         * @param array<mixed> $data
44
         */
45
        public function __construct(array $data)
1✔
46
        {
47
                foreach ($data as $key => $value) {
1✔
48
                        $method = 'set' . str_replace('_', '', ucwords($key, '_'));
1✔
49

50
                        if (!method_exists($this, $method)) {
1✔
51
                                throw new ApiRouteWrongPropertyException(
1✔
52
                                        sprintf('Unknown property "%s" on annotation "%s"', $key, static::class)
1✔
53
                                );
54
                        }
55

56
                        $this->$method($value);
1✔
57
                }
58
        }
1✔
59

60
        public function setDescription(?string $description): void
1✔
61
        {
62
                $this->description = $description;
1✔
63
        }
1✔
64

65
        public function getDescription(): ?string
66
        {
67
                return $this->description;
×
68
        }
69

70
        public function getPath(): string
71
        {
72
                return $this->path;
1✔
73
        }
74

75
        public function getMethod(): string
76
        {
77
                return $this->method;
1✔
78
        }
79

80
        /**
81
         * @return array<mixed>
82
         */
83
        public function getParameters(): array
84
        {
85
                return $this->parameters;
×
86
        }
87

88
        public function setPriority(int $priority): void
1✔
89
        {
90
                $this->priority = $priority;
1✔
91
        }
1✔
92

93
        public function getPriority(): int
94
        {
95
                return $this->priority;
1✔
96
        }
97

98
        public function setFormat(string $format): void
1✔
99
        {
100
                $this->format = $format;
1✔
101
        }
1✔
102

103
        public function getFormat(): string
104
        {
105
                return $this->format;
1✔
106
        }
107

108
        /**
109
         * @param array<mixed>|null $example
110
         */
111
        public function setExample(?array $example): void
112
        {
113
                $this->example = $example;
×
114
        }
115

116
        /**
117
         * @return array<mixed>|null
118
         */
119
        public function getExample(): ?array
120
        {
121
                return $this->example;
×
122
        }
123

124
        public function setSection(?string $section): void
1✔
125
        {
126
                $this->section = $section;
1✔
127
        }
1✔
128

129
        public function getSection(): ?string
130
        {
131
                return $this->section;
1✔
132
        }
133

134
        /**
135
         * @param array<string> $tags
136
         */
137
        public function setTags(array $tags): void
1✔
138
        {
139
                $this->tags = $tags;
1✔
140
        }
1✔
141

142
        /**
143
         * @return array<string>
144
         */
145
        public function getTags(): array
146
        {
147
                $return = [];
1✔
148

149
                /**
150
                 * Tag may be saves aither with color: [tagName => color] or without: [tagName]
151
                 */
152
                foreach ($this->tags as $tag => $color) {
1✔
153
                        if (is_numeric($tag)) {
1✔
154
                                $return[$color] = '#9b59b6';
1✔
155
                        } else {
156
                                $return[$tag] = $color;
1✔
157
                        }
158
                }
159

160
                return $return;
1✔
161
        }
162

163
        /**
164
         * @param array<int> $response_codes
165
         */
166
        public function setResponseCodes(array $response_codes): void
167
        {
168
                $this->response_codes = $response_codes;
×
169
        }
170

171
        /**
172
         * @return array<int>
173
         */
174
        public function getResponseCodes(): array
175
        {
176
                return $this->response_codes;
×
177
        }
178

179
        public function setDisable(bool $disable): void
180
        {
181
                $this->disable = (bool) $disable;
×
182
        }
183

184
        public function getDisable(): bool
185
        {
186
                return $this->disable;
×
187
        }
188

189
        protected function setPath(string $path): void
1✔
190
        {
191
                if (!$path) {
1✔
192
                        throw new ApiRouteWrongPropertyException('ApiRoute path can not be empty');
×
193
                }
194

195
                $this->path = (string) $path;
1✔
196
        }
1✔
197

198
        protected function setMethod(string $method): void
1✔
199
        {
200
                $this->method = strtoupper($method);
1✔
201
        }
1✔
202

203
        /**
204
         * @param array<mixed> $parameters
205
         * @throws ApiRouteWrongPropertyException
206
         */
207
        protected function setParameters(array $parameters): void
1✔
208
        {
209
                foreach ($parameters as $key => $info) {
1✔
210
                        if (strpos($this->getPath(), '<' . $key . '>') === false) {
1✔
211
                                throw new ApiRouteWrongPropertyException('Parameter <' . $key . '> is not present in the url mask');
1✔
212
                        }
213

214
                        foreach ($info as $info_key => $value) {
1✔
215
                                if (!in_array($info_key, $this->parameters_infos, true)) {
1✔
216
                                        throw new ApiRouteWrongPropertyException(sprintf(
1✔
217
                                                'You cat set only these description informations: [%s] - "%s" given',
1✔
218
                                                implode(', ', $this->parameters_infos),
1✔
219
                                                $info_key
220
                                        ));
221
                                }
222

223
                                if (!is_scalar($value) && $value !== null) {
1✔
224
                                        throw new ApiRouteWrongPropertyException(
1✔
225
                                                'You cat set only scalar parameters informations (key [' . $info_key . '])'
1✔
226
                                        );
227
                                }
228
                        }
229
                }
230

231
                $this->parameters = $parameters;
1✔
232
        }
1✔
233

234
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc