1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Cubiche\Core\Validator\Mapping; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Validator\Assert; |
14
|
|
|
use Metadata\ClassMetadata as BaseClassMetadata; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* ClassMetadata class. |
18
|
|
|
* |
19
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class ClassMetadata extends BaseClassMetadata |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
public $defaultGroup; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public function getClassName() |
32
|
|
|
{ |
33
|
|
|
return $this->name; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param $property |
38
|
|
|
* |
39
|
|
|
* @return PropertyMetadata|null |
40
|
|
|
*/ |
41
|
|
|
protected function getPropertyMetadata($property) |
42
|
|
|
{ |
43
|
|
|
return isset($this->propertyMetadata[$property]) ? $this->propertyMetadata[$property] : null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $property |
48
|
|
|
* @param Assert $constraint |
49
|
|
|
* @param string $group |
50
|
|
|
*/ |
51
|
|
|
public function addPropertyConstraint($property, Assert $constraint, $group = null) |
52
|
|
|
{ |
53
|
|
|
$propertyMetadata = $this->getPropertyMetadata($property); |
54
|
|
|
if ($propertyMetadata === null) { |
55
|
|
|
$propertyMetadata = new PropertyMetadata($this->getClassName(), $property); |
56
|
|
|
$propertyMetadata->defaultGroup = $this->defaultGroup; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$propertyMetadata->addConstraint($constraint, $group); |
60
|
|
|
|
61
|
|
|
$this->addPropertyMetadata($propertyMetadata); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $property |
66
|
|
|
* @param array $constraints |
67
|
|
|
* @param string $group |
68
|
|
|
*/ |
69
|
|
|
public function addPropertyConstraints($property, array $constraints, $group = null) |
70
|
|
|
{ |
71
|
|
|
foreach ($constraints as $constraint) { |
72
|
|
|
$this->addPropertyConstraint($property, $constraint, $group); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return PropertyMetadata[] |
78
|
|
|
*/ |
79
|
|
|
public function getPropertiesMetadata() |
80
|
|
|
{ |
81
|
|
|
return $this->propertyMetadata; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Merges the constraints of the given metadata into this object. |
86
|
|
|
* |
87
|
|
|
* @param ClassMetadata $source |
88
|
|
|
*/ |
89
|
|
|
public function mergeConstraints(ClassMetadata $source) |
90
|
|
|
{ |
91
|
|
|
foreach ($source->getPropertiesMetadata() as $property => $propertyMetadata) { |
92
|
|
|
foreach ($propertyMetadata->getConstraints() as $group => $constraints) { |
93
|
|
|
$this->addPropertyConstraints($property, $constraints, $group); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function serialize() |
102
|
|
|
{ |
103
|
|
|
return serialize(array( |
104
|
|
|
$this->name, |
105
|
|
|
$this->methodMetadata, |
106
|
|
|
$this->propertyMetadata, |
107
|
|
|
$this->fileResources, |
108
|
|
|
$this->createdAt, |
109
|
|
|
$this->defaultGroup, |
110
|
|
|
)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function unserialize($str) |
117
|
|
|
{ |
118
|
|
|
list( |
119
|
|
|
$this->name, |
120
|
|
|
$this->methodMetadata, |
121
|
|
|
$this->propertyMetadata, |
122
|
|
|
$this->fileResources, |
123
|
|
|
$this->createdAt, |
124
|
|
|
$this->defaultGroup) = unserialize($str); |
125
|
|
|
|
126
|
|
|
$this->reflection = new \ReflectionClass($this->name); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|