NumberArrayList
Raxos\Collection\NumberArrayList is an ArrayList that accepts integers or floats. It implements ValidatedArrayListInterface and adds a sum helper.
php
class NumberArrayList extends ArrayList implements ValidatedArrayListInterfaceMethods
sum
php
public function sum(): float|intReturns the sum of all items as a float or int.
validateItem
php
public static function validateItem(mixed $item): voidThrows a CollectionInvalidTypeException when the item is neither an int nor a float. This runs for every item when the list is built through of().
Example
php
<?php
declare(strict_types=1);
use Raxos\Collection\NumberArrayList;
$prices = NumberArrayList::of([9.99, 4, 2.5]);
$prices->sum(); // 16.49See also the typed lists concept page.