项目作者: teodoroleckie

项目描述 :
⚡ Simple and powerful library for collections in PHP
高级语言: PHP
项目地址: git://github.com/teodoroleckie/collection.git
创建时间: 2021-04-30T05:46:48Z
项目社区:https://github.com/teodoroleckie/collection

开源协议:MIT License

下载


Simple and powerful library for collections in PHP

Latest Version on Packagist
Scrutinizer Code Quality
Build Status
Total Downloads
Code Intelligence Status

Installation

You can install the package via composer:

  1. composer require tleckie/collection

Usage

Hash

Hash collections should be used when you need the keys to be strings.

  1. <?php
  2. use Tleckie\Collection\Hash;
  3. use Tleckie\Collection\HashInterface;
  4. // Create your own collection type
  5. class UserCollection extends Hash {}
  6. $users = [
  7. 'user1' => new User('John'),
  8. 'user2' => new User('Pedro'),
  9. 'user3' => new User('Mario'),
  10. 'user4' => new User('Walter'),
  11. 'user5' => new User('Mario')
  12. ];
  13. $userCollection = new UserCollection($users);
  14. /**
  15. * @param HashInterface $userCollection
  16. */
  17. public function addCollection(HashInterface $userCollection){ ... }
  18. /**
  19. * @param UserCollection $userCollection
  20. */
  21. public function addCollection(UserCollection $userCollection){ ... }
  1. use Tleckie\Collection\Hash;
  2. $collection = new Hash(['key' => 'value']);
  3. $collection->append('otherKey', 'other value');

Hash methods:

  1. public function current(): mixed;
  2. public function next(): void;
  3. public function key(): null|string;
  4. public function valid(): bool;
  5. public function rewind(): void;
  6. public function count(): int;
  7. public function sortByKey(): HashInterface;
  8. public function sortByValue(): HashInterface;
  9. public function sort(callable$callable): HashInterface;
  10. public function reverse(): HashInterface;
  11. public function filter(callable $filterFunction): HashInterface;
  12. public function find(callable $findFunction): HashInterface;
  13. public function empty(): HashInterface;
  14. public function hasKey(string $key): bool;
  15. public function hasValue(mixed $item): bool;
  16. public function get(string $key): mixed;
  17. public function append(string $key, mixed $item): HashInterface;
  18. public function prepend(string $key, mixed $item): HashInterface;
  19. public function remove(string $key): HashInterface;
  20. public function shuffle(): HashInterface;
  21. public function toArray(): array;

Sequence:

The sequences are used when the key of the collection is not important besides being ordered. Many of its methods are immutable.

If you want you can create your own type of collection

  1. <?php
  2. use Tleckie\Collection\Sequence;
  3. use Tleckie\Collection\SequenceInterface;
  4. // Create your own collection type
  5. class UserCollection extends Sequence {}
  6. $users = [
  7. new User('John'),
  8. new User( 'Pedro'),
  9. new User('Mario'),
  10. new User('Walter'),
  11. new User('Mario')
  12. ];
  13. $userCollection = new UserCollection($users);
  14. /**
  15. * @param SequenceInterface $userCollection
  16. */
  17. public function addCollection(SequenceInterface $userCollection){ ... }
  18. /**
  19. * @param UserCollection $userCollection
  20. */
  21. public function addCollection(UserCollection $userCollection){ ... }
  1. use Tleckie\Collection\Sequence;
  2. $collection = new Sequence(['value', 'value2']);
  3. $collection->append('other value');

Sequence methods:

  1. public function current(): mixed;
  2. public function next(): void;
  3. public function key(): null|int;
  4. public function valid(): bool;
  5. public function rewind(): void;
  6. public function count(): int;
  7. public function sortByKey(): SequenceInterface;
  8. public function sortByValue(): SequenceInterface;
  9. public function sort(callable$callable): SequenceInterface;
  10. public function reverse(): SequenceInterface;
  11. public function filter(callable $filterFunction): SequenceInterface;
  12. public function find(callable $findFunction): SequenceInterface;
  13. public function empty(): SequenceInterface;
  14. public function hasKey(int $index): bool;
  15. public function hasValue(mixed $item): bool;
  16. public function get(int $index): mixed;
  17. public function append(mixed $item): SequenceInterface;
  18. public function prepend(mixed $item): SequenceInterface;
  19. public function remove(int $index): SequenceInterface;
  20. public function shuffle(): SequenceInterface;
  21. public function toArray(): array;

Collection:

Use the collection when you have numeric and string keys.

  1. <?php
  2. use Tleckie\Collection\Collection;
  3. // Create your own collection type
  4. class UserCollection extends Collection {}
  5. $users = [
  6. new User(1, 'John'),
  7. new User(2, 'Pedro'),
  8. new User(3, 'Mario'),
  9. new User(4, 'Walter'),
  10. new User(5, 'Mario')
  11. ];
  12. $collection = new UserCollection($users);
  13. // iterate
  14. foreach($collection as $user){
  15. $user->name();
  16. }
  17. // count elements
  18. $collection->count(); //5
  19. count($collection); //5
  20. // filter
  21. $collection = $collection->filter(function (int $key, User $user) {
  22. return $user->id() > 4;
  23. }); // UserCollection( [ User(5) ] );
  24. // filterNot
  25. $collection = $collection->filterNot(function (int $key, User $user) {
  26. return $user->id() > 4;
  27. }); // UserCollection( [ User(1),User(2),User(3),User(4) ] );
  28. // find
  29. $collection = $collection->find(function (int $key, User $user) {
  30. return $user->name() === 'Mario';
  31. }); // UserCollection( [ User(3), User(5) ] );
  32. // findIndex
  33. $collection = $collection->find(function (int $key, User $user) {
  34. return $key === 1;
  35. }); // UserCollection( [ User(2) ] );
  36. // sort
  37. $collection = $collection->sort(function (User $current, User $next) {
  38. if ($current->id() === $next->id()) {
  39. return 0;
  40. }
  41. return ($current->id() > $next->id()) ? -1 : 1;
  42. }); // UserCollection( [ User(5), User(4), User(3), User(2), User(1) ] );

Other methods:

  1. $collection->shuffle(): CollectionInterface;
  2. $collection->reverse(): CollectionInterface;
  3. $collection->prepend(mixed $item): CollectionInterface;
  4. $collection->append(mixed $item): CollectionInterface;
  5. $collection->toArray(): array;
  6. $collection->push(mixed $item): CollectionInterface;
  7. $collection->pull(): mixed;