aircury/collection

Library to provide collection structures, to use instead of arrays


README

PHP Arrays with additional functionality. Arrays on steroids.

Usage

There are two ways of using this library. To create Collections of objects or Collections of PHP scalar types.

Object Collections

To create you own Collection, the easiest way is to copy and adjust the sample CarCollection, replacing Car with the class that you need.

More generically, these are the steps to create your own Collection:

  • Extend the AbstractCollection class
  • Implement on the child these methods,
    • getClass() to provide information about the expected class or interface that all the objects on the collection should be.
    • offsetGet($offset) to specify the return class.
    • toArray() to provide PHPDoc information about the elements of the array.
    • first() to specify the return class.

Scalar type Collections

This library comes out of the box with the following Collections, ready to use:

  • StringCollection
  • StringOrNullCollection
  • IntegerCollection
  • IntegerOrNullCollection

These classes include additional expected functionality that you would expect from the data type, e.g. implode(), sum(), ...

If they do not suit your needs, you can extend any of them or the AbstractScalarTypeCollection to implement your own.