1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/old-town/old-town-propertyset |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace OldTown\PropertySet; |
7
|
|
|
|
8
|
|
|
use OldTown\PropertySet\Exception\IllegalPropertyException; |
9
|
|
|
use OldTown\PropertySet\Verifiers\Exception\VerifyException; |
10
|
|
|
use OldTown\PropertySet\Verifiers\PropertyVerifierInterface; |
11
|
|
|
use SplObjectStorage; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class PropertySchema |
16
|
|
|
* |
17
|
|
|
* @package OldTown\PropertySet |
18
|
|
|
*/ |
19
|
|
|
class PropertySchema |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var SplObjectStorage|PropertyVerifierInterface[] |
23
|
|
|
*/ |
24
|
|
|
protected $verifiers; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $name; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* @var integer |
35
|
|
|
*/ |
36
|
|
|
protected $type; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param null $name |
40
|
|
|
*/ |
41
|
|
|
public function __construct($name = null) |
42
|
|
|
{ |
43
|
|
|
$this->name = $name; |
44
|
|
|
$this->verifiers = new SplObjectStorage(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function getPropertyName() |
51
|
|
|
{ |
52
|
|
|
return $this->name; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $name |
57
|
|
|
* |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function setPropertyName($name) |
61
|
|
|
{ |
62
|
|
|
$this->name = (string)$name; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return int |
69
|
|
|
*/ |
70
|
|
|
public function getType() |
71
|
|
|
{ |
72
|
|
|
return $this->type; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param int $type |
77
|
|
|
* |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
public function setType($type) |
81
|
|
|
{ |
82
|
|
|
$this->type = (integer)$type; |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return SplObjectStorage|PropertyVerifierInterface[] |
89
|
|
|
*/ |
90
|
|
|
public function getVerifiers() |
91
|
|
|
{ |
92
|
|
|
return $this->verifiers; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param PropertyVerifierInterface $pv |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function addVerifier(PropertyVerifierInterface $pv) |
101
|
|
|
{ |
102
|
|
|
$this->verifiers->attach($pv); |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param PropertyVerifierInterface $pv |
110
|
|
|
* |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
public function removeVerifier(PropertyVerifierInterface $pv) |
114
|
|
|
{ |
115
|
|
|
$this->verifiers->detach($pv); |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param $value |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
* |
125
|
|
|
* @throws IllegalPropertyException |
126
|
|
|
*/ |
127
|
|
|
public function validate($value) |
128
|
|
|
{ |
129
|
|
|
$verifiers = $this->getVerifiers(); |
130
|
|
|
foreach ($verifiers as $pv) { |
131
|
|
|
try { |
132
|
|
|
$pv->verify($value); |
133
|
|
|
} catch (VerifyException $e) { |
134
|
|
|
throw new IllegalPropertyException($e->getMessage(), $e->getCode(), $e); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|