PHP code example of iresults / collection

1. Go to this page and download the library: Download iresults/collection library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

iresults / collection example snippets



use Iresults\Collection\Collection;
$collection = new Collection('a', 'b', 'c');
$result = $collection->reduce(
    fn ( ?string $carry, string $item) => ($carry ?? 'the start') . '/' . strtoupper($item)
);
assert('the start/A/B/C' === $result);


use Iresults\Collection\AbstractCollection;

/**
 * @extends AbstractCollection<Person>
 */
class PersonCollection extends AbstractCollection
{
    public function __construct(Person ...$items)
    {
        parent::__construct(...$items);
    }
}


$collection = new PersonCollection(
    new Person('Daniel'),
    new Person('Gary'),
    new Person('Loren'),
);

$result = $collection->filter(
    fn (Person $item) => 'Gary' === $item->name
);
assert($result instanceof PersonCollection);
assert(1 === $result->count());
assert([new Person('Gary')] == $result->getArrayCopy());


use Iresults\Collection\Map;
use Iresults\Collection\Pair;

final readonly class Person {
    public function __construct(public string $name){}
}

final readonly class PersonalInformation {
    public function __construct(public int $age) {}
}


/** @var Map<Person,PersonalInformation> $exampleMap */
$exampleMap = new Map(
    new Pair(new Person('Daniel'), new PersonalInformation(37)),
    new Pair(new Person('Gary'), new PersonalInformation(61)),
    new Pair(new Person('Loren'), new PersonalInformation(23)),
);
$result = $fixture->map(
    fn (
        PersonalInformation $pi,
        Person $person,
    ) => $person->name . ' is ' . $pi->age . ' years old'
);
assert($result instanceof Map);
$expected = [
    'Daniel is 37 years old',
    'Gary is 61 years old',
    'Loren is 23 years old',
];
assert(array_values($result->getValues()) === $expected);