Download the PHP package hectororm/collection without Composer
On this page you can find all versions of the php package hectororm/collection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hectororm/collection
More information about hectororm/collection
Files in hectororm/collection
Package collection
Short Description Hector Collection
License MIT
Homepage https://gethectororm.com
Informations about the package collection
Hector Collection
Hector Collection is the module of Hector ORM. Can be used independently of ORM.
Installation
Composer
You can install Hector Collection with Composer, it's the recommended installation.
Dependencies
- PHP ^8.0
Usage
Collection can be view like an array. But you don't call array_*()
functions but directly methods of collection.
The collections implement CollectionInterface
, only Collection
is countable.
Available collections:
Collection
: default collectionLazyCollection
: lazy collection whose uses generators to improve performances, but is unique usage!
CollectionInterface
methods
CollectionInterface::new(Closure|iterable $iterable): static
Create new instance of current collection.
Collection::count(): int
Count number of items in collection.
For lazy collection, all iterator is traversed.
Similar to count()
function.
CollectionInterface::getArrayCopy(): array
Use method to get the array representation of your collection.
CollectionInterface::all(): array
Use method to get all values of your collection as array.
CollectionInterface::isEmpty(): bool
Know if collection is empty.
CollectionInterface::isList(): bool
Know if collection is list.
CollectionInterface::collect(): self
Collect all data from current collection into another one.
For lazy collection, collect all remaining items into classic collection.
CollectionInterface::sort(callable|int|null $callback = null): static
Sort items of collection.
Similar to PHP array sort functions.
CollectionInterface::multiSort(callable ...$callback): static
Multi sort items of collection.
CollectionInterface::filter(?callable $callback = null): static
Filter items with callback.
Similar to array_filter()
function.
CollectionInterface::filterInstanceOf(string|object ...$class): static
Filter items with object instance comparison.
Similar to is_a()
function.
CollectionInterface::map(callable $callback): static
Apply callback on items and return result of callback.
Similar to array_map()
function.
CollectionInterface::each(callable $callback): static
Apply callback on items and return items.
Similar to array_walk()
function.
CollectionInterface::search(callable|mixed $needle): int|string|false
Search key of item with callback or value.
Similar to array_search()
function.
CollectionInterface::get(int $index = 0): mixed
Get item at index.
Negative index, starts at end of collection.
CollectionInterface::first(?callable $callback = null): mixed
Search first item.
If callback given, search first item whose respect callback.
CollectionInterface::last()
Search last item.
If callback given, search first item whose respect callback.
CollectionInterface::slice(int $offset, int|null $length = null): static
Extract a slice of the collection.
Similar to array_slice()
function.
CollectionInterface::contains(mixed $value, bool $strict = false): bool
Collection contains value?
Similar to in_array()
function.
CollectionInterface::chunk(int $length): static
Chunk collection items into collection of fixed length.
Similar to array_chunk()
function.
CollectionInterface::keys(): static
Get keys of collection items.
Similar to array_keys()
function.
CollectionInterface::values(): static
Get values of collection items.
Similar to array_values()
function.
CollectionInterface::unique(): static
Get uniques items of collection items.
Similar to array_unique()
function.
CollectionInterface::flip(): static
Flip keys and values.
Similar to array_flip()
function.
CollectionInterface::reverse(bool $preserve_keys = false): static
Reverse order of items.
Similar to array_reverse()
function.
CollectionInterface::column(string|int|Closure|null $column_key, string|int|Closure|null $index_key = null): static
Get column value or reindex collection.
Similar to array_column()
function.
CollectionInterface::rand(int $length = 1): static
Get random values of collection.
Similar to array_rand()
function.
CollectionInterface::sum(): float|int
Get sum of values.
Similar to array_sum()
function.
CollectionInterface::avg(): float|int
Get average of values.
CollectionInterface::median(): float|int
Get median of values.
CollectionInterface::variance(): float|int
Get population variance of values.
CollectionInterface::deviation(): float|int
Get population deviation of values.
CollectionInterface::reduce(callable $callback, mixed $initial = null): mixed
Reduce collection with callback and optional initial value.
Similar to array_reduce()
function.
Collection
methods
Collection
class implement ArrayAccess
interface to allow to manipulate collection like an array.
Collection::append(mixed ...$value): static
Append value(s) to collection.
Similar to array_push()
function.
Collection::prepend()
Prepend value(s) to collection.
Similar to array_unshift()
function.
Collection::lazy(): CollectionInterface
New lazy collection from current collection.
LazyCollection
methods
Lazy collection use generators to improve performances. Usage of a collection is unique, but can be chained.