1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace As3\Modlr\Models\Collections; |
4
|
|
|
|
5
|
|
|
use As3\Modlr\Metadata\EmbedMetadata; |
6
|
|
|
use As3\Modlr\Models\AbstractModel; |
7
|
|
|
use As3\Modlr\Models\Embed; |
8
|
|
|
use As3\Modlr\Store\Store; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Model collection that contains embedded fragments from a persistence (database) layer. |
12
|
|
|
* |
13
|
|
|
* @author Jacob Bare <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class EmbedCollection extends AbstractCollection |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var EmbedMetadata |
19
|
|
|
*/ |
20
|
|
|
protected $metadata; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructor. |
24
|
|
|
* |
25
|
|
|
* @param EmbedMetadata $metadata |
26
|
|
|
* @param Store $store |
27
|
|
|
* @param AbstractModel[] $models |
28
|
|
|
*/ |
29
|
|
|
public function __construct(EmbedMetadata $metadata, Store $store, array $models = []) |
30
|
|
|
{ |
31
|
|
|
$this->metadata = $metadata; |
32
|
|
|
parent::__construct($store, $models); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Creates a new Embed model instance based on the collection |
37
|
|
|
* |
38
|
|
|
* @return Embed |
39
|
|
|
*/ |
40
|
|
|
public function createNewEmbed() |
41
|
|
|
{ |
42
|
|
|
$embed = $this->store->loadEmbed($this->getMetadata(), []); |
43
|
|
|
$embed->getState()->setNew(); |
44
|
|
|
return $embed; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Gets the metadata for the model collection. |
49
|
|
|
* |
50
|
|
|
* @return EmbedMetadata |
51
|
|
|
*/ |
52
|
|
|
public function getMetadata() |
53
|
|
|
{ |
54
|
|
|
return $this->metadata; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function getType() |
61
|
|
|
{ |
62
|
|
|
return $this->getMetadata()->name; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function isDirty() |
69
|
|
|
{ |
70
|
|
|
if (true === $this->hasDirtyModels()) { |
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
return parent::isDirty(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
protected function validateAdd(AbstractModel $model) |
80
|
|
|
{ |
81
|
|
|
$this->validateModelClass($model); |
82
|
|
|
$this->store->validateEmbedSet($this->getMetadata(), $model->getName()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Validates that the model class instance is supported. |
87
|
|
|
* |
88
|
|
|
* @param AbstractModel $model |
89
|
|
|
* @throws \InvalidArgumentException |
90
|
|
|
*/ |
91
|
|
|
protected function validateModelClass(AbstractModel $model) |
92
|
|
|
{ |
93
|
|
|
if (!$model instanceof Embed) { |
94
|
|
|
throw new \InvalidArgumentExcepton('The model must be an instanceof of Embed'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|