1 | <?php |
||
26 | trait EntityEditMethods |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * Handle the request to edit an entity |
||
31 | * |
||
32 | * @param mixed $entityId |
||
33 | */ |
||
34 | public function edit($entityId) |
||
71 | |||
72 | /** |
||
73 | * Get the update successful entity message |
||
74 | * |
||
75 | * @param EntityInterface $entity |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | protected function getEditSuccessMessage(EntityInterface $entity) |
||
85 | |||
86 | /** |
||
87 | * Get update service |
||
88 | * |
||
89 | * @return EntityUpdateService |
||
90 | */ |
||
91 | abstract public function getUpdateService(); |
||
92 | |||
93 | /** |
||
94 | * @return FormInterface|EntityForm |
||
95 | */ |
||
96 | abstract function getForm(); |
||
97 | |||
98 | /** |
||
99 | * Get invalid form data message |
||
100 | * |
||
101 | * @param \Exception $caught |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | abstract protected function getGeneralErrorMessage(\Exception $caught); |
||
106 | |||
107 | /** |
||
108 | * Get invalid form data message |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | abstract protected function getInvalidFormDataMessage(); |
||
113 | |||
114 | /** |
||
115 | * Sets a value to be used by render |
||
116 | * |
||
117 | * The key argument can be an associative array with values to be set |
||
118 | * or a string naming the passed value. If an array is given then the |
||
119 | * value will be ignored. |
||
120 | * |
||
121 | * Those values must be set in the request attributes so they can be used |
||
122 | * latter by any other middle ware in the stack. |
||
123 | * |
||
124 | * @param string|array $key |
||
125 | * @param mixed $value |
||
126 | * |
||
127 | * @return ControllerInterface |
||
128 | */ |
||
129 | abstract public function set($key, $value = null); |
||
130 | |||
131 | /** |
||
132 | * Redirects the flow to another route/path |
||
133 | * |
||
134 | * @param string $path the route or path to redirect to |
||
135 | * |
||
136 | * @return ControllerInterface|self|$this |
||
137 | */ |
||
138 | abstract public function redirect($path); |
||
139 | |||
140 | /** |
||
141 | * Add an error flash message |
||
142 | * |
||
143 | * @param string $message |
||
144 | * @return self |
||
145 | */ |
||
146 | abstract public function addErrorMessage($message); |
||
147 | |||
148 | /** |
||
149 | * Add a success flash message |
||
150 | * |
||
151 | * @param string $message |
||
152 | * @return self |
||
153 | */ |
||
154 | abstract public function addSuccessMessage($message); |
||
155 | |||
156 | /** |
||
157 | * Handles the request to view an entity |
||
158 | * |
||
159 | * @param int $entityId |
||
160 | * |
||
161 | * @return null|EntityInterface |
||
162 | */ |
||
163 | abstract public function show($entityId = 0); |
||
164 | |||
165 | /** |
||
166 | * Returns the translation for the provided message |
||
167 | * |
||
168 | * @param string $message |
||
169 | * @param string $domain |
||
170 | * @param string $locale |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | abstract public function translate( |
||
177 | |||
178 | /** |
||
179 | * Get entity singular name used on controller actions |
||
180 | * |
||
181 | * @return string |
||
182 | */ |
||
183 | abstract protected function getEntityNameSingular(); |
||
184 | } |
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: