1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/orm package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Orm\Entity; |
11
|
|
|
|
12
|
|
|
use League\Event\EmitterAwareTrait; |
13
|
|
|
use Slick\Common\Utils\Collection\AbstractCollection; |
14
|
|
|
use Slick\Orm\EntityInterface; |
15
|
|
|
use Slick\Orm\Event\EntityAdded; |
16
|
|
|
use Slick\Orm\Exception\EntityNotFoundException; |
17
|
|
|
use Slick\Orm\Exception\InvalidArgumentException; |
18
|
|
|
use Slick\Orm\Orm; |
19
|
|
|
use Slick\Orm\RepositoryInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Entity Collection |
23
|
|
|
* |
24
|
|
|
* @package Slick\Orm\Entity |
25
|
|
|
* @author Filipe Silva <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class EntityCollection extends AbstractCollection implements |
28
|
|
|
EntityCollectionInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $type; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var RepositoryInterface |
37
|
|
|
*/ |
38
|
|
|
protected $repository; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Emitter methods |
42
|
|
|
*/ |
43
|
|
|
use EmitterAwareTrait; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Creates the collection for entity type with provided data |
47
|
|
|
* |
48
|
|
|
* @param string $entityType |
49
|
|
|
* @param array|\Traversable $data |
50
|
|
|
*/ |
51
|
24 |
|
public function __construct($entityType, $data = []) |
52
|
|
|
{ |
53
|
24 |
|
parent::__construct($data); |
54
|
24 |
|
$this->type = $entityType; |
55
|
24 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Offset to set |
59
|
|
|
* |
60
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
61
|
|
|
* |
62
|
|
|
* @param mixed $offset The offset to assign the value to. |
63
|
|
|
* @param mixed $value The value to set. |
64
|
|
|
*/ |
65
|
2 |
|
public function offsetSet($offset, $value) |
66
|
|
|
{ |
67
|
2 |
|
$this->add($value); |
68
|
2 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Adds an entity to the collection |
72
|
|
|
* |
73
|
|
|
* This method adds an entity to the collection by accepting the entity |
74
|
|
|
* itself or the entity id value |
75
|
|
|
* |
76
|
|
|
* @param EntityInterface|integer $entity |
77
|
|
|
* |
78
|
|
|
* @throws EntityNotFoundException If the provided id is does not |
79
|
|
|
* references an existing entity. |
80
|
|
|
* @throws InvalidArgumentException If the entity is not from |
81
|
|
|
* the provided entity type |
82
|
|
|
* |
83
|
|
|
* @return self |
84
|
|
|
*/ |
85
|
6 |
|
public function add($entity) |
86
|
|
|
{ |
87
|
6 |
|
if ($entity instanceof EntityInterface) { |
88
|
6 |
|
return $this->addEntity($entity); |
89
|
|
|
} |
90
|
|
|
return $this->addEntityWithId($entity); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Adds an entity to the collection |
95
|
|
|
* |
96
|
|
|
* @param EntityInterface $entity |
97
|
|
|
* @throws InvalidArgumentException If the entity is not from |
98
|
|
|
* the provided entity type |
99
|
|
|
* |
100
|
|
|
* @return self |
101
|
|
|
*/ |
102
|
6 |
|
public function addEntity(EntityInterface $entity) |
103
|
|
|
{ |
104
|
6 |
|
if (!is_a($entity, $this->type)) { |
105
|
|
|
throw new InvalidArgumentException( |
106
|
|
|
"Trying to add an entity that is not a {$this->type}." |
107
|
|
|
); |
108
|
|
|
} |
109
|
6 |
|
$this->data[] = $entity; |
110
|
6 |
|
$event = new EntityAdded($entity); |
111
|
6 |
|
$this->getEmitter()->emit($event->getName()); |
112
|
6 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Adds an entity by entity id |
117
|
|
|
* |
118
|
|
|
* @param mixed $entityId |
119
|
|
|
* |
120
|
|
|
* @throws EntityNotFoundException If the provided id is does not |
121
|
|
|
* references an existing entity. |
122
|
|
|
* @throws InvalidArgumentException If the entity is not from |
123
|
|
|
* the provided entity type |
124
|
|
|
* |
125
|
|
|
* @return self |
126
|
|
|
*/ |
127
|
|
|
public function addEntityWithId($entityId) |
128
|
|
|
{ |
129
|
|
|
$entity = $this->getRepository()->get($entityId); |
130
|
|
|
if (!$entity) { |
131
|
|
|
throw new EntityNotFoundException( |
132
|
|
|
"The entity with is '{$entityId}' was not found." |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
return $this->addEntity($entity); |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Gets entity repository for this collection |
141
|
|
|
* |
142
|
|
|
* @return RepositoryInterface |
143
|
|
|
*/ |
144
|
|
|
public function getRepository() |
145
|
|
|
{ |
146
|
|
|
if (null == $this->repository) { |
147
|
|
|
$this->setRepository(Orm::getRepository($this->type)); |
148
|
|
|
} |
149
|
|
|
return $this->repository; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Set repository for this entity collection |
154
|
|
|
* |
155
|
|
|
* @param RepositoryInterface $repository |
156
|
|
|
* |
157
|
|
|
* @return self|$this|EntityCollection |
158
|
|
|
*/ |
159
|
|
|
public function setRepository(RepositoryInterface $repository) |
160
|
|
|
{ |
161
|
|
|
$this->repository = $repository; |
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
} |