|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ClassGeneration package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Antonio Spinelli <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ClassGeneration; |
|
13
|
|
|
|
|
14
|
|
|
use ClassGeneration\Collection\ArrayCollection; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Property Collection ClassGeneration |
|
18
|
|
|
* @author Antonio Spinelli <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class PropertyCollection extends ArrayCollection |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Adds a new Property on the collection. |
|
25
|
|
|
* |
|
26
|
|
|
* @param PropertyInterface $property |
|
27
|
|
|
* |
|
28
|
|
|
* @return bool |
|
29
|
|
|
* @throws \InvalidArgumentException |
|
30
|
|
|
*/ |
|
31
|
11 |
|
public function add($property) |
|
32
|
|
|
{ |
|
33
|
11 |
|
if (!$property instanceof PropertyInterface) { |
|
34
|
1 |
|
throw new \InvalidArgumentException( |
|
35
|
|
|
'This Property must be a instance of \ClassGeneration\PropertyInterface' |
|
36
|
1 |
|
); |
|
37
|
|
|
} |
|
38
|
11 |
|
if ($property->getName() === null) { |
|
39
|
5 |
|
$property->setName('property' . ($this->count() + 1)); |
|
40
|
5 |
|
} |
|
41
|
|
|
|
|
42
|
11 |
|
parent::set($property->getName(), $property); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
11 |
|
return true; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Gets the current Property. |
|
49
|
|
|
* @return Property |
|
50
|
|
|
*/ |
|
51
|
5 |
|
public function current() |
|
52
|
|
|
{ |
|
53
|
5 |
|
return parent::current(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Gets the Property Iterator. |
|
58
|
|
|
* @return PropertyIterator|Property[] |
|
59
|
|
|
*/ |
|
60
|
15 |
|
public function getIterator() |
|
61
|
|
|
{ |
|
62
|
15 |
|
return new PropertyIterator($this); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Parse the Property Collection to string. |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
10 |
|
public function toString() |
|
70
|
|
|
{ |
|
71
|
10 |
|
$string = ''; |
|
72
|
10 |
|
$properties = $this->getIterator(); |
|
73
|
10 |
|
foreach ($properties as $property) { |
|
74
|
6 |
|
$string .= $property->toString(); |
|
75
|
10 |
|
} |
|
76
|
|
|
|
|
77
|
10 |
|
return $string; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Find the properties by name. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $propertyName |
|
84
|
|
|
* |
|
85
|
|
|
* @return PropertyCollection |
|
86
|
|
|
*/ |
|
87
|
2 |
|
public function getByName($propertyName) |
|
88
|
|
|
{ |
|
89
|
2 |
|
$foundList = new self(); |
|
90
|
2 |
|
$list = $this->getIterator(); |
|
91
|
2 |
|
foreach ($list as $property) { |
|
92
|
2 |
|
if ((is_array($propertyName) && in_array($property->getName(), $propertyName)) |
|
93
|
2 |
|
|| ($property->getName() === $propertyName) |
|
94
|
2 |
|
) { |
|
95
|
2 |
|
$foundList->add($property); |
|
96
|
2 |
|
} |
|
97
|
2 |
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
return $foundList; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Removes tags by name. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $propertyName |
|
106
|
|
|
* |
|
107
|
|
|
* @return PropertyCollection |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public function removeByName($propertyName) |
|
110
|
|
|
{ |
|
111
|
1 |
|
$removedList = new self(); |
|
112
|
1 |
|
$list = $this->getIterator(); |
|
113
|
1 |
|
foreach ($list as $index => $property) { |
|
114
|
1 |
|
if ((is_array($propertyName) && in_array($property->getName(), $propertyName)) |
|
115
|
1 |
|
|| ($property->getName() === $propertyName) |
|
116
|
1 |
|
) { |
|
117
|
1 |
|
$removedList->add(clone $property); |
|
118
|
1 |
|
$this->remove($index); |
|
119
|
1 |
|
} |
|
120
|
1 |
|
} |
|
121
|
|
|
|
|
122
|
1 |
|
return $removedList; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.