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