1. Go to this page and download the library: Download jakubgiminski/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/ */
jakubgiminski / collection example snippets
class UserCollection extends Collection
{
public function __construct(array $users = [])
{
parent::__construct(
$users,
// Set up typing - every element must be an instance of User
Type::object(User::class),
// Set up a unique index - user id
new UniqueIndex(function (User $user) {
return $user->getId();
})
);
}
}
class AddressCollection extends Collection
{
public function __construct(array $addresses = [])
{
parent::__construct(
$addresses,
// Set up typing - every element must be an array
Type::array(),
// Set up a unique index - all the fields
new UniqueIndex(function (array $address) {
return $address['postCode'] . $address['street'] . $address['homeNumber'];
})
);
}
}