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\Mvc\Exception\Service\EntityNotFoundException; |
13
|
|
|
use Slick\Orm\EntityInterface; |
14
|
|
|
use Slick\Orm\Orm; |
15
|
|
|
use Slick\Orm\Repository\EntityRepository; |
16
|
|
|
use Slick\Orm\RepositoryInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class EntityBasedMethods |
20
|
|
|
* |
21
|
|
|
* @package Slick\Mvc\Controller |
22
|
|
|
* @author Filipe Silva <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
trait EntityBasedMethods |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* For basic entity metadata |
29
|
|
|
*/ |
30
|
|
|
use CrudCommonMethods; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var RepositoryInterface |
34
|
|
|
*/ |
35
|
|
|
protected $repository; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Gets entity with provided primary key |
39
|
|
|
* |
40
|
|
|
* @param mixed $entityId |
41
|
|
|
* |
42
|
|
|
* @return EntityInterface |
43
|
|
|
* |
44
|
|
|
* @throws EntityNotFoundException If no entity was found with |
45
|
|
|
* provided primary key |
46
|
|
|
*/ |
47
|
4 |
|
protected function getEntity($entityId) |
48
|
|
|
{ |
49
|
4 |
|
$entity = $this->getRepository()->get($entityId); |
50
|
4 |
|
if (!$entity instanceof EntityInterface) { |
51
|
2 |
|
throw new EntityNotFoundException( |
52
|
1 |
|
"There are no entities with provided entity ID." |
53
|
1 |
|
); |
54
|
|
|
} |
55
|
2 |
|
return $entity; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get entity repository |
60
|
|
|
* |
61
|
|
|
* @return RepositoryInterface|EntityRepository |
62
|
|
|
*/ |
63
|
8 |
|
public function getRepository() |
64
|
|
|
{ |
65
|
8 |
|
if (null == $this->repository) { |
66
|
4 |
|
$this->setRepository( |
67
|
4 |
|
Orm::getRepository($this->getEntityClassName()) |
68
|
2 |
|
); |
69
|
2 |
|
} |
70
|
8 |
|
return $this->repository; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set entity repository |
75
|
|
|
* |
76
|
|
|
* @param RepositoryInterface $repository |
77
|
|
|
* |
78
|
|
|
* @return self|$this|EntityBasedMethods |
|
|
|
|
79
|
|
|
*/ |
80
|
4 |
|
public function setRepository(RepositoryInterface $repository) |
81
|
|
|
{ |
82
|
4 |
|
$this->repository = $repository; |
83
|
4 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the current entity descriptor |
88
|
|
|
* |
89
|
|
|
* @return \Slick\Orm\Descriptor\EntityDescriptorInterface |
90
|
|
|
*/ |
91
|
2 |
|
protected function getEntityDescriptor() |
92
|
|
|
{ |
93
|
2 |
|
return $this->getRepository() |
94
|
2 |
|
->getEntityDescriptor(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Gets the URL base path form this controller |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
abstract protected function getBasePath(); |
103
|
|
|
} |
In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.
If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.