alcamo / collection
Collection classes and traits that work to some extent like arrays
Installs: 125
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/alcamo/collection
Requires
- php: 7.3 - 8.0
- alcamo/exception: ^0.1
- php-ds/php-ds: ^1.3
Requires (Dev)
- alcamo/string: ^0.6
- phpunit/phpunit: ^9
- squizlabs/php_codesniffer: 3.*
README
use alcamo\collection\Collection;
class CollectionWithHash extends Collection
{
public function sha1(): string
{
return sha1(implode('|', $this->data_));
}
}
$collection = new CollectionWithHash([ 'foo', 'bar' ]);
echo $collection[0] . "\n";
echo count($collection) . "\n";
echo $collection->sha1() . "\n";
This will output
foo
2
4fa0d6984df3b91af1f0942b7522987783050b90
The $collection object behaves very much like an array, supporting
count(), iteration and array access. You can add functionality by
adding more methods to your class derived from Collection.
Overview
This package provides collection traits and classes featuring
array-like behaviour by accessing an inner object $data_, which may
be, for instance, an array or an SPL object.
This allows to restrict access to the inner object, for example implementing a readonly array, or to add further methods.
In addition, this package contains some specific collection classes, some of which are based on this mechanism.
Ready-to-use traits and classes
-
Collectionis a class with an array as$data_that implements theCountable,IteratorandArrayAccessinterfaces and provides some further methods. All indexes are converted to strings so that objects having a__toString()method can be used as keys without need for explicit conversion.All of its functionality is actually contained in the trait
CollectionTraitso that it is easy to integrate into existing class hierarchies. -
ReadonlyCollectionis the same except that it prevents write access through theArrayAccessinterface. Again, all of its functionality is contained in a trait,ReadonlyCollectionTrait. -
SplObjectStorageCollection(usingSplObjectStorageCollectionTrait) andReadonlySplObjectStorageCollection(usingReadonlySplObjectStorageCollectionTrait) do the same with anSplObjectStorageobject as$data_. -
PrefixFirstMatchCollection(usingPrefixFirstMatchCollectionTrait) andReadonlyPrefixFirstMatchCollection(usingReadonlyPrefixFirstMatchCollectionTrait) do the same asCollectionandReadonlyCollectionexcept that array access works on the first array elementwhose key matches an initial substring of the supplied offset string (or object which is implicitly converted to a string). -
ReadonlyPrefixSetis a particular-purpose class making use of the traits in this package. It implements a set of prefixes and provides an efficient methodcontains()that checks whether a given string matches one of the prefixes in the set. -
ReadonlyPrefixBlackWhiteListis based onReadonlyPrefixSet, adding the feature that the object can be used as either a blacklist or a whitelist. -
StringSetOrAnyStringimplements an object which represents either a finite set of strings or the set of all strings.
Provided traits
The above classes and traits are built from the following atomic traits.
Traits providing the inner object
ArrayDataTraitprovides an inner object of typearray.SplObjectStorageDataTraitprovides an inner object of typeSplObjectStorage
Extensions of this package can easily provide similar traits for other object types.
Cloning
CloneTrait provides a __clone() method to create a deep copy of
$data_. It works for both arrays and objects.
Item count
CountableTrait provides a method count() telling the number of
items in $data_. It works for both arrays and objects.
Iteration
ArrayIteratorTraitprovides the methods needed for theIteratorinterface when$data_is an array. In addition, this trait supplies methods to access the first and the last value in the array and to get all keys of the array.ObjectIteratorTraitdoes the same for an inner object that implementsIterator.SplObjectStorageIteratorTraitdoes the same for an inner object of classSplObjectStorageIterator. A separate trait is needed becauseSplObjectStorageiteration does not behave as one would expect.IteratorAggregateTraitprovides the methods needed for theIteratorAggregateinterface. To use it,$data_must be an object that implementsIteratorAggregate.
All of these traits result in a collection object that supports
iteration, but the developer must know at compile time which type
$data_ will have.
Array access
ReadArrayAccessTraitprovides the methods of theArrayAccessinterface needed for reading.StringIndexedReadArrayAccessTraitdoes the same but converts all keys to strings. This allows for direct use of objects as keys if the have a__toString()method.PrefixFirstMatchReadArrayAccessTraitworks similar toStringIndexedReadArrayAccessTraitbut works on the first array element whose key matches an initial substring of the supplied offset string (or object which is implicitly converted to a string).WriteArrayAccessTraitprovides the methods ofArrayAccessneeded for writing.StringIndexedWriteArrayAccessTraitdoes the same but converts all keys to strings. Again, this allows for direct use of objects as keys if the have a__toString()method.PreventWriteArrayAccessTraitimplements the methods needed for writing in a way that they throw aReadonlyViolationexception on each write attempt. Hence a class usingReadArrayAccessTraitandPreventWriteArrayAccessTraitimplements theArrayAccessinterface for a readonly array.
contains()
ContainsTrait provides a method contains() telling whether
$data_ has an item with the indicated value. It works for both
arrays and objects.
Calling methods of the inner object
DecoratorTrait forwards calls of unknown methods to $data_.
Other traits
BlackWhiteListTraitadds functionality that declares a collection to be a blacklist or a whitelist.