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\Service\Entity; |
11
|
|
|
|
12
|
|
|
use Slick\Mvc\Exception\Service\InvalidEntityClassException; |
13
|
|
|
use Slick\Mvc\Exception\Service\MissingEntityException; |
14
|
|
|
use Slick\Mvc\Service\EntityServiceInterface; |
15
|
|
|
use Slick\Orm\EntityInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Base Entity Service |
19
|
|
|
* |
20
|
|
|
* @package Slick\Mvc\Service\Entity |
21
|
|
|
* @author Filipe Silva <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
abstract class AbstractEntityService implements EntityServiceInterface |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $entityClass; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var EntityInterface |
33
|
|
|
*/ |
34
|
|
|
protected $entity; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set FQ entity class name |
38
|
|
|
* |
39
|
|
|
* @param string $className |
40
|
|
|
* |
41
|
|
|
* @throws InvalidEntityClassException If the provided class name does |
42
|
|
|
* not implements the Slick\Orm\EntityInterface interface. |
43
|
|
|
* |
44
|
|
|
* @return $this|self|AbstractEntityService |
45
|
|
|
*/ |
46
|
14 |
|
public function setEntityClass($className) |
47
|
|
|
{ |
48
|
|
|
if ( |
49
|
14 |
|
!class_exists($className) || |
50
|
14 |
|
!is_subclass_of($className, EntityInterface::class) |
51
|
7 |
|
) { |
52
|
2 |
|
throw new InvalidEntityClassException( |
53
|
2 |
|
"Class '{$className}' does not implements the " . |
54
|
1 |
|
"Slick\Orm\EntityInterface interface." |
55
|
1 |
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
12 |
|
$this->entityClass = $className; |
59
|
12 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Sets the entity that will be the subject of this service |
64
|
|
|
* |
65
|
|
|
* @param EntityInterface $entity |
66
|
|
|
* |
67
|
|
|
* @return $this|self|AbstractEntityService |
68
|
|
|
*/ |
69
|
2 |
|
public function setEntity(EntityInterface $entity) |
70
|
|
|
{ |
71
|
2 |
|
$this->entity = $entity; |
72
|
2 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the entity FQ class name |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
6 |
|
public function getEntityClassName() |
81
|
|
|
{ |
82
|
6 |
|
if (null == $this->entityClass) { |
83
|
2 |
|
$this->setEntityClass(get_class($this->getEntity())); |
84
|
1 |
|
} |
85
|
6 |
|
return $this->entityClass; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Gets entity class |
90
|
|
|
* |
91
|
|
|
* @return EntityInterface |
92
|
|
|
*/ |
93
|
4 |
|
public function getEntity() |
94
|
|
|
{ |
95
|
4 |
|
if (null == $this->entity) { |
96
|
2 |
|
throw new MissingEntityException( |
97
|
1 |
|
"There are no Entity object set for this service." |
98
|
1 |
|
); |
99
|
|
|
} |
100
|
2 |
|
return $this->entity; |
101
|
|
|
} |
102
|
|
|
} |