1. Go to this page and download the library: Download markhj/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/ */
markhj / collection example snippets
use Markhj\Collection\Collection;
$collection = new Collection;
$collection->push('John', 'Jane');
$collection->map(function($item) {
return $item . ' Doe';
});
var_dump($collection->all()); // John Doe, Jane Doe
public function push(...$objects): Collection;
public function set($key, $object): Collection;
public function add(Collection $collection): Collection;
public function get($key);
public function keyExists($key): bool;
public function all(): array;
public function count(?callable $filter = null): int;
public function empty(): Collection;
public function reverse(): Collection;
public function keys(): Collection;
public function toJson(): string;
public function indexOf($item): ?int;
public function remove(...$keys): Collection;
public function first();
public function last();
public function clone(): Collection;
public function append(Collection $collection): Collection;
public function prepend(Collection $collection): Collection;
public function crop(int $start, int $size): Collection;
public function limit(int $limit): Collection;
public function has($item): bool;
public function hasAllOf(...$items): bool;
public function hasAnyOf(...$items): bool;
public function map(callable $handler): Collection;
public function filter(callable $handler): Collection;
public function forEach(callable $handler): Collection;
public function retrieve(callable $filter): Collection;
public function merge(Collection $target): Collection;
public function mergePreserving(Collection $target): Collection;
public function append(Collection $collection): Collection;
public function prepend(Collection $collection): Collection;
public function before($key, Collection $collection): Collection;
public function after($key, Collection $collection): Collection;
public function isAssociative(): bool;
public function isGraceful(): bool;
$collection = new AssociativeCollection;
$collection->add('order', $order);
$collection->add('customer', $customer);
// ...
$collection->get('customer'); // Returns the customer
foreach ($collection as $key => $value) {
// ...
}
$a = (new Collection)->push(1, 2, 3);
$b = (new Collection)->push(1, 10, 20, 40);
$a->merge($b); // 1, 10, 20, 40
$a = (new Collection)->push(1, 2, 3);
$b = (new Collection)->push(1, 10, 20, 40);
$a->mergePreserving($b); // 1, 2, 3, 40
public function validate($item): bool
public function validate($item): bool
{
return is_string($item);
}
public function validate($item): bool
{
return get_class($item) == '...';
}
class MyCustomDateCollection extends Collection
{
public function earliest()
{
// ...
}
public function latest()
{
// ...
}
}
class PaymentMethodCollection extends Collection
{
public function hasExpiredCreditCard(): bool
{
// Pseudo/example code to find expired card
return $this->count(function($paymentMethod) {
return $paymentMethod->isExpired();
}) > 0;
}
public function getCreditCards(): Collection
{
return $this->retrieve(function($paymentMethod) {
// Logic to determine of $paymentMethod is a credit card
// or another type
});
}
public function getDefaultPaymentMethod(): ?PaymentMethod
{
// Find the default payment method
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.