1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/mvc package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Mvc\Controller; |
11
|
|
|
|
12
|
|
|
use Slick\Common\Log; |
13
|
|
|
use Slick\Filter\StaticFilter; |
14
|
|
|
use Slick\Mvc\Exception\Service\EntityNotFoundException; |
15
|
|
|
use Slick\Orm\EntityInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Entity View Methods |
19
|
|
|
* |
20
|
|
|
* @package Slick\Mvc\Controller |
21
|
|
|
* @author Filipe Silva <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
trait EntityViewMethods |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Add a warning flash message |
28
|
|
|
* |
29
|
|
|
* @param string $message |
30
|
|
|
* @return self |
31
|
|
|
*/ |
32
|
|
|
abstract public function addWarningMessage($message); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get missing entity warning message |
36
|
|
|
* |
37
|
|
|
* @param mixed $entityId |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
2 |
|
protected function getMissingEntityMessage($entityId) |
42
|
|
|
{ |
43
|
2 |
|
$singleName = $this->getEntityNameSingular(); |
44
|
2 |
|
return "The {$singleName} with ID {$entityId} was not found."; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Redirect callback after missing entity detection |
49
|
|
|
* |
50
|
|
|
* @return $this|\Slick\Mvc\ControllerInterface|static |
51
|
|
|
*/ |
52
|
2 |
|
protected function redirectFromMissingEntity() |
53
|
|
|
{ |
54
|
2 |
|
return $this->redirect($this->getBasePath()); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Handles the request to view an entity |
59
|
|
|
* |
60
|
|
|
* @param int $entityId |
61
|
|
|
*/ |
62
|
4 |
|
public function show($entityId = 0) |
63
|
|
|
{ |
64
|
4 |
|
$entityId = StaticFilter::filter('text', $entityId); |
65
|
|
|
try { |
66
|
4 |
|
$entity = $this->getEntity($entityId); |
67
|
2 |
|
$this->set($this->getEntityNameSingular(), $entity); |
|
|
|
|
68
|
3 |
|
} catch (EntityNotFoundException $caught) { |
69
|
2 |
|
Log::logger()->addNotice($caught->getMessage()); |
70
|
2 |
|
$this->addWarningMessage( |
71
|
2 |
|
$this->getMissingEntityMessage($entityId) |
72
|
1 |
|
); |
73
|
2 |
|
$this->redirectFromMissingEntity(); |
74
|
|
|
} |
75
|
|
|
|
76
|
4 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Gets entity with provided primary key |
80
|
|
|
* |
81
|
|
|
* @param mixed $entityId |
82
|
|
|
* |
83
|
|
|
* @return EntityInterface |
84
|
|
|
* |
85
|
|
|
* @throws EntityNotFoundException If no entity was found with |
86
|
|
|
* provided primary key |
87
|
|
|
*/ |
88
|
|
|
abstract protected function getEntity($entityId); |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get entity singular name used on controller actions |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
abstract protected function getEntityNameSingular(); |
96
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.