| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the ramsey/collection library |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
* |
| 9 |
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> |
| 10 |
* @license http://opensource.org/licenses/MIT MIT |
| 11 |
*/ |
| 12 |
declare (strict_types=1); |
| 13 |
namespace Dudlewebs\WPMCS\Ramsey\Collection; |
| 14 |
|
| 15 |
/** |
| 16 |
* A collection represents a group of objects. |
| 17 |
* |
| 18 |
* Each object in the collection is of a specific, defined type. |
| 19 |
* |
| 20 |
* This is a direct implementation of `CollectionInterface`, provided for |
| 21 |
* the sake of convenience. |
| 22 |
* |
| 23 |
* Example usage: |
| 24 |
* |
| 25 |
* ``` php |
| 26 |
* $collection = new \Ramsey\Collection\Collection('My\\Foo'); |
| 27 |
* $collection->add(new \My\Foo()); |
| 28 |
* $collection->add(new \My\Foo()); |
| 29 |
* |
| 30 |
* foreach ($collection as $foo) { |
| 31 |
* // Do something with $foo |
| 32 |
* } |
| 33 |
* ``` |
| 34 |
* |
| 35 |
* It is preferable to subclass `AbstractCollection` to create your own typed |
| 36 |
* collections. For example: |
| 37 |
* |
| 38 |
* ``` php |
| 39 |
* namespace My\Foo; |
| 40 |
* |
| 41 |
* class FooCollection extends \Ramsey\Collection\AbstractCollection |
| 42 |
* { |
| 43 |
* public function getType() |
| 44 |
* { |
| 45 |
* return 'My\\Foo'; |
| 46 |
* } |
| 47 |
* } |
| 48 |
* ``` |
| 49 |
* |
| 50 |
* And then use it similarly to the earlier example: |
| 51 |
* |
| 52 |
* ``` php |
| 53 |
* $fooCollection = new \My\Foo\FooCollection(); |
| 54 |
* $fooCollection->add(new \My\Foo()); |
| 55 |
* $fooCollection->add(new \My\Foo()); |
| 56 |
* |
| 57 |
* foreach ($fooCollection as $foo) { |
| 58 |
* // Do something with $foo |
| 59 |
* } |
| 60 |
* ``` |
| 61 |
* |
| 62 |
* The benefit with this approach is that you may do type-checking on the |
| 63 |
* collection object: |
| 64 |
* |
| 65 |
* ``` php |
| 66 |
* if ($collection instanceof \My\Foo\FooCollection) { |
| 67 |
* // the collection is a collection of My\Foo objects |
| 68 |
* } |
| 69 |
* ``` |
| 70 |
* |
| 71 |
* @template T |
| 72 |
* @extends AbstractCollection<T> |
| 73 |
*/ |
| 74 |
class Collection extends AbstractCollection |
| 75 |
{ |
| 76 |
/** |
| 77 |
* The type of elements stored in this collection. |
| 78 |
* |
| 79 |
* A collection's type is immutable once it is set. For this reason, this |
| 80 |
* property is set private. |
| 81 |
*/ |
| 82 |
private string $collectionType; |
| 83 |
/** |
| 84 |
* Constructs a collection object of the specified type, optionally with the |
| 85 |
* specified data. |
| 86 |
* |
| 87 |
* @param string $collectionType The type (FQCN) associated with this |
| 88 |
* collection. |
| 89 |
* @param array<array-key, T> $data The initial items to store in the collection. |
| 90 |
*/ |
| 91 |
public function __construct(string $collectionType, array $data = []) |
| 92 |
{ |
| 93 |
$this->collectionType = $collectionType; |
| 94 |
parent::__construct($data); |
| 95 |
} |
| 96 |
public function getType(): string |
| 97 |
{ |
| 98 |
return $this->collectionType; |
| 99 |
} |
| 100 |
} |
| 101 |
|