1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace As3\Modlr\Models\Collections; |
4
|
|
|
|
5
|
|
|
use As3\Modlr\Metadata\EntityMetadata; |
6
|
|
|
use As3\Modlr\Models\AbstractModel; |
7
|
|
|
use As3\Modlr\Models\Model; |
8
|
|
|
use As3\Modlr\Store\Store; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Model collection that contains record representations from a persistence (database) layer. |
12
|
|
|
* |
13
|
|
|
* @author Jacob Bare <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
abstract class ModelCollection extends AbstractCollection |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var EntityMetadata |
19
|
|
|
*/ |
20
|
|
|
protected $metadata; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructor. |
24
|
|
|
* |
25
|
|
|
* @param EntityMetadata $metadata |
26
|
|
|
* @param Store $store |
27
|
|
|
* @param AbstractModel[] $models |
28
|
|
|
*/ |
29
|
|
|
public function __construct(EntityMetadata $metadata, Store $store, array $models = []) |
30
|
|
|
{ |
31
|
|
|
$this->metadata = $metadata; |
32
|
|
|
parent::__construct($store, $models); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns all models in this collection without triggering auto-loading. |
37
|
|
|
* |
38
|
|
|
* @return AbstractModel[] |
39
|
|
|
*/ |
40
|
|
|
public function allWithoutLoad() |
41
|
|
|
{ |
42
|
|
|
return $this->models; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Gets the identifiers for this collection. |
47
|
|
|
* |
48
|
|
|
* @param bool $onlyUnloaded Whether to only include unloaded models in the results. |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
abstract public function getIdentifiers($onlyUnloaded = true); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Gets the metadata for the model collection. |
55
|
|
|
* |
56
|
|
|
* @return EntityMetadata |
57
|
|
|
*/ |
58
|
|
|
public function getMetadata() |
59
|
|
|
{ |
60
|
|
|
return $this->metadata; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Gets the query field for this collection. |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
abstract public function getQueryField(); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
* |
73
|
|
|
* Overloaded to ensure models are loaded from the store. |
74
|
|
|
* |
75
|
|
|
*/ |
76
|
|
|
public function getSingleResult() |
77
|
|
|
{ |
78
|
|
|
$this->loadFromStore(); |
79
|
|
|
return parent::getSingleResult(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function getType() |
86
|
|
|
{ |
87
|
|
|
return $this->getMetadata()->type; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritDoc} |
92
|
|
|
*/ |
93
|
|
|
public function rewind() |
94
|
|
|
{ |
95
|
|
|
$this->loadFromStore(); |
96
|
|
|
parent::rewind(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Loads this collection from the store. |
101
|
|
|
*/ |
102
|
|
|
protected function loadFromStore() |
103
|
|
|
{ |
104
|
|
|
if (false === $this->isLoaded()) { |
105
|
|
|
// Loads collection from the database on iteration. |
106
|
|
|
$models = $this->store->loadCollection($this); |
107
|
|
|
$this->setModels($models); |
108
|
|
|
$this->loaded = true; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
protected function modelsMatch(AbstractModel $model, AbstractModel $loaded) |
116
|
|
|
{ |
117
|
|
|
$this->validateModelClass($model); |
118
|
|
|
$this->validateModelClass($loaded); |
119
|
|
|
return $model->getType() === $loaded->getType() && $model->getId() === $loaded->getId(); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
protected function validateAdd(AbstractModel $model) |
126
|
|
|
{ |
127
|
|
|
$this->validateModelClass($model); |
128
|
|
|
$this->store->validateRelationshipSet($this->getMetadata(), $model->getType()); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Validates that the model class instance is supported. |
133
|
|
|
* |
134
|
|
|
* @param AbstractModel $model |
135
|
|
|
* @throws \InvalidArgumentException |
136
|
|
|
*/ |
137
|
|
|
protected function validateModelClass(AbstractModel $model) |
138
|
|
|
{ |
139
|
|
|
if (!$model instanceof Model) { |
140
|
|
|
throw new \InvalidArgumentException('The model must be an instanceof of Model'); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
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: