Download the PHP package stephenfrank/typedcollections without Composer
On this page you can find all versions of the php package stephenfrank/typedcollections. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download stephenfrank/typedcollections
More information about stephenfrank/typedcollections
Files in stephenfrank/typedcollections
Package typedcollections
Short Description Collection classes for strict(ish) typing
License
Informations about the package typedcollections
TypedCollections
stephenfrank/typedcollections: "0.1.x"
A collection of helper classes to enforce type safety in array collections.
TypedCollection (base class)
The base class is array-accessible and enforces type-strictness by checking each item added to the collection against the defined type.
The type can be defined through the constructor arguments or will be defined automatically based on the first item added.
{Type}Collection
Several classes have built-in types for strict checking against:
- bools
BoolCollection
- integers
IntegerCollection
- floats
FloatCollection
- strings
StringCollection
- array
ArrayCollection
- objects
ObjectCollection
ClassCollection
The ClassCollection will check that each item added to the array is of a particular class using get_class()
.
InstanceCollection
The InstanceCollection will check each item using instanceof
so that collections can be defined by an interface or inherited class.
What’s the point?
Sometimes type strictness is important when exposing a public API in a framework or library. Effectively it reduces the number of places where the API consumer can shoot themselves in the foot.
Sometimes when using a third-party/legacy library you might want type-check against the kind of garbage they return and raise an exception when they don't return clean data
Lastly, InstanceCollection
makes it super easy to create your own typed collections.