1 | <?php |
||
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() |
|
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) |
|
85 | |||
86 | /** |
||
87 | * Get the current entity descriptor |
||
88 | * |
||
89 | * @return \Slick\Orm\Descriptor\EntityDescriptorInterface |
||
90 | */ |
||
91 | 2 | protected function getEntityDescriptor() |
|
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.