@@ 9-68 (lines=60) @@ | ||
6 | use Isolate\LazyObjects\Proxy\Definition; |
|
7 | use Traversable; |
|
8 | ||
9 | class DefinitionCollection implements \IteratorAggregate |
|
10 | { |
|
11 | /** |
|
12 | * @var Definition[]|array |
|
13 | */ |
|
14 | private $definitions; |
|
15 | ||
16 | public function __construct(array $elements = array()) |
|
17 | { |
|
18 | $this->definitions = []; |
|
19 | ||
20 | foreach ($elements as $definitionFactory) { |
|
21 | $this->validateFactory($definitionFactory); |
|
22 | $definition = $definitionFactory->createDefinition(); |
|
23 | $this->validateDefinition($definition); |
|
24 | ||
25 | $this->definitions[] = $definition; |
|
26 | } |
|
27 | } |
|
28 | ||
29 | /** |
|
30 | * @param mixed $value |
|
31 | * @return bool |
|
32 | */ |
|
33 | public function add(Factory $definitionFactory) |
|
34 | { |
|
35 | $definition = $definitionFactory->createDefinition(); |
|
36 | $this->validateDefinition($definition); |
|
37 | ||
38 | $this->definitions[] = $definition; |
|
39 | } |
|
40 | ||
41 | /** |
|
42 | * @return \ArrayIterator |
|
43 | */ |
|
44 | public function getIterator() |
|
45 | { |
|
46 | return new \ArrayIterator($this->definitions); |
|
47 | } |
|
48 | ||
49 | /** |
|
50 | * @param $definitionFactory |
|
51 | */ |
|
52 | private function validateFactory($definitionFactory) |
|
53 | { |
|
54 | if (!$definitionFactory instanceof Factory) { |
|
55 | throw new \InvalidArgumentException("Definition collection elements needs to implement Definition\\Factory."); |
|
56 | } |
|
57 | } |
|
58 | ||
59 | /** |
|
60 | * @param $definition |
|
61 | */ |
|
62 | private function validateDefinition($definition) |
|
63 | { |
|
64 | if (!$definition instanceof Definition) { |
|
65 | throw new \RuntimeException("Definition\\Factory needs to create Entity\\Definition instance."); |
|
66 | } |
|
67 | } |
|
68 | } |
|
69 |
@@ 8-64 (lines=57) @@ | ||
5 | use Isolate\Framework\UnitOfWork\Entity\Definition\Factory; |
|
6 | use Isolate\UnitOfWork\Entity\Definition; |
|
7 | ||
8 | class DefinitionCollection implements \IteratorAggregate |
|
9 | { |
|
10 | private $definitions; |
|
11 | ||
12 | /** |
|
13 | * @param array $elements |
|
14 | */ |
|
15 | public function __construct(array $elements = array()) |
|
16 | { |
|
17 | $this->definitions = []; |
|
18 | ||
19 | foreach ($elements as $definitionFactory) { |
|
20 | $this->validateFactory($definitionFactory); |
|
21 | $definition = $definitionFactory->createDefinition(); |
|
22 | $this->validateDefinition($definition); |
|
23 | ||
24 | $this->definitions[] = $definition; |
|
25 | } |
|
26 | } |
|
27 | ||
28 | /** |
|
29 | * @param Factory $definitionFactory |
|
30 | * @return bool |
|
31 | */ |
|
32 | public function add(Factory $definitionFactory) |
|
33 | { |
|
34 | $definition = $definitionFactory->createDefinition(); |
|
35 | $this->validateDefinition($definition); |
|
36 | ||
37 | $this->definitions[] = $definition; |
|
38 | } |
|
39 | ||
40 | public function getIterator() |
|
41 | { |
|
42 | return new \ArrayIterator($this->definitions); |
|
43 | } |
|
44 | ||
45 | /** |
|
46 | * @param $definitionFactory |
|
47 | */ |
|
48 | private function validateFactory($definitionFactory) |
|
49 | { |
|
50 | if (!$definitionFactory instanceof Factory) { |
|
51 | throw new \InvalidArgumentException("Definition collection elements needs to implement Definition\\Factory."); |
|
52 | } |
|
53 | } |
|
54 | ||
55 | /** |
|
56 | * @param $definition |
|
57 | */ |
|
58 | private function validateDefinition($definition) |
|
59 | { |
|
60 | if (!$definition instanceof Definition) { |
|
61 | throw new \RuntimeException("Definition\\Factory needs to create Entity\\Definition instance."); |
|
62 | } |
|
63 | } |
|
64 | } |
|
65 |