1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Collections\Traits; |
4
|
|
|
|
5
|
|
|
use JsonSerializable; |
6
|
|
|
use Nip\Collections\AbstractCollection; |
7
|
|
|
use Nip\Utility\Arr; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class TransformMethodsTrait |
11
|
|
|
* @package Nip\Collections\Traits |
12
|
|
|
*/ |
13
|
|
|
trait TransformMethodsTrait |
14
|
|
|
{ |
15
|
|
|
protected $serializable = ['items']; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Get the values of a given key. |
19
|
|
|
* |
20
|
|
|
* @param string|array|int|null $value |
21
|
|
|
* @param string|null $key |
22
|
|
|
* @return static |
23
|
|
|
*/ |
24
|
|
|
public function pluck($value, $key = null) |
25
|
|
|
{ |
26
|
|
|
return new static(Arr::pluck($this->items, $value, $key)); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Run a map over each of the items. |
31
|
|
|
* |
32
|
|
|
* @param callable $callback |
33
|
|
|
* @return static |
34
|
|
|
*/ |
35
|
|
|
public function map(callable $callback) |
36
|
|
|
{ |
37
|
|
|
$keys = array_keys($this->items); |
38
|
|
|
|
39
|
|
|
$items = array_map($callback, $this->items, $keys); |
40
|
|
|
|
41
|
|
|
return new static(array_combine($keys, $items)); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Concatenate values of a given key as a string. |
46
|
|
|
* |
47
|
|
|
* @param string $value |
48
|
|
|
* @param string|null $glue |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function implode($value, $glue = null) |
52
|
|
|
{ |
53
|
|
|
$first = $this->first(); |
|
|
|
|
54
|
|
|
|
55
|
|
|
if (is_array($first) || is_object($first)) { |
56
|
|
|
return implode($glue, $this->pluck($value)->all()); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return implode($value, $this->items); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Transform each item in the collection using a callback. |
64
|
|
|
* |
65
|
|
|
* @param callable $callback |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
|
|
public function transform(callable $callback) |
69
|
|
|
{ |
70
|
|
|
$this->items = $this->map($callback)->all(); |
|
|
|
|
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public function toArray(): array |
79
|
|
|
{ |
80
|
|
|
return array_map(function ($value) { |
81
|
|
|
return $value instanceof AbstractCollection ? $value->toArray() : $value; |
82
|
|
|
}, $this->items); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns a serialized string representation of this array object. |
87
|
|
|
* |
88
|
|
|
* @link http://php.net/manual/en/serializable.serialize.php Serializable::serialize() |
89
|
|
|
* |
90
|
|
|
* @return string a PHP serialized string. |
91
|
|
|
*/ |
92
|
|
|
public function serialize(): string |
93
|
|
|
{ |
94
|
|
|
$properties = $this->__sleep(); |
95
|
|
|
$data = []; |
96
|
|
|
foreach ($properties as $property) { |
97
|
|
|
$data[$property] = $this->{$property}; |
98
|
|
|
} |
99
|
|
|
return serialize($data); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
public function __sleep() |
106
|
|
|
{ |
107
|
|
|
return $this->serializable; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Converts a serialized string representation into an instance object. |
112
|
|
|
* |
113
|
|
|
* @link http://php.net/manual/en/serializable.unserialize.php Serializable::unserialize() |
114
|
|
|
* |
115
|
|
|
* @param string $serialized A PHP serialized string to unserialize. |
116
|
|
|
* |
117
|
|
|
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint |
118
|
|
|
*/ |
119
|
|
|
public function unserialize($serialized): void |
120
|
|
|
{ |
121
|
|
|
/** @var array<array-key, T> $data */ |
122
|
|
|
$data = unserialize($serialized, ['allowed_classes' => $this->unserializeAllowedClasses()]); |
123
|
|
|
if (!is_array($data)) { |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
foreach ($data as $property => $value) { |
127
|
|
|
$this->{$property} = $value; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Specify data which should be serialized to JSON |
133
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
134
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
135
|
|
|
* which is a value of any type other than a resource. |
136
|
|
|
* @since 5.4.0 |
137
|
|
|
*/ |
138
|
|
|
public function jsonSerialize() |
139
|
|
|
{ |
140
|
|
|
return array_map(function ($value) { |
141
|
|
|
if ($value instanceof JsonSerializable) { |
142
|
|
|
return $value->jsonSerialize(); |
143
|
|
|
} else { |
144
|
|
|
return $value; |
145
|
|
|
} |
146
|
|
|
}, $this->items); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
protected function unserializeAllowedClasses() |
150
|
|
|
{ |
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.