1 | <?php |
||
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 | 4 | protected function getEntity($entityId) |
|
47 | { |
||
48 | 4 | $entity = $this->getRepository()->get($entityId); |
|
49 | 4 | if (!$entity instanceof EntityInterface) { |
|
50 | 2 | throw new EntityNotFoundException( |
|
51 | 1 | "There are no entities with provided entity ID." |
|
52 | 1 | ); |
|
53 | } |
||
54 | 2 | return $entity; |
|
55 | } |
||
56 | |||
57 | /** |
||
58 | * Get entity repository |
||
59 | * |
||
60 | * @return RepositoryInterface |
||
61 | */ |
||
62 | 6 | public function getRepository() |
|
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) |
|
84 | |||
85 | /** |
||
86 | * Get the current entity descriptor |
||
87 | * |
||
88 | * @return \Slick\Orm\Descriptor\EntityDescriptorInterface |
||
89 | */ |
||
90 | 2 | protected function getEntityDescriptor() |
|
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.