1 | <?php |
||
18 | abstract class Model |
||
19 | { |
||
20 | protected $mappingClasses = []; |
||
21 | |||
22 | /** |
||
23 | * Contains property name mappings. |
||
24 | * |
||
25 | * [ |
||
26 | * 'data_array_property1' => 'objectProperty1', |
||
27 | * 'data_array_property2' => 'objectProperty2', |
||
28 | * ] |
||
29 | * |
||
30 | * Data array property uses as keys |
||
31 | * because there is can be more then one rule per object property |
||
32 | * |
||
33 | * f.g. $data['nmodels'] and ['modelsnum'] should map in modelsCount property. |
||
34 | * Otherwise not unique array keys cause remapping of properties. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $propNameMap = []; |
||
39 | |||
40 | /** |
||
41 | * Constructor |
||
42 | * |
||
43 | * @param array $data |
||
44 | */ |
||
45 | 98 | public function __construct($data = []) |
|
49 | |||
50 | /** |
||
51 | * Set from array |
||
52 | * |
||
53 | * @param array $data |
||
54 | * @return $this |
||
55 | */ |
||
56 | 96 | public function fromArray($data) |
|
57 | { |
||
58 | 96 | foreach ($data as $key => $val) { |
|
59 | 68 | if (is_int($key)) { |
|
60 | 47 | if (method_exists($this, "add")) { |
|
61 | 47 | $this->add($val); |
|
|
|||
62 | 47 | } |
|
63 | 47 | } |
|
64 | |||
65 | 68 | $propertyName = $key; |
|
66 | 68 | $ourPropertyName = array_search($propertyName, $this->propNameMap); |
|
67 | |||
68 | 68 | if ($ourPropertyName && isset($data[$ourPropertyName])) { |
|
69 | $propertyName = $ourPropertyName; |
||
70 | } |
||
71 | |||
72 | 68 | if (!empty($this->propNameMap)) { |
|
73 | 52 | if (array_key_exists($key, $this->propNameMap)) { |
|
74 | 47 | $propertyName = $this->propNameMap[$key]; |
|
75 | 47 | } |
|
76 | 52 | } |
|
77 | |||
78 | 68 | if (property_exists($this, $propertyName)) { |
|
79 | 65 | if (isset($this->mappingClasses[$propertyName])) { |
|
80 | 55 | $this->{$propertyName} = new $this->mappingClasses[$propertyName]($val); |
|
81 | 55 | } else { |
|
82 | 65 | $this->{$propertyName} = $val; |
|
83 | } |
||
84 | 65 | } |
|
85 | 96 | } |
|
86 | 96 | return $this; |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Set from json |
||
91 | * |
||
92 | * @param string $json |
||
93 | * @return $this |
||
94 | */ |
||
95 | 1 | public function fromJson($json) |
|
96 | { |
||
97 | 1 | $this->fromArray(json_decode($json, true)); |
|
98 | 1 | return $this; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Get array from object |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | 21 | public function toArray() |
|
107 | { |
||
108 | 21 | return $this->toArrayRecursive($this); |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Get array from object |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | 1 | public function toJson() |
|
120 | |||
121 | /** |
||
122 | * Get array from object |
||
123 | * |
||
124 | * @param array|object $data |
||
125 | * @return array |
||
126 | */ |
||
127 | 19 | protected function toArrayRecursive($data) |
|
163 | } |
||
164 |
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: