Complex classes like ContentController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ContentController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class ContentController implements \Anax\DI\IInjectionAware |
||
18 | { |
||
19 | use \Anax\DI\TInjectable; |
||
20 | |||
21 | /** |
||
22 | * Properties |
||
23 | */ |
||
24 | private $content = null; |
||
25 | private $limitListPerPage = null; // Limit contents on list page |
||
26 | private $minimumLength = 3; // Minimum length on text-fields (ex. ingress, title etc) |
||
27 | private $types = [ // Content types |
||
28 | 'blog-post' => ['url' => 'blog/read/', 'field' => 'slug', 'perfix' => '', 'title' => 'Blog'], |
||
29 | 'page' => ['url' => 'page/page/', 'field' => 'url', 'perfix' => '', 'title' => 'Page'] |
||
30 | ]; |
||
31 | private $filters = array('bbcode','clickable','markdown', 'nl2br', 'shortcode'); |
||
32 | private $urlPrefix = "content.php/"; |
||
33 | |||
34 | /** |
||
35 | * Construct the controller |
||
36 | * |
||
37 | * @Return Void |
||
38 | */ |
||
39 | public function initialize(){ |
||
40 | $this->content = new \Chp\TextContent\Content(); |
||
41 | $this->content->setDI($this->di); |
||
1 ignored issue
–
show
|
|||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Index content - use listAction |
||
46 | * |
||
47 | * @Returns Void |
||
48 | */ |
||
49 | public function indexAction(){ |
||
52 | |||
53 | /** |
||
54 | * Setup content (If it allready exist it will restore database to begining) |
||
55 | * |
||
56 | * @Return Void |
||
57 | */ |
||
58 | public function setupAction(){ |
||
59 | $toDo = "Restore or setup"; |
||
60 | $toWhat = "content database tables"; |
||
61 | $title = "{$toDo} {$toWhat}"; |
||
62 | $form = $this->confirmForm($this->url->create($this->urlPrefix . 'content/')); |
||
1 ignored issue
–
show
|
|||
63 | $status = $form->check(); |
||
64 | |||
65 | if($status === true){ |
||
66 | |||
67 | $this->db->dropTableIfExists('content')->execute(); |
||
68 | |||
69 | $this->db->createTable( |
||
1 ignored issue
–
show
|
|||
70 | 'content', |
||
71 | [ |
||
72 | 'id' => ['integer', 'primary key', 'not null', 'auto_increment'], |
||
73 | 'slug' => ['char(80)'], |
||
74 | 'url' => ['char(80)'], |
||
75 | 'type' => ['char(80)'], |
||
76 | 'title' => ['varchar(80)'], |
||
77 | 'ingress' => ['text'], |
||
78 | 'text' => ['text'], |
||
79 | 'filters' => ['char(80)'], |
||
80 | 'author' => ['integer', 'not null'], |
||
81 | 'published' => ['datetime'], |
||
82 | 'created' => ['datetime'], |
||
83 | 'updated' => ['datetime'], |
||
84 | 'deleted' => ['datetime'] |
||
85 | ] |
||
86 | )->execute(); |
||
87 | |||
88 | $this->db->insert( |
||
1 ignored issue
–
show
|
|||
89 | 'content', |
||
90 | ['slug', 'url', 'type', 'title', 'ingress', 'text', 'filters', 'author', 'published', 'created'] |
||
91 | ); |
||
92 | |||
93 | $now = date('Y-m-d H:i:s'); |
||
94 | |||
95 | $this->db->execute([ |
||
1 ignored issue
–
show
|
|||
96 | 'welcome_to_your_new_blog', |
||
97 | NULL, |
||
98 | 'blog-post', |
||
99 | 'Welcome to your new blog', |
||
100 | 'This is example-blogg to show your new blog system!', |
||
101 | 'You can start blog by visit [url=' . $this->url->create($this->urlPrefix . "content/add/") . ']Add content[/url]', |
||
102 | 'bbcode', |
||
103 | 3, |
||
104 | $now, |
||
105 | $now |
||
106 | ]); |
||
107 | |||
108 | $this->db->execute([ |
||
109 | 'welcome_to_your_new_page', |
||
110 | 'example-page', |
||
111 | 'page', |
||
112 | 'Welcome to your new page', |
||
113 | 'This is example-page to show your new page system!', |
||
114 | 'You can start makeing pages by visit [url=' . $this->url->create($this->urlPrefix . "content/add/") . ']Add content[/url]', |
||
1 ignored issue
–
show
|
|||
115 | 'bbcode', |
||
116 | 3, |
||
117 | $now, |
||
118 | $now |
||
119 | ]); |
||
120 | |||
121 | $this->db->dropTableIfExists('content_tags')->execute(); |
||
1 ignored issue
–
show
|
|||
122 | |||
123 | $this->db->createTable( |
||
1 ignored issue
–
show
|
|||
124 | 'content_tags', |
||
125 | [ |
||
126 | 'idContent' => ['integer', 'not null'], |
||
127 | 'tag' => ['varchar(150)', 'not null'], |
||
128 | 'slug' => ['varchar(150)', 'not null'] |
||
129 | ] |
||
130 | )->execute(); |
||
131 | |||
132 | $this->db->insert( |
||
133 | 'content_tags', |
||
134 | ['idContent', 'tag', 'slug'] |
||
135 | ); |
||
136 | |||
137 | $this->db->execute([ |
||
138 | 1, |
||
139 | 'New blog', |
||
140 | 'new_blog' |
||
141 | ]); |
||
142 | |||
143 | $this->db->execute([ |
||
144 | 1, |
||
145 | 'Blog information', |
||
146 | 'blog_information' |
||
147 | ]); |
||
148 | |||
149 | $this->views->add('text-content/action-finish', [ |
||
150 | 'title' => $title, |
||
151 | 'msg' => "Content and content tags database tables " . strtolower($toDo). " was successful!" |
||
152 | ], 'main'); |
||
153 | |||
154 | } |
||
155 | else{ |
||
156 | $this->views->add('text-content/action', [ |
||
157 | 'title' => $title, |
||
158 | 'toDo' => strtolower($toDo), |
||
159 | 'toWhat' => $toWhat, |
||
160 | 'form' => $form->getHTML(['novalidate' => true]) |
||
161 | ], 'main'); |
||
162 | } |
||
163 | |||
164 | $this->theme->setTitle($title); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * List content (by type) |
||
169 | * |
||
170 | * @Param String $type Type to list |
||
171 | * @Param Integer $page Page that paging is on |
||
172 | * @Return Void |
||
173 | */ |
||
174 | public function listAction($type = null, $published = false, $page = null){ |
||
175 | $type = ($this->checkType($type)) ? $type : null; |
||
176 | $title = "All Content {$type} listed"; |
||
177 | $published = boolval($published); |
||
178 | |||
179 | $form = $this->listForm($type, $published); |
||
180 | $form->check(); |
||
181 | |||
182 | if(!is_null($type)){ |
||
183 | $contents = $this->content->getAllContentOfType($type, $page, $this->limitListPerPage, $published); |
||
184 | } |
||
185 | else{ |
||
186 | $contents = $this->content->getAllContent($page, $this->limitListPerPage, $published); |
||
187 | } |
||
188 | |||
189 | foreach($contents AS $key => $content){ |
||
190 | $available = $this->checkIfAvailable($content->published); |
||
191 | |||
192 | $contents[$key]->typeTxt = $this->getTypeTitle($content->type); |
||
193 | $contents[$key]->title = htmlentities($content->title, null, 'UTF-8'); |
||
194 | $contents[$key]->editUrl = $this->url->create($this->urlPrefix . "content/edit/{$content->id}"); |
||
195 | $contents[$key]->removeUrl = $this->url->create($this->urlPrefix . "content/remove/{$content->id}"); |
||
196 | $contents[$key]->showUrl = $this->getUrlToContent($content); |
||
197 | $contents[$key]->available = ((!$available) ? "not-" : null) . "published"; |
||
198 | $contents[$key]->publishedTxt = ($available) ? $contents[$key]->published : "Not published yet"; |
||
199 | } |
||
200 | |||
201 | $this->theme->setTitle($title); |
||
202 | $this->views->add('text-content/list', |
||
203 | [ |
||
204 | 'title' => $title, |
||
205 | 'form' => $form->getHTML(array('novalidate' => true)), |
||
206 | 'contents' => $contents, |
||
207 | 'addUrl' => $this->url->create($this->urlPrefix . 'content/add/'), |
||
208 | 'setupUrl' => $this->url->create($this->urlPrefix . 'content/setup/') |
||
209 | ] |
||
210 | ); |
||
211 | |||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Add content to database |
||
216 | * |
||
217 | * @Return Void |
||
218 | */ |
||
219 | public function addAction(){ |
||
220 | $action = "Add"; |
||
221 | $title = "{$action} content"; |
||
222 | |||
223 | $form = $this->contentForm($action); |
||
224 | $form->check(); |
||
225 | |||
226 | $this->theme->setTitle($title); |
||
227 | $this->views->add('text-content/post', |
||
228 | [ |
||
229 | 'title' => $title, |
||
230 | 'action' => $action, |
||
231 | 'form' => $form->getHTML(array('novalidate' => true)) |
||
232 | ]); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Edit content in database |
||
237 | * |
||
238 | * @Param Integer $id Index for content to edit |
||
239 | * @Return Void |
||
240 | */ |
||
241 | public function editAction($id = null){ |
||
242 | $action = "Edit"; |
||
243 | $title = "{$action} content"; |
||
244 | $url = $this->url->create($this->urlPrefix . 'content/'); |
||
245 | |||
246 | if(is_null($id) || !is_numeric($id)) |
||
247 | $this->response->redirect($url); |
||
248 | |||
249 | $content = $this->content->getContentById($id, false); |
||
250 | |||
251 | if(is_null($content->id)) |
||
252 | $this->response->redirect($url); |
||
253 | |||
254 | $form = $this->contentForm(strtolower($action), $content); |
||
255 | $form->check(); |
||
256 | |||
257 | $this->theme->setTitle($title); |
||
258 | |||
259 | $this->views->add('text-content/post', |
||
260 | [ |
||
261 | 'title' => $title, |
||
262 | 'action' => $action, |
||
263 | 'form' => $form->getHTML(array('novalidate' => true)) |
||
264 | ] |
||
265 | ); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Remove content |
||
270 | * |
||
271 | * @Param Integer $id Index to content to remove |
||
272 | * @Return String $error Database-error msg |
||
273 | */ |
||
274 | public function removeAction($id = null){ |
||
275 | $action = "Delete"; |
||
276 | $title = "Delete content"; |
||
277 | $url = $this->url->create($this->urlPrefix . "content/"); |
||
278 | |||
279 | if(is_null($id) || !is_numeric($id)){ |
||
280 | $this->response->redirect($url); |
||
281 | } |
||
282 | |||
283 | $content = $this->content->find($id); |
||
284 | |||
285 | if(is_null($content->id)){ |
||
286 | $this->response->redirect($url); |
||
287 | } |
||
288 | |||
289 | $form = $this->confirmForm($url); |
||
290 | $status = $form->Check(); |
||
291 | |||
292 | if ($status === true) { |
||
293 | $now = date('Y-m-d H:i:s'); |
||
294 | |||
295 | $content->deleted = $now; |
||
296 | $content->save(); |
||
297 | |||
298 | $this->response->redirect($url); |
||
299 | } |
||
300 | |||
301 | $this->theme->setTitle($title); |
||
302 | $this->views->add('text-content/action', [ |
||
303 | 'title' => $title, |
||
304 | 'toDo' => strtolower($action), |
||
305 | 'toWhat' => strtolower($this->getTypeTitle($content->type)), |
||
306 | 'which' => htmlentities($content->title), |
||
307 | 'form' => $form->getHTML(['novalidate' => true]), |
||
308 | ], 'main'); |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Prepare form to add or edit content |
||
313 | * |
||
314 | * @Param String $type Selected content-type |
||
315 | * @Param Boolean $published If content should be published already |
||
316 | * @Return Object $form CForm object |
||
317 | */ |
||
318 | private function listForm($type = null, $published = false){ |
||
319 | |||
320 | $type_options = array_merge([0 => "Select a type of content"], $this->getTypes()); |
||
321 | |||
322 | $form = new \Mos\HTMLForm\CForm([], [ |
||
323 | 'type' => [ |
||
324 | 'type' => 'select', |
||
325 | 'label' => 'Type of content:', |
||
326 | 'options' => $type_options, |
||
327 | 'value' => (isset($type)) ? $type : '' |
||
328 | ], |
||
329 | 'published' => [ |
||
330 | 'type' => 'checkbox', |
||
331 | 'label' => 'Published', |
||
332 | 'checked' => $published, |
||
333 | 'value' => 1 |
||
334 | ], |
||
335 | 'submit' => [ |
||
336 | 'value' => 'Filter', |
||
337 | 'type' => 'submit', |
||
338 | 'callback' => function ($form) { |
||
339 | $published = ($this->request->getPost('published')) ? 1 : 0; |
||
340 | $type = $form->Value('type'); |
||
341 | |||
342 | $this->response->redirect($this->url->create($this->urlPrefix . "content/list/{$type}/{$published}")); |
||
343 | } |
||
344 | ] |
||
345 | ]); |
||
346 | |||
347 | return $form; |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Prepare form to add or edit content |
||
352 | * |
||
353 | * @Param String $action What to do (add or edit) |
||
354 | * @Param Object $values Content values to add form elements |
||
355 | * @Return Object $form CForm object |
||
356 | */ |
||
357 | private function contentForm($action, $values = null){ |
||
358 | if(isset($values) && is_object($values)){ |
||
359 | $valArr = get_object_vars($values); |
||
360 | extract($valArr); |
||
361 | } |
||
362 | |||
363 | $slug = (isset($slug)) ? $slug : NULL; |
||
364 | |||
365 | $type_options = $this->getTypes(); |
||
366 | |||
367 | $form = new \Mos\HTMLForm\CForm([], [ |
||
368 | 'id' => [ |
||
369 | 'type' => 'hidden', |
||
370 | 'value' => (isset($id)) ? $id : 0 |
||
371 | ], |
||
372 | 'title' => [ |
||
373 | 'type' => 'text', |
||
374 | 'label' => 'Title: (Between ' . $this->minimumLength . ' to 80 chars)', |
||
375 | 'maxlength' => 80, |
||
376 | 'required' => true, |
||
377 | 'value' => (isset($title)) ? $title : '', |
||
378 | 'validation' => [ |
||
379 | 'custom_test' => array( |
||
380 | 'message' => 'Minmum length is ' . $this->minimumLength . ' chars.', |
||
381 | 'test' => array($this, 'minimumLength') |
||
382 | ) |
||
383 | ] |
||
384 | ], |
||
385 | 'url' => [ |
||
386 | 'type' => 'text', |
||
387 | 'label' => 'Url:', |
||
388 | 'maxlength' => 80, |
||
389 | 'value' => (isset($url)) ? $url : '', |
||
390 | 'required' => false, |
||
391 | 'validation' => [ |
||
392 | 'custom_test' => array( |
||
393 | 'message' => 'Url is not in accepted-format for url:s (accepted characters is \'a-z0-9-_()\').', |
||
394 | 'test' => array($this, 'validateSlug') |
||
395 | ) |
||
396 | ] |
||
397 | ], |
||
398 | 'ingress' => [ |
||
399 | 'type' => 'textarea', |
||
400 | 'label' => 'Ingress: (minimum 3 chars)', |
||
401 | 'value' => (isset($ingress)) ? $ingress : '', |
||
402 | 'required' => true, |
||
403 | 'validation' => [ |
||
404 | 'custom_test' => array( |
||
405 | 'message' => 'Minmum length is ' . $this->minimumLength . ' chars.', |
||
406 | 'test' => array($this, 'minimumLength') |
||
407 | ) |
||
408 | ] |
||
409 | ], |
||
410 | 'text' => [ |
||
411 | 'type' => 'textarea', |
||
412 | 'label' => 'Text: (minimum 3 chars)', |
||
413 | 'value' => (isset($text)) ? $text : '', |
||
414 | 'required' => true, |
||
415 | 'validation' => [ |
||
416 | 'custom_test' => array( |
||
417 | 'message' => 'Minmum length is ' . $this->minimumLength . ' chars.', |
||
418 | 'test' => array($this, 'minimumLength') |
||
419 | ) |
||
420 | ] |
||
421 | ], |
||
422 | 'type' => [ |
||
423 | 'type' => 'select', |
||
424 | 'label' => 'Type of content:', |
||
425 | 'options' => $type_options, |
||
426 | 'value' => (isset($type)) ? $type : '', |
||
427 | 'required' => true, |
||
428 | 'validation' => [ |
||
429 | 'not_empty', |
||
430 | 'custom_test' => array( |
||
431 | 'message' => 'You need to select a existing type.', |
||
432 | 'test' => array($this, 'checkType') |
||
433 | ) |
||
434 | ] |
||
435 | ], |
||
436 | 'tags' => [ |
||
437 | 'type' => 'text', |
||
438 | 'label' => 'Tags: (Seperate by \',\')', |
||
439 | 'value' => (isset($tags)) ? $tags : '', |
||
440 | 'required' => false |
||
441 | ], |
||
442 | 'filters' => [ |
||
443 | 'type' => 'checkbox-multiple', |
||
444 | 'label' => 'Text filter:', |
||
445 | 'values' => $this->filters, |
||
446 | 'checked' => (isset($filters)) ? explode(',', $filters) : array(), |
||
447 | 'validation' => [ |
||
448 | 'custom_test' => array( |
||
449 | 'message' => 'You need to select a existing type.', |
||
450 | 'test' => array($this, 'checkFilter') |
||
451 | ) |
||
452 | ] |
||
453 | ], |
||
454 | 'published' => [ |
||
455 | 'type' => 'datetime', |
||
456 | 'label' => 'Published: (YYYY-MM-DD HH:MM:SS)', |
||
457 | 'value' => (isset($published)) ? $published : '', |
||
458 | 'validation' => [ |
||
459 | 'custom_test' => array( |
||
460 | 'message' => 'It need to be in a correct date and time with format: YYYY-MM-DD HH:MM:SS.', |
||
461 | 'test' => array($this, 'checkDatetime') |
||
462 | ) |
||
463 | ] |
||
464 | ], |
||
465 | 'publishedNow' => [ |
||
466 | 'type' => 'checkbox', |
||
467 | 'label' => 'Publish now:', |
||
468 | 'checked' => (!isset($published) && !isset($id)), |
||
469 | 'value' => 'yes' |
||
470 | ], |
||
471 | 'submit' => [ |
||
472 | 'value' => 'Save', |
||
473 | 'type' => 'submit', |
||
474 | 'callback' => function ($form) use($slug, $action) { |
||
475 | $now = date('Y-m-d H:i:s'); |
||
476 | |||
477 | $newSlug = $this->slugify($form->Value('title')); |
||
478 | |||
479 | if($slug != $newSlug && isset($newSlug)){ |
||
480 | $newSlug = $this->content->makeSlugToContent($newSlug, $form->Value('type')); |
||
481 | } |
||
482 | |||
483 | $content = array( |
||
484 | 'title' => $form->Value('title'), |
||
485 | 'slug' => $newSlug, |
||
486 | 'url' => $form->Value('url'), |
||
487 | 'ingress' => $form->Value('ingress'), |
||
488 | 'text' => $form->Value('text'), |
||
489 | 'type' => $form->Value('type'), |
||
490 | 'filters' => ($form->Value('filters')) ? implode(",", $form->Value('filters')) : '', |
||
491 | 'published' => ($this->request->getPost('publishedNow') && $this->request->getPost('publishedNow') == 'yes') ? $now : $form->Value('published') |
||
492 | ); |
||
493 | |||
494 | $id = ($form->Value('id')) ? intval($form->Value('id')) : 0; |
||
495 | |||
496 | if($id != 0){ |
||
497 | $content['updated'] = $now; |
||
498 | $content['id'] = $id; |
||
499 | } |
||
500 | else{ |
||
501 | $content['created'] = $now; |
||
502 | $content['author'] = 0; |
||
503 | } |
||
504 | |||
505 | $this->content->save($content); |
||
506 | |||
507 | if(!$this->content->id) { |
||
508 | return false; |
||
509 | } |
||
510 | else if($id != 0){ |
||
511 | $this->db->delete( |
||
512 | 'content_tags', |
||
513 | 'idContent = ?' |
||
514 | ); |
||
515 | $this->db->execute([$this->content->id]); |
||
516 | |||
517 | $this->db->insert( |
||
518 | 'content_tags', |
||
519 | ['idContent', 'tag', 'slug'] |
||
520 | ); |
||
521 | |||
522 | if($form->Value('tags')){ |
||
523 | $tags = explode(",", $form->Value('tags')); |
||
524 | |||
525 | foreach($tags as $tag){ |
||
526 | $tag = trim($tag); |
||
527 | $this->db->execute([ |
||
528 | $id, |
||
529 | $tag, |
||
530 | $this->slugify($tag) |
||
531 | ]); |
||
532 | } |
||
533 | } |
||
534 | } |
||
535 | |||
536 | $this->response->redirect($this->url->create($this->urlPrefix . "content/")); |
||
537 | } |
||
538 | ] |
||
539 | ]); |
||
540 | |||
541 | return $form; |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * Prepare confirmation form |
||
546 | * |
||
547 | * @Param String $returnUrl Return url |
||
548 | * @Return Object $form Form-object |
||
549 | */ |
||
550 | public function confirmForm($returnUrl = null){ |
||
551 | $returnUrl = (isset($returnUrl)) ? $returnUrl : $this->request->getBaseUrl(); |
||
552 | |||
553 | $form = new \Mos\HTMLForm\CForm([], [ |
||
554 | 'submit' => [ |
||
555 | 'type' => 'submit', |
||
556 | 'value' => 'Yes', |
||
557 | 'callback' => function() { |
||
558 | return true; |
||
559 | } |
||
560 | ], |
||
561 | 'submit-no' => [ |
||
562 | 'type' => "submit", |
||
563 | 'value' => 'No', |
||
564 | 'callback' => function() use($returnUrl) { |
||
565 | $this->response->redirect($returnUrl); |
||
566 | } |
||
567 | ] |
||
568 | ] |
||
569 | ); |
||
570 | |||
571 | // Check the status of the form |
||
572 | return $form; |
||
573 | } |
||
574 | |||
575 | /** |
||
576 | * Check if content is published |
||
577 | * |
||
578 | * @Param String $datetime When it will be published |
||
579 | * @Return Boolean True/false Validate result |
||
580 | */ |
||
581 | 1 | public function checkIfAvailable($datetime){ |
|
582 | 1 | return ($datetime <= date('Y-m-d H:i:s')) ? true : false; |
|
583 | } |
||
584 | |||
585 | /** |
||
586 | * Create a link to the content, based on its type. |
||
587 | * |
||
588 | * @Param Object $content Content to link to |
||
589 | * @Return String With url for content |
||
590 | */ |
||
591 | 1 | public function getUrlToContent($content) { |
|
592 | 1 | if(isset($this->types[$content->type])){ |
|
593 | 1 | $type = $this->types[$content->type]; // Get type from type index |
|
594 | |||
595 | 1 | return $this->url->create($this->urlPrefix . "{$type['url']}{$type['perfix']}{$content->{$type['field']}}"); |
|
596 | } |
||
597 | |||
598 | 1 | return null; |
|
599 | } |
||
600 | |||
601 | /** |
||
602 | * Return array with all content types title and keys (Use for content-type select) |
||
603 | * |
||
604 | * @Return Array $types Array with the types title and keys |
||
605 | */ |
||
606 | 1 | public function getTypes(){ |
|
607 | 1 | $types = array(); |
|
608 | |||
609 | // Loop through and save types key as key and title as value in a new array |
||
610 | 1 | foreach($this->types AS $key => $value){ |
|
611 | 1 | $types[$key] = $value['title']; |
|
612 | 1 | } |
|
613 | |||
614 | 1 | return $types; |
|
615 | } |
||
616 | |||
617 | /** |
||
618 | * Return name of one specific type |
||
619 | * |
||
620 | * @Params String $type Type key |
||
621 | * @Return String Type title |
||
622 | */ |
||
623 | public function getTypeTitle($type){ |
||
624 | return $this->types[$type]['title']; |
||
625 | } |
||
626 | |||
627 | /** |
||
628 | * Create a slug of a string, to be used as url. |
||
629 | * |
||
630 | * @Param String $str String to format as slug. |
||
631 | * @Return String $str Formatted slug. |
||
632 | */ |
||
633 | 1 | public function slugify($str) { |
|
634 | 1 | $str = mb_strtolower(trim($str)); |
|
635 | |||
636 | 1 | $str = str_replace(array("å","ä","ö"), array("a","a","o"), utf8_decode(utf8_encode($str))); |
|
637 | |||
638 | 1 | $str = preg_replace('/[^a-z0-9-_()]/', '_', $str); |
|
639 | 1 | $str = trim(preg_replace('/_+/', '_', $str), '_'); |
|
640 | 1 | return $str; |
|
641 | } |
||
642 | |||
643 | /** |
||
644 | * Check so the choosed type exist. |
||
645 | * |
||
646 | * @Param String $type Choosed type on content |
||
647 | * @Returns Boolean Validate result |
||
648 | */ |
||
649 | 1 | public function checkType($type){ |
|
650 | 1 | return isset($this->types[$type]); |
|
651 | } |
||
652 | |||
653 | /** |
||
654 | * Validate posted datetime so it is correct |
||
655 | * |
||
656 | * @Param String $datetime Posted datetime to check |
||
657 | * @Return Boolean True/false Validate status |
||
658 | */ |
||
659 | public function checkDatetime($datetime){ |
||
660 | if($datetime){ |
||
661 | $format = 'Y-m-d H:i:s'; |
||
662 | $d = \DateTime::createFromFormat($format, $datetime); |
||
663 | return $d && $d->format($format) == $datetime; |
||
664 | } |
||
665 | return true; |
||
666 | } |
||
667 | |||
668 | /** |
||
669 | * Minimum length (set by $this->minimumLength) |
||
670 | * |
||
671 | * @Param String $value Value from form-element to validate |
||
672 | * @Return Boolean True/false Validate result |
||
673 | */ |
||
674 | 1 | public function minimumLength($value){ |
|
677 | |||
678 | /** |
||
679 | * Validate slug url |
||
680 | * |
||
681 | * @Param String $url Url to validate |
||
682 | * @Return Boolean True/false True if valid otherwish false |
||
683 | */ |
||
684 | 1 | public function validateSlug($url){ |
|
687 | |||
688 | /** |
||
689 | * Check so the select filters exist. |
||
690 | * |
||
691 | * @Param Array $filters Array with select filters |
||
692 | * @Return Boolean $result Return the result of test |
||
693 | */ |
||
694 | 1 | public function checkFilter($filter){ |
|
695 | 1 | if(isset($filter)){ |
|
696 | // For each filter, check if the filter exist |
||
697 | 1 | foreach($this->filters as $val){ |
|
698 | 1 | if($val == $filter) |
|
699 | 1 | return true; |
|
700 | 1 | } |
|
701 | 1 | return false; |
|
702 | } |
||
703 | return true; |
||
705 | } |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.