1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace T4webDomain; |
4
|
|
|
|
5
|
|
|
use ArrayObject; |
6
|
|
|
|
7
|
|
|
class Collection extends ArrayObject |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param string $entityMethod |
11
|
|
|
* @return static |
12
|
|
|
*/ |
13
|
|
View Code Duplication |
public function rebuildByEntityMethod($entityMethod) |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
$newCollection = new static(); |
16
|
|
|
|
17
|
|
|
foreach ($this->getArrayCopy() as $entity) { |
18
|
|
|
$newCollection->offsetSet($entity->$entityMethod(), $entity); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
return $newCollection; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $entityMethod |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
|
|
public function getValuesByEntityMethod($entityMethod) |
29
|
|
|
{ |
30
|
|
|
$values = []; |
31
|
|
|
|
32
|
|
|
foreach ($this->getArrayCopy() as $entity) { |
33
|
|
|
$values[] = $entity->$entityMethod(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $values; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $entityMethod |
41
|
|
|
* @param mixed $value |
42
|
|
|
* @return static |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function filter($entityMethod, $value) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$newCollection = new static(); |
47
|
|
|
|
48
|
|
|
foreach ($this->getArrayCopy() as $entity) { |
49
|
|
|
if ($entity->$entityMethod() == $value) { |
50
|
|
|
$newCollection->offsetSet($entity->getId(), $entity); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $newCollection; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return Collection |
59
|
|
|
*/ |
60
|
|
|
public function rebuildById() |
61
|
|
|
{ |
62
|
|
|
return $this->rebuildByEntityMethod('getId'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function getIds() |
69
|
|
|
{ |
70
|
|
|
$ids = []; |
71
|
|
|
|
72
|
|
|
foreach ($this as $entity) { |
73
|
|
|
$ids[] = $entity->getId(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $ids; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $attribute |
81
|
|
|
* @param bool $unique |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function getValueByAttribute($attribute, $unique = true) |
85
|
|
|
{ |
86
|
|
|
$methodName = 'get' . ucfirst($attribute); |
87
|
|
|
|
88
|
|
|
if (!method_exists(reset($this), $methodName)) { |
89
|
|
|
throw new \RuntimeException("Entity " . get_class(reset($this)) . " does not have a method $methodName"); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$result = []; |
93
|
|
|
|
94
|
|
|
foreach ($this as $entity) { |
95
|
|
|
$result[] = $entity->{$methodName}(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($unique) { |
99
|
|
|
$result = array_unique($result); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $result; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return Entity |
107
|
|
|
*/ |
108
|
|
|
public function getFirst() |
109
|
|
|
{ |
110
|
|
|
foreach ($this->getArrayCopy() as $entity) { |
111
|
|
|
return $entity; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.