Skip to content

Commit

Permalink
add toArray method
Browse files Browse the repository at this point in the history
  • Loading branch information
doganoo committed Dec 17, 2020
1 parent b7cfb3c commit 6ac50ce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Datastructure/Lists/ArrayList/ArrayList.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,6 @@ public function sort(): bool {
return $value !== null;
}, ARRAY_FILTER_USE_BOTH);


$timSort = new TimSort();
$array = $timSort->sort($array);

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

/**
* Returns the content as an array
*
* @return array
*/
public function toArray(): array {
return array_filter(
$this->array
, static function ($value, $key) {
return $value !== null;
}
, ARRAY_FILTER_USE_BOTH
);
}

/**
* Specify data which should be serialized to JSON
*
Expand Down
13 changes: 13 additions & 0 deletions tests/Lists/ArrayList/ArrayListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,17 @@ public function testSort() {
$this->assertTrue(6 === $arrayList->get(5));
}

public function testToArray(): void {
$arrayList = new ArrayList();
$arrayList->add(1);
$arrayList->add(2);
$arrayList->add(3);
$arrayList->add(4);
$arrayList->add(5);

$this->assertTrue(
[1, 2, 3, 4, 5] === $arrayList->toArray()
);
}

}

0 comments on commit 6ac50ce

Please sign in to comment.