|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Isolate\UnitOfWork\Entity\Identifier; |
|
4
|
|
|
|
|
5
|
|
|
use Isolate\UnitOfWork\Entity\Definition\Repository; |
|
6
|
|
|
use Isolate\UnitOfWork\Entity\Identifier; |
|
7
|
|
|
use Isolate\UnitOfWork\Exception\RuntimeException; |
|
8
|
|
|
use Isolate\UnitOfWork\Object\PropertyAccessor; |
|
9
|
|
|
|
|
10
|
|
|
class EntityIdentifier implements Identifier |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Repository |
|
14
|
|
|
*/ |
|
15
|
|
|
private $definitions; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var PropertyAccessor |
|
19
|
|
|
*/ |
|
20
|
|
|
private $propertyAccessor; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param Repository $definitions |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(Repository $definitions) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->definitions = $definitions; |
|
28
|
|
|
$this->propertyAccessor = new PropertyAccessor(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param $object |
|
33
|
|
|
* @return boolean |
|
34
|
|
|
*/ |
|
35
|
|
|
public function isEntity($object) |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->definitions->hasDefinition($object); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param mixed $entity |
|
42
|
|
|
* @return bool |
|
43
|
|
|
* @throws RuntimeException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function isPersisted($entity) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->validateEntity($entity); |
|
48
|
|
|
$entityDefinition = $this->definitions->getDefinition($entity); |
|
49
|
|
|
|
|
50
|
|
|
return $entityDefinition->getIdentityStrategy()->isIdentified($entity); |
|
51
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param $entity |
|
56
|
|
|
* @return mixed |
|
57
|
|
|
* @throws RuntimeException |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getIdentity($entity) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->validateEntity($entity); |
|
62
|
|
|
$entityDefinition = $this->definitions->getDefinition($entity); |
|
63
|
|
|
|
|
64
|
|
|
return $entityDefinition->getIdentityStrategy()->getIdentity($entity); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param $entity |
|
69
|
|
|
* @throws RuntimeException |
|
70
|
|
|
*/ |
|
71
|
|
|
private function validateEntity($entity) |
|
72
|
|
|
{ |
|
73
|
|
|
if (!$this->definitions->hasDefinition($entity)) { |
|
74
|
|
|
throw new RuntimeException(sprintf("Class \"%s\" does not have definition.", get_class($entity))); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|