1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Collections\Traits; |
4
|
|
|
|
5
|
|
|
use Collections\ArrayList; |
6
|
|
|
use Collections\Dictionary; |
7
|
|
|
use Collections\Iterable; |
8
|
|
|
use Collections\VectorInterface; |
9
|
|
|
|
10
|
|
|
trait CommonMutableContainerTrait |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* {@inheritdoc} |
14
|
|
|
*/ |
15
|
1 |
|
public function values() |
16
|
|
|
{ |
17
|
1 |
|
return new ArrayList($this); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return array |
22
|
|
|
*/ |
23
|
2 |
View Code Duplication |
public function toValuesArray() |
|
|
|
|
24
|
|
|
{ |
25
|
1 |
|
$arr = []; |
26
|
1 |
|
foreach ($this as $value) { |
|
|
|
|
27
|
1 |
|
if ($value instanceof Iterable) { |
28
|
|
|
$arr[] = $value->toArray(); |
29
|
|
|
} else { |
30
|
1 |
|
$arr[] = $value; |
31
|
|
|
} |
32
|
1 |
|
} |
33
|
|
|
|
34
|
2 |
|
return $arr; |
35
|
|
|
} |
36
|
|
|
|
37
|
3 |
|
public function toKeysArray() |
38
|
3 |
|
{ |
39
|
1 |
|
$res = []; |
40
|
1 |
|
foreach ($this as $k => $_) { |
|
|
|
|
41
|
1 |
|
$res[] = $k; |
42
|
1 |
|
} |
43
|
|
|
|
44
|
1 |
|
return $res; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
17 |
View Code Duplication |
public function toArray() |
|
|
|
|
51
|
|
|
{ |
52
|
17 |
|
$arr = []; |
53
|
17 |
|
foreach ($this as $key => $value) { |
|
|
|
|
54
|
16 |
|
if ($value instanceof Iterable) { |
55
|
6 |
|
$arr[$key] = $value->toArray(); |
56
|
6 |
|
} else { |
57
|
16 |
|
$arr[$key] = $value; |
58
|
1 |
|
} |
59
|
17 |
|
} |
60
|
|
|
|
61
|
17 |
|
return $arr; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
1 |
|
public static function fromArray(array $arr) |
68
|
|
|
{ |
69
|
1 |
|
$map = new static(); |
70
|
1 |
|
foreach ($arr as $k => $v) { |
71
|
1 |
|
if (is_array($v)) { |
72
|
1 |
|
$map[$k] = new static($v); |
|
|
|
|
73
|
|
|
} else { |
74
|
1 |
|
$map[$k] = $v; |
75
|
|
|
} |
76
|
1 |
|
} |
77
|
|
|
|
78
|
1 |
|
return $map; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritDoc} |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
2 |
|
public function groupBy($callback) |
86
|
|
|
{ |
87
|
2 |
|
$group = new Dictionary(); |
88
|
2 |
|
foreach ($this as $value) { |
|
|
|
|
89
|
2 |
|
$key = $callback($value); |
90
|
2 |
|
if (!$group->containsKey($key)) { |
91
|
2 |
|
$element = $this instanceof VectorInterface ? new static([$value]) : new ArrayList([$value]); |
|
|
|
|
92
|
2 |
|
$group->add($key, $element); |
93
|
2 |
|
} else { |
94
|
2 |
|
$value = $group->get($key)->add($value); |
95
|
2 |
|
$group->set($key, $value); |
96
|
|
|
} |
97
|
2 |
|
} |
98
|
|
|
|
99
|
2 |
|
return $group; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritDoc} |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
2 |
|
public function indexBy($callback) |
107
|
|
|
{ |
108
|
2 |
|
$group = new Dictionary(); |
109
|
2 |
|
foreach ($this as $value) { |
|
|
|
|
110
|
2 |
|
$key = $callback($value); |
111
|
2 |
|
$group->set($key, $value); |
112
|
2 |
|
} |
113
|
|
|
|
114
|
2 |
|
return $group; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function reduce(callable $callback, $initial = null) |
121
|
|
|
{ |
122
|
|
|
foreach ($this as $element) { |
|
|
|
|
123
|
|
|
$initial = $callback($initial, $element); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $initial; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
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.