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 | * @param int $totalCount |
||
29 | */ |
||
30 | public function __construct(EntityMetadata $metadata, Store $store, array $models = [], $totalCount) |
||
35 | |||
36 | /** |
||
37 | * Returns all models in this collection without triggering auto-loading. |
||
38 | * |
||
39 | * @return AbstractModel[] |
||
40 | */ |
||
41 | public function allWithoutLoad() |
||
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() |
||
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() |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function getType() |
||
90 | |||
91 | /** |
||
92 | * {@inheritDoc} |
||
93 | */ |
||
94 | public function rewind() |
||
99 | |||
100 | /** |
||
101 | * Loads this collection from the store. |
||
102 | */ |
||
103 | protected function loadFromStore() |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | protected function validateAdd(AbstractModel $model) |
||
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) |
||
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: