Skip to content

Commit 6ac50ce

Browse files
committed
add toArray method
1 parent b7cfb3c commit 6ac50ce

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Datastructure/Lists/ArrayList/ArrayList.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,6 @@ public function sort(): bool {
485485
return $value !== null;
486486
}, ARRAY_FILTER_USE_BOTH);
487487

488-
489488
$timSort = new TimSort();
490489
$array = $timSort->sort($array);
491490

@@ -499,6 +498,21 @@ public function sort(): bool {
499498
return true;
500499
}
501500

501+
/**
502+
* Returns the content as an array
503+
*
504+
* @return array
505+
*/
506+
public function toArray(): array {
507+
return array_filter(
508+
$this->array
509+
, static function ($value, $key) {
510+
return $value !== null;
511+
}
512+
, ARRAY_FILTER_USE_BOTH
513+
);
514+
}
515+
502516
/**
503517
* Specify data which should be serialized to JSON
504518
*

tests/Lists/ArrayList/ArrayListTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,17 @@ public function testSort() {
264264
$this->assertTrue(6 === $arrayList->get(5));
265265
}
266266

267+
public function testToArray(): void {
268+
$arrayList = new ArrayList();
269+
$arrayList->add(1);
270+
$arrayList->add(2);
271+
$arrayList->add(3);
272+
$arrayList->add(4);
273+
$arrayList->add(5);
274+
275+
$this->assertTrue(
276+
[1, 2, 3, 4, 5] === $arrayList->toArray()
277+
);
278+
}
279+
267280
}

0 commit comments

Comments
 (0)