|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Arrayy\Collection; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* A collection represents a group of objects. |
|
7
|
|
|
* |
|
8
|
|
|
* Each object in the collection is of a specific, defined type. |
|
9
|
|
|
* |
|
10
|
|
|
* This is a direct implementation of `CollectionSetTypeInterface`, |
|
11
|
|
|
* which provides a simple api for your collections. |
|
12
|
|
|
* |
|
13
|
|
|
* Example usage: |
|
14
|
|
|
* |
|
15
|
|
|
* ``` php |
|
16
|
|
|
* $collection = new \Arrayy\Collection\Collection(\My\FooInterface::class); |
|
17
|
|
|
* $collection->add(new \My\Foo()); |
|
18
|
|
|
* $collection->add(new \My\Foo()); |
|
19
|
|
|
* |
|
20
|
|
|
* foreach ($collection as $foo) { |
|
21
|
|
|
* if ($foo instanceof \My\FooInterface) { |
|
22
|
|
|
* // Do something with $foo |
|
23
|
|
|
* } |
|
24
|
|
|
* } |
|
25
|
|
|
* ``` |
|
26
|
|
|
* |
|
27
|
|
|
* It is preferable to subclass `AbstractCollection` to create your own typed |
|
28
|
|
|
* collections. For example: |
|
29
|
|
|
* |
|
30
|
|
|
* ``` php |
|
31
|
|
|
* namespace My; |
|
32
|
|
|
* |
|
33
|
|
|
* class FooCollection extends \Arrayy\Collection\AbstractCollection |
|
34
|
|
|
* { |
|
35
|
|
|
* public function getType(): string |
|
36
|
|
|
* { |
|
37
|
|
|
* return FooInterface::class; |
|
38
|
|
|
* } |
|
39
|
|
|
* } |
|
40
|
|
|
* ``` |
|
41
|
|
|
* |
|
42
|
|
|
* And then use it similarly to the earlier example: |
|
43
|
|
|
* |
|
44
|
|
|
* ``` php |
|
45
|
|
|
* namespace My; |
|
46
|
|
|
* |
|
47
|
|
|
* $fooCollection = new \My\FooCollection(); |
|
48
|
|
|
* $fooCollection->add(new \My\Foo()); |
|
49
|
|
|
* $fooCollection->add(new \My\Foo()); |
|
50
|
|
|
* |
|
51
|
|
|
* foreach ($fooCollection as $foo) { |
|
52
|
|
|
* if ($foo instanceof \My\FooInterface) { |
|
53
|
|
|
* // Do something with $foo |
|
54
|
|
|
* } |
|
55
|
|
|
* } |
|
56
|
|
|
* ``` |
|
57
|
|
|
* |
|
58
|
|
|
* INFO: this collection thingy is inspired by https://github.com/ramsey/collection/ |
|
59
|
|
|
*/ |
|
60
|
|
|
class Collection extends AbstractCollection |
|
61
|
|
|
{ |
|
62
|
|
|
/** |
|
63
|
|
|
* The type of elements stored in this collection. |
|
64
|
|
|
* |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
private $collectionTypeTmp; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Constructs a collection object of the specified type, optionally with the |
|
71
|
|
|
* specified data. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $type |
|
74
|
|
|
* @param mixed $data |
|
75
|
|
|
* <p> |
|
76
|
|
|
* The initial items to store in the collection. |
|
77
|
|
|
* </p> |
|
78
|
|
|
*/ |
|
79
|
2 |
|
public function __construct(string $type, $data = []) |
|
80
|
|
|
{ |
|
81
|
2 |
|
$this->collectionTypeTmp = $type; |
|
82
|
|
|
|
|
83
|
2 |
|
parent::__construct($data); |
|
84
|
1 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* The type (FQCN) associated with this collection. |
|
88
|
|
|
* |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function getType(): string |
|
92
|
|
|
{ |
|
93
|
2 |
|
return $this->collectionTypeTmp; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|