1 | <?php |
||
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 = []) |
||
34 | |||
35 | /** |
||
36 | * Returns all models in this collection without triggering auto-loading. |
||
37 | * |
||
38 | * @return AbstractModel[] |
||
39 | */ |
||
40 | public function allWithoutLoad() |
||
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() |
||
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() |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | public function getType() |
||
89 | |||
90 | /** |
||
91 | * {@inheritDoc} |
||
92 | */ |
||
93 | public function rewind() |
||
98 | |||
99 | /** |
||
100 | * Loads this collection from the store. |
||
101 | */ |
||
102 | protected function loadFromStore() |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | protected function modelsMatch(AbstractModel $model, AbstractModel $loaded) |
||
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | protected function validateAdd(AbstractModel $model) |
||
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) |
||
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: