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
|
|
|
* @param int $totalCount |
29
|
|
|
*/ |
30
|
|
|
public function __construct(EntityMetadata $metadata, Store $store, array $models = [], $totalCount) |
31
|
|
|
{ |
32
|
|
|
$this->metadata = $metadata; |
33
|
|
|
parent::__construct($store, $models, $totalCount); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns all models in this collection without triggering auto-loading. |
38
|
|
|
* |
39
|
|
|
* @return AbstractModel[] |
40
|
|
|
*/ |
41
|
|
|
public function allWithoutLoad() |
42
|
|
|
{ |
43
|
|
|
return $this->models; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Gets the identifiers for this collection. |
48
|
|
|
* |
49
|
|
|
* @param bool $onlyUnloaded Whether to only include unloaded models in the results. |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
abstract public function getIdentifiers($onlyUnloaded = true); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Gets the metadata for the model collection. |
56
|
|
|
* |
57
|
|
|
* @return EntityMetadata |
58
|
|
|
*/ |
59
|
|
|
public function getMetadata() |
60
|
|
|
{ |
61
|
|
|
return $this->metadata; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Gets the query field for this collection. |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
abstract public function getQueryField(); |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
* |
74
|
|
|
* Overloaded to ensure models are loaded from the store. |
75
|
|
|
* |
76
|
|
|
*/ |
77
|
|
|
public function getSingleResult() |
78
|
|
|
{ |
79
|
|
|
$this->loadFromStore(); |
80
|
|
|
return parent::getSingleResult(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function getType() |
87
|
|
|
{ |
88
|
|
|
return $this->getMetadata()->type; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritDoc} |
93
|
|
|
*/ |
94
|
|
|
public function rewind() |
95
|
|
|
{ |
96
|
|
|
$this->loadFromStore(); |
97
|
|
|
parent::rewind(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Loads this collection from the store. |
102
|
|
|
*/ |
103
|
|
|
protected function loadFromStore() |
104
|
|
|
{ |
105
|
|
|
if (false === $this->isLoaded()) { |
106
|
|
|
// Loads collection from the database on iteration. |
107
|
|
|
$models = $this->store->loadCollection($this); |
108
|
|
|
$this->setModels($models); |
109
|
|
|
$this->loaded = true; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
protected function validateAdd(AbstractModel $model) |
117
|
|
|
{ |
118
|
|
|
$this->validateModelClass($model); |
119
|
|
|
$this->store->validateRelationshipSet($this->getMetadata(), $model->getType()); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Validates that the model class instance is supported. |
124
|
|
|
* |
125
|
|
|
* @param AbstractModel $model |
126
|
|
|
* @throws \InvalidArgumentException |
127
|
|
|
*/ |
128
|
|
|
protected function validateModelClass(AbstractModel $model) |
129
|
|
|
{ |
130
|
|
|
if (!$model instanceof Model) { |
131
|
|
|
throw new \InvalidArgumentException('The model must be an instanceof of Model'); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
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: