fiv / collection
Easy way to iterate over your data
Installs: 28 840
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: 4.*
This package is auto-updated.
Last update: 2024-11-12 07:10:22 UTC
README
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
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.
Install
Via Composer
$ composer require fiv/collection
Base collection usage
$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.
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
$ ./vendor/bin/phpunit
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.