Download the PHP package da2e/generic-collection without Composer
On this page you can find all versions of the php package da2e/generic-collection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download da2e/generic-collection
More information about da2e/generic-collection
Files in da2e/generic-collection
Package generic-collection
Short Description PHP-based implementation for generic collection
License MIT
Informations about the package generic-collection
Generic collection
This small library provides PHP-based implementation for generic collection.
PHP lacks generics (in opposite to Java, C++, etc). The problem is that you can't control and be sure which types do elements implement in an array/collection/hash/...
An example:
In Java for example you can eliminate that by using the generics:
This library provides an OOP solution to have homogeneous types in a collection/array.
Built-in types
- DA2E\GenericCollection\Type\ArrayType (corresponds to PHP built-in is_array() function).
- DA2E\GenericCollection\Type\BoolType (corresponds to PHP built-in is_bool() function).
- DA2E\GenericCollection\Type\CallableType (corresponds to PHP built-in is_callable() function).
- DA2E\GenericCollection\Type\FloatType (corresponds to PHP built-in is_float() function).
- DA2E\GenericCollection\Type\IntType (corresponds to PHP built-in is_int() function).
- DA2E\GenericCollection\Type\IterableType (corresponds to PHP built-in is_iterable() function).
- DA2E\GenericCollection\Type\MixedType (passes any value through).
- DA2E\GenericCollection\Type\NullType (corresponds to PHP built-in is_null() function).
- DA2E\GenericCollection\Type\NumericType (corresponds to PHP built-in is_numeric() function).
- DA2E\GenericCollection\Type\ObjectType (corresponds to PHP built-in is_object() function).
- DA2E\GenericCollection\Type\ResourceType (corresponds to PHP built-in is_resource() function).
- DA2E\GenericCollection\Type\ScalarType (corresponds to PHP built-in is_scalar() function).
- DA2E\GenericCollection\Type\StringType (corresponds to PHP built-in is_string() function).
Special types:
-
DA2E\GenericCollection\Type\CustomType: accepts a callback function with one argument to build a custom validator. E.g.:
- DA2E\GenericCollection\Type\GCollectionType: a type for embedded GCollections. E.g.:
ObjectType
ObjectType optionally accepts a fully-qualified class name to validate that value implements the given class. e.g.:
How to use
- Create a Generic Collection.
- Pass a Type for that collection in a constructor.
- Generic Collection implements \ArrayAccess interface, so just set/push elements to array.
- While adding elements to the collection it is immediately validated for the given type.
- If the type is invalid, a DA2E\GenericCollection\Exception\InvalidTypeException will be thrown.
An example:
- If you pass the collection to a method/function as an argument, you could demand that all elements should implement an exact type:
If the collection contains object you could additionally assure that elements are instances of a specific class.
Additional functions of GCollectionInterface
There many additional functions in GCollectionInterface (e.g. map, filter, slice, shuffle, etc.). Please refer to the interface to see all of the functions.