|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the PommProject/ModelManager package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) 2014 - 2015 Grégoire HUBERT <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace PommProject\ModelManager\Model; |
|
11
|
|
|
|
|
12
|
|
|
use PommProject\ModelManager\Model\FlexibleEntity\FlexibleEntityInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* IdentityMapper |
|
16
|
|
|
* |
|
17
|
|
|
* Cache for FlexibleEntityInterface instances to ensure there are no different |
|
18
|
|
|
* instances for the same data. |
|
19
|
|
|
* |
|
20
|
|
|
* @package ModelManager |
|
21
|
|
|
* @copyright 2014 - 2015 Grégoire HUBERT |
|
22
|
|
|
* @author Grégoire HUBERT |
|
23
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
|
24
|
|
|
*/ |
|
25
|
|
|
class IdentityMapper |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var FlexibleEntityInterface[] |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $instances = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* getSignature |
|
34
|
|
|
* |
|
35
|
|
|
* Compute a unique signature upon entity's values in its primary key. If |
|
36
|
|
|
* an empty primary key is provided, null is returned. |
|
37
|
|
|
* |
|
38
|
|
|
* @static |
|
39
|
|
|
* @access public |
|
40
|
|
|
* @param FlexibleEntityInterface $entity |
|
41
|
|
|
* @param array $primary_key |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function getSignature(FlexibleEntityInterface $entity, array $primary_key) |
|
45
|
|
|
{ |
|
46
|
|
|
if (count($primary_key) === 0) { |
|
47
|
|
|
return null; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return sha1(sprintf("%s|%s", serialize($entity->fields($primary_key)), get_class($entity))); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* fetch |
|
55
|
|
|
* |
|
56
|
|
|
* Pool FlexibleEntityInterface instances and update them if necessary. |
|
57
|
|
|
* |
|
58
|
|
|
* @access public |
|
59
|
|
|
* @param FlexibleEntityInterface $entity |
|
60
|
|
|
* @param array $primary_key |
|
61
|
|
|
* @return FlexibleEntityInterface |
|
62
|
|
|
*/ |
|
63
|
|
|
public function fetch(FlexibleEntityInterface $entity, array $primary_key) |
|
64
|
|
|
{ |
|
65
|
|
|
$signature = self::getSignature($entity, $primary_key); |
|
66
|
|
|
|
|
67
|
|
|
if ($signature === null) { |
|
68
|
|
|
return $entity; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (!array_key_exists($signature, $this->instances)) { |
|
72
|
|
|
$this->instances[$signature] = $entity; |
|
73
|
|
|
$entity->status(FlexibleEntityInterface::STATUS_EXIST); |
|
74
|
|
|
} else { |
|
75
|
|
|
$this->instances[$signature]->hydrate($entity->fields()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this->instances[$signature]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* clear |
|
83
|
|
|
* |
|
84
|
|
|
* Flush instances from the identity mapper. |
|
85
|
|
|
* |
|
86
|
|
|
* @access public |
|
87
|
|
|
* @return IdentityMapper $this |
|
88
|
|
|
*/ |
|
89
|
|
|
public function clear() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->instances = []; |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|