1 | <?php |
||
18 | class Endpoint implements Countable, IteratorAggregate, ArrayAccess, Arrayable, Jsonable |
||
19 | { |
||
20 | use DelegatedArrayTrait; |
||
21 | |||
22 | /** |
||
23 | * @var FieldCollection |
||
24 | */ |
||
25 | protected $fields; |
||
26 | |||
27 | /** |
||
28 | * @var AbstractDataSource |
||
29 | */ |
||
30 | protected $source; |
||
31 | |||
32 | protected $auto = false; |
||
33 | |||
34 | public function __construct($source = []) |
||
35 | { |
||
36 | $this->fields = new FieldCollection(); |
||
37 | $this->initSource($source); |
||
38 | } |
||
39 | |||
40 | public static function auto($source) |
||
41 | { |
||
42 | $endpoint = new static($source); |
||
43 | $endpoint->auto = true; |
||
44 | |||
45 | return $endpoint; |
||
46 | } |
||
47 | |||
48 | protected function initSource($source) |
||
52 | |||
53 | /** |
||
54 | * @param string $name |
||
55 | * @param array $arguments |
||
56 | * |
||
57 | * @return Field|static|mixed |
||
58 | */ |
||
59 | public function __call($name, $arguments) |
||
75 | |||
76 | public function source($newSource = null) |
||
86 | |||
87 | /** |
||
88 | * Collection containing all the fields in the form. |
||
89 | * |
||
90 | * @return FieldCollection |
||
91 | */ |
||
92 | public function fields() |
||
96 | |||
97 | /** |
||
98 | * Get a field by name (dotted offset). |
||
99 | * |
||
100 | * (you can also use array notation like: |
||
101 | * <code>$form['author.name']</code> |
||
102 | * |
||
103 | * @param string $name |
||
104 | * |
||
105 | * @return Field |
||
106 | */ |
||
107 | public function field($name) |
||
111 | |||
112 | /** |
||
113 | * Get the fields value as an associative array. |
||
114 | * By default a nested array is returned. |
||
115 | * Passing true as the first parameter, a flat |
||
116 | * array will be returned, with dotted offsets |
||
117 | * as the keys. |
||
118 | * |
||
119 | * @param bool $flat |
||
120 | * |
||
121 | * @return array |
||
122 | */ |
||
123 | public function toArray($flat = false) |
||
127 | |||
128 | /** |
||
129 | * Required by DelegatedArrayTrait. |
||
130 | * |
||
131 | * @return FieldCollection |
||
132 | */ |
||
133 | protected function getDelegatedStorage() |
||
137 | |||
138 | /** |
||
139 | * Sets the fields values back to the models. |
||
140 | */ |
||
141 | public function writeSource() |
||
147 | |||
148 | /** |
||
149 | * Fills the form with the values coming from the DB |
||
150 | * and HTTP input. |
||
151 | */ |
||
152 | public function populate() |
||
157 | |||
158 | public function fromSource() |
||
162 | |||
163 | public function fromInput() |
||
164 | { |
||
165 | $request = method_exists($request = \Request::instance(), 'all') |
||
166 | ? $request |
||
167 | : \Input::instance(); |
||
168 | |||
169 | return $this->from($request->all()); |
||
170 | } |
||
171 | |||
172 | public function from($source) |
||
180 | |||
181 | /** |
||
182 | * Saves the models back to the database. |
||
183 | */ |
||
184 | public function save() |
||
188 | |||
189 | /** |
||
190 | * Validates the form, then sets eventual errors on each field. |
||
191 | * |
||
192 | * @return mixed |
||
193 | */ |
||
194 | public function validate() |
||
200 | |||
201 | /** |
||
202 | * @return mixed |
||
203 | */ |
||
204 | public function errors() |
||
210 | |||
211 | /** |
||
212 | * Convert the object to its JSON representation. |
||
213 | * |
||
214 | * @param int $options |
||
215 | * |
||
216 | * @return string |
||
217 | */ |
||
218 | public function toJson($options = 0) |
||
229 | |||
230 | protected function getKey() |
||
236 | |||
237 | protected function process() |
||
264 | |||
265 | protected function load() |
||
273 | } |
||
274 |
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: