Skip to content

Commit c9d90e1

Browse files
committed
Add collection traverse methods
1 parent 90f18fd commit c9d90e1

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

Traits/ArrayUtilities.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
trait ArrayUtilities
1010
{
11-
1211
/**
1312
* Applies the callback to the elements of the given arrays
1413
* https://www.php.net/manual/en/function.array-map.php
@@ -140,6 +139,38 @@ public function prepend(array|TraverseInterface $combine): self
140139
return $this->merge($combine, true);
141140
}
142141

142+
/**
143+
* Remove a portion of the array and replace it with something else
144+
* https://www.php.net/manual/en/function.array-splice.php
145+
*
146+
* @param int $offset
147+
* @param int|null $length
148+
* @param mixed $replacement
149+
* @param mixed|null $splicedResults
150+
* @return ArrayUtilities|Traverse
151+
*/
152+
public function splice(
153+
int $offset, ?int $length, mixed $replacement = [], mixed &$splicedResults = null
154+
): self {
155+
$splicedResults = array_splice($this->raw, $offset, $length, $replacement);
156+
$splicedResults = new self($splicedResults);
157+
return $this;
158+
}
159+
160+
/**
161+
* Extract a slice of the array
162+
* https://www.php.net/manual/en/function.array-slice.php
163+
*
164+
* @param int $offset
165+
* @param int|null $length
166+
* @param bool $preserveKeys
167+
* @return ArrayUtilities|Traverse
168+
*/
169+
public function slice(int $offset, ?int $length, bool $preserveKeys = false): self {
170+
$this->raw = array_slice($this->raw, $offset, $length, $preserveKeys);
171+
return $this;
172+
}
173+
143174
/**
144175
* Computes the difference of arrays
145176
* https://www.php.net/manual/en/function.array-diff.php
@@ -551,14 +582,13 @@ public function keys(): self
551582
* A helper function to handle collect args
552583
*
553584
* @param array|TraverseInterface $collect
554-
* @return array|TraverseInterface
585+
* @return array
555586
*/
556-
protected function handleCollectArg(array|TraverseInterface $collect): array|TraverseInterface
587+
protected function handleCollectArg(array|TraverseInterface $collect): array
557588
{
558589
if ($collect instanceof TraverseInterface) {
559590
$collect = $collect->toArray();
560591
}
561592
return $collect;
562593
}
563-
564594
}

Traverse.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,16 @@ public function validAndTravers(string $method, array $args, mixed $fallback = n
220220
return $this;
221221
}
222222

223+
223224
/**
224-
* Json decode value
225+
* Returns the JSON representation of a value
226+
* https://www.php.net/manual/en/function.json-encode.php
227+
*
225228
* @return self
226229
*/
227-
public function jsonDecode(): self
230+
public function toJson(mixed $value, int $flags = 0, int $depth = 512): mixed
228231
{
229-
$this->raw = json_decode($this->raw);
230-
return $this::value($this->raw);
232+
return json_encode($value, $flags, $depth);
231233
}
232234

233235
/**

tests/unitary-dto-traverse.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,18 @@
208208
'equal' => 'fish'
209209
], 'Last returned wrong value');
210210

211+
$this->add($obj->shopList->splice(1, 2, splicedResults: $result)->count(), [
212+
'equal' => 5
213+
], 'Splice returned wrong value');
214+
215+
$this->add($result->count(), [
216+
'equal' => 2
217+
], 'Spliced Results returned wrong value');
218+
219+
$this->add($obj->shopList->slice(1, 2)->count(), [
220+
'equal' => 2
221+
], 'Splice returned wrong value');
222+
211223
$this->add($obj->shopList->prepend(['prepend'])->first(), [
212224
'equal' => 'prepend'
213225
], 'Prepend returned wrong value');

0 commit comments

Comments
 (0)