PHP code example of guennichi / collection

1. Go to this page and download the library: Download guennichi/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/ */

    

guennichi / collection example snippets


use Guennichi\Collection\Collection;

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

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

$persons = new PersonCollection(
    new Person('Person1'),
    new Person('Person2'),
    new Person('Person3'),
);

$persons->first()->name // Person1. Also supports autocomplete, thanks @template annotations.
$persons->contains(new Person('Person1')); // false
$persons->count(); // 3
count($persons); // 3
$persons->each(static function (Person $person, int $index) {
    // Do something
});
$persons->filter(static fn (Person $person) => $person->name === 'Person3'); // new PersonCollection instance with [Person('Person3')]

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

/**
 * @extends Collection<Employee>
 */
final class EmployeeCollection extends Collection
{
    public function __construct(Employee ...$elements)
    {
        parent::__construct(...$elements);
    }
}

$employees = $persons->filter(static fn (Person $person) => in_array($person->name, ['Person1', 'Person3']))
                     ->mapTo(EmployeeCollection::class, static fn (Person $person) => new Employee($person->name));

foreach ($employees as $employee) {
    // $employee is instanceof Employee class
}

$employees->first(); // Employee('Person1'): instance of Employee class
$employees->every(static fn (Employee $employee) => !empty($employee->name)); // True
$employees->sortAscBy(static fn (Employee $employee) => $employee->name); // new collection instance with [Employee('Person1'), Employee('Person3')]
$employees->sortDescBy(static fn (Employee $employee) => $employee->name); // new collection instance with [Employee('Person3'), Employee('Person1')]

json_encode($employees) // [{"name":"Person1"}, {"name":"Person3"}]