1 | <?php |
||
7 | class Chain |
||
8 | { |
||
9 | /** |
||
10 | * Create a new chain from a source. |
||
11 | * |
||
12 | * @param array|Traversable $source |
||
13 | * |
||
14 | * @return static |
||
15 | */ |
||
16 | 12 | public static function from($source) |
|
20 | |||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | private $source; |
||
25 | |||
26 | /** |
||
27 | * @param array $source |
||
28 | */ |
||
29 | 12 | public function __construct(array $source) |
|
33 | |||
34 | /** |
||
35 | * Get the current source. |
||
36 | * |
||
37 | * @return array |
||
38 | */ |
||
39 | 9 | public function toArray() |
|
43 | |||
44 | /** |
||
45 | * Check if a key is defined in the source. |
||
46 | * |
||
47 | * @param mixed $needle |
||
48 | * |
||
49 | * @return boolean |
||
50 | */ |
||
51 | 1 | public function hasKey($needle) |
|
55 | |||
56 | /** |
||
57 | * Check if a value is defined in the source. |
||
58 | * |
||
59 | * @param mixed $needle |
||
60 | * |
||
61 | * @return boolean |
||
62 | */ |
||
63 | 1 | public function hasValue($needle) |
|
67 | |||
68 | /** |
||
69 | * Get a copy with only values. |
||
70 | * |
||
71 | * @return static |
||
72 | */ |
||
73 | 1 | public function values() |
|
80 | |||
81 | /** |
||
82 | * Get a copy with only keys. |
||
83 | * |
||
84 | * @return static |
||
85 | */ |
||
86 | 1 | public function keys() |
|
93 | |||
94 | /** |
||
95 | * Get a copy merged with new values. |
||
96 | * |
||
97 | * @param array $values |
||
98 | * |
||
99 | * @return static |
||
100 | */ |
||
101 | 1 | public function merge(array $values) |
|
108 | |||
109 | /** |
||
110 | * Get a copy intersected with new values. |
||
111 | * |
||
112 | * @param array $values |
||
113 | * |
||
114 | * @return static |
||
115 | */ |
||
116 | 1 | public function intersect(array $values) |
|
123 | |||
124 | /** |
||
125 | * Get a copy diffed with other values. |
||
126 | * |
||
127 | * @param array $values |
||
128 | * |
||
129 | * @return static |
||
130 | */ |
||
131 | 1 | public function diff(array $values) |
|
138 | |||
139 | /** |
||
140 | * Get a copy with only unique values. |
||
141 | * |
||
142 | * @return static |
||
143 | */ |
||
144 | 1 | public function unique() |
|
151 | |||
152 | /** |
||
153 | * Reduce the array with a callback. |
||
154 | * |
||
155 | * @param callable $fn |
||
156 | * @param mixed $initial |
||
157 | * |
||
158 | * @return mixed |
||
159 | */ |
||
160 | 1 | public function reduce(callable $fn, $initial = null) |
|
164 | |||
165 | /** |
||
166 | * Get a copy filtered with a callback. |
||
167 | * |
||
168 | * @param callable $fn |
||
169 | * |
||
170 | * @return static |
||
171 | */ |
||
172 | 1 | public function filter(callable $fn) |
|
179 | |||
180 | /** |
||
181 | * Get a copy mapped with a callback. |
||
182 | * |
||
183 | * @param callable $fn |
||
184 | * |
||
185 | * @return static |
||
186 | */ |
||
187 | 1 | public function map(callable $fn) |
|
194 | } |
||
195 |