|
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\RepositoryInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class EntityBasedMethods |
|
19
|
|
|
* |
|
20
|
|
|
* @package Slick\Mvc\Controller |
|
21
|
|
|
* @author Filipe Silva <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
trait EntityBasedMethods |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* For basic entity metadata |
|
28
|
|
|
*/ |
|
29
|
|
|
use CrudCommonMethods; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var RepositoryInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $repository; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Gets entity with provided primary key |
|
38
|
|
|
* |
|
39
|
|
|
* @param mixed $entityId |
|
40
|
|
|
* |
|
41
|
|
|
* @return EntityInterface |
|
42
|
|
|
* |
|
43
|
|
|
* @throws EntityNotFoundException If no entity was found with |
|
44
|
|
|
* provided primary key |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function getEntity($entityId) |
|
47
|
|
|
{ |
|
48
|
|
|
$entity = $this->getRepository()->get($entityId); |
|
49
|
|
|
if (!$entity instanceof EntityInterface) { |
|
50
|
|
|
throw new EntityNotFoundException( |
|
51
|
|
|
"There are no entities with provided entity ID." |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
return $entity; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get entity repository |
|
59
|
|
|
* |
|
60
|
|
|
* @return RepositoryInterface |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function getRepository() |
|
63
|
|
|
{ |
|
64
|
2 |
|
if (null == $this->repository) { |
|
65
|
2 |
|
$this->setRepository( |
|
66
|
2 |
|
Orm::getRepository($this->getEntityClassName()) |
|
67
|
2 |
|
); |
|
68
|
2 |
|
} |
|
69
|
2 |
|
return $this->repository; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Set entity repository |
|
74
|
|
|
* |
|
75
|
|
|
* @param RepositoryInterface $repository |
|
76
|
|
|
* |
|
77
|
|
|
* @return self|$this|EntityBasedMethods |
|
|
|
|
|
|
78
|
|
|
*/ |
|
79
|
2 |
|
public function setRepository(RepositoryInterface $repository) |
|
80
|
|
|
{ |
|
81
|
2 |
|
$this->repository = $repository; |
|
82
|
2 |
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get the current entity descriptor |
|
87
|
|
|
* |
|
88
|
|
|
* @return \Slick\Orm\Descriptor\EntityDescriptorInterface |
|
89
|
|
|
*/ |
|
90
|
2 |
|
protected function getEntityDescriptor() |
|
91
|
|
|
{ |
|
92
|
2 |
|
return $this->getRepository() |
|
93
|
2 |
|
->getEntityDescriptor(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Gets the URL base path form this controller |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
abstract protected function getBasePath(); |
|
102
|
|
|
} |
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.