Collection
, (*1)
Manipulate array of items in OOP style.
ObjectCollection
- contain/manipulate objects only with specific class
TypedCollection
- contain/manipulate items with defined validation rule
BaseCollection
- contain/manipulate any items, (*2)
ObjectCollection
it the most useful collection.
You can specify class of object and add only this objects. All validations perform ObjectCollection
Other data types restricted.
This class guarantee strict type data., (*3)
Install
Via Composer, (*4)
``` bash
$ composer require fiv/collection, (*5)
## Base collection usage
``` php
$collection = new \Fiv\Collection\BaseCollection();
$collection[] = 1;
$collection[] = 2;
echo $skeleton->getFirst()
Example : ObjectCollection
In java there ara features like ArrayList<UserModel>
in php you can extend ObjectCollection
to UserModelCollection
and define class name., (*6)
class UserModel {
public function getName(){
}
}
class UsersCollection extend ObjectCollection {
public function objectsClassName(){
return 'UserModel';
}
}
# ....
$users = new UserCollection();
foreach($users as $user){
# at this point you can drop out all you validations
# ObjectCollection guarantee that all items are UserModel
$user->getName();
}
$users->append(123); // Fail. 123 is not instance of UserModel
Testing
bash
$ ./vendor/bin/phpunit
, (*7)
Contributing
Please see CONTRIBUTING for details., (*8)
Credits
License
The MIT License (MIT). Please see License File for more information., (*9)