1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of php-simple-request. |
4
|
|
|
* |
5
|
|
|
* php-simple-request is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* php-simple-request is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with php-simple-request. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
namespace Mcustiel\SimpleRequest\Annotation\Validator; |
19
|
|
|
|
20
|
|
|
use Mcustiel\SimpleRequest\Annotation\ValidatorAnnotation; |
21
|
|
|
use Mcustiel\SimpleRequest\Validator\Properties as PropertiesValidator; |
22
|
|
|
use Mcustiel\SimpleRequest\Exception\InvalidAnnotationException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @Annotation |
26
|
|
|
* @Target({ "PROPERTY", "ANNOTATION" }) |
27
|
|
|
* |
28
|
|
|
* @author mcustiel |
29
|
|
|
*/ |
30
|
|
|
class Properties extends ValidatorAnnotation |
31
|
|
|
{ |
32
|
|
|
public $properties; |
33
|
|
|
public $additionalProperties; |
34
|
|
|
|
35
|
74 |
|
public function __construct() |
36
|
|
|
{ |
37
|
74 |
|
parent::__construct(PropertiesValidator::class); |
38
|
74 |
|
} |
39
|
|
|
|
40
|
74 |
|
public function getValue() |
41
|
|
|
{ |
42
|
74 |
|
$count = count($this->properties); |
43
|
|
|
$this->validatePropertiesCountOrThrowException($count); |
44
|
74 |
|
|
45
|
|
|
return [ |
46
|
|
|
'properties' => $this->getPropertiesConfig($count), |
47
|
|
|
'additionalProperties' => $this->additionalProperties, |
48
|
|
|
]; |
49
|
|
|
} |
50
|
74 |
|
|
51
|
74 |
|
private function getPropertiesConfig($count) |
52
|
74 |
|
{ |
53
|
74 |
|
$properties = array(); |
54
|
|
|
for ($i = 0; $i < $count; $i += 2) { |
55
|
74 |
|
$properties[$this->properties[$i]] = $this->properties[$i + 1]; |
56
|
|
|
} |
57
|
|
|
return $properties; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
private function validatePropertiesCountOrThrowException($count) |
62
|
|
|
{ |
63
|
|
|
if ($count % 2 != 0) { |
64
|
|
|
throw new InvalidAnnotationException( |
65
|
|
|
'Properties field must specify a set of (name, validator) pairs' |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|