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, count($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 unique hash for this collection. |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function getHash() |
53
|
|
|
{ |
54
|
|
|
$hash = []; |
55
|
|
|
foreach ($this as $embed) { |
56
|
|
|
$hash[] = $embed->getHash(); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
sort($hash); |
59
|
|
|
return md5(serialize($hash)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Gets the metadata for the model collection. |
64
|
|
|
* |
65
|
|
|
* @return EmbedMetadata |
66
|
|
|
*/ |
67
|
|
|
public function getMetadata() |
68
|
|
|
{ |
69
|
|
|
return $this->metadata; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function getType() |
76
|
|
|
{ |
77
|
|
|
return $this->getMetadata()->name; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function isDirty() |
84
|
|
|
{ |
85
|
|
|
if (true === $this->hasDirtyModels()) { |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
return parent::isDirty(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
protected function validateAdd(AbstractModel $model) |
95
|
|
|
{ |
96
|
|
|
$this->validateModelClass($model); |
97
|
|
|
$this->store->validateEmbedSet($this->getMetadata(), $model->getName()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Validates that the model class instance is supported. |
102
|
|
|
* |
103
|
|
|
* @param AbstractModel $model |
104
|
|
|
* @throws \InvalidArgumentException |
105
|
|
|
*/ |
106
|
|
|
protected function validateModelClass(AbstractModel $model) |
107
|
|
|
{ |
108
|
|
|
if (!$model instanceof Embed) { |
109
|
|
|
throw new \InvalidArgumentExcepton('The model must be an instanceof of Embed'); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: