PHP code example of managur / collection
1. Go to this page and download the library: Download managur/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/ */
managur / collection example snippets
use Managur\Collection\Collection;
$collection = new Collection(['your', 'collectible', 'data']);
$collection = collect(['your', 'collectible', 'data']);
declare(strict_types=1);
use Managur\Collection\Collection;
final class IntegerCollection extends Collection
{
protected $valueType = 'integer';
}
declare(strict_types=1);
use Managur\Collection\Collection;
final class IntegerKeyCollection extends Collection
{
protected $keyType = 'integer';
}
protected function keyStrategy($value)
{
return $value->id();
}
$user = new User('my-user-id', 'Managur User');
$collection = new UserCollection();
$collection[] = $user;
var_dump($collection['my-user-id'] === $user); // returns true
use Managur\Collection\Collection;
$integerValueCollection = Collection::newTypedValueCollection('integer', [1,2,3,4,5]);
$integerKeyCollection = Collection::newTypedKeyCollection('integer', ['a','b',1]);
$integerCollection = Collection::newTypedCollection('integer', 'integer', [1,2,3,4,5]);
$collection[] = 'new data';
$collection->append('new data');
$collection[4] = 'new data';
$collection->offsetSet(4, 'new data');
$collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
$doubles = $collection->map(function ($value) {
return $value * 2;
});
$ints = new IntegerCollection([1, 2, 3, 4, 5]);
$strings = $ints->mapInto(function (int $item): string {
return (string)($item*10);
}, StringCollection::class);
$collection = new Collection(['a' => 1, 'b' => 2, 'c' => 3]);
$doubles = $collection->slice(0, 2); // ['a' => 1, 'b' => 2]
$collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
$collection->each(function ($value) {
echo $value * 2;
});
$collection = new Collection($arrayOfUserObjects);
$emails = $collection->reduce(function ($carry, $user) {
$carry[] = $user->email();
return $carry
}, []);
$collection = new Collection($arrayOfUserObjects);
$filtered = $collection->filter(function ($user) {
return $user->isActive();
});
$collection = new Collection($arrayOfUserObjects);
$firstGmailUser = $collection->first(function ($user) {
return false !== stripos($user->emailAddress(), '@gmail.');
});
$collection = new Collection($arrayOfUserObjects);
$lastGmailUser = $collection->last(function ($user) {
return false !== stripos($user->emailAddress(), '@gmail.');
});
$collection = new Collection($arrayOfUserObjects);
$containsAGmailUser = $collection->contains(function ($user) {
return false !== stripos($user->emailAddress(), '@gmail.');
});
$collection = new Collection([]);
$collection->isEmpty(); // true
$collection = new Collection([1, 2, 3]);
$collection->isNotEmpty(); // true
$collection->push(5, 6, 7, 8, 9);
$collection = new Collection([1, 2, 3, 4, 5]);
$popped = $collection->pop();
$collection1 = new Collection([1,2,3,4,5]);
$collection2 = new Collection([6,7,8,9,10]);
$merged = $collection1->merge($collection2);
$collection1 = new Collection([1,2,3,4,5]);
$collection2 = $collection1->into(IntegerCollection::class);
$collection = collectInto(IntegerCollection::class, [1,2,3,4,5]);
$collection = new Collection([5,2,6,4,7,1]);
$sorted = $collection->sort();
$alsoSorted = $collection->sort(function ($a, $b) {
return $a <=> $b;
});
$collection = new Collection([5,2,6,4,7,1]);
$sorted = $collection->asort();
$alsoSorted = $collection->asort(function ($a, $b) {
return $a <=> $b;
});
$collection = new Collection([1,2,3,4,5]);
$shuffled = $collection->shuffle();
$collection = new Collection(['a', 'b']);
$joined = $collection->implode(', '); // 'a, b'
$collection = new Collection($users);
$joined = $collection->implode(', ', function (User $user): string {
return $user->name();
});