1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sprocketbox\Eloquence; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Eloquence |
9
|
|
|
* |
10
|
|
|
* @package Sprocketbox\Eloquence |
11
|
|
|
*/ |
12
|
|
|
class IdentityManager |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The current instance. |
16
|
|
|
* |
17
|
|
|
* @var \Sprocketbox\Eloquence\IdentityManager |
18
|
|
|
*/ |
19
|
|
|
protected static IdentityManager $instance; |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Get the current Eloquence instance. |
23
|
|
|
* |
24
|
|
|
* @return \Sprocketbox\Eloquence\IdentityManager |
25
|
|
|
*/ |
26
|
|
|
public static function getInstance(): IdentityManager |
27
|
|
|
{ |
28
|
|
|
if (! isset(self::$instance)) { |
29
|
|
|
self::$instance = new static; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return self::$instance; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The identity map for models, mapped as identity => model |
37
|
|
|
* |
38
|
|
|
* @var array<string, Model> |
39
|
|
|
*/ |
40
|
|
|
protected array $models = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Check if the identity is already stored. |
44
|
|
|
* |
45
|
|
|
* @param \Sprocketbox\Eloquence\ModelIdentity $identity |
46
|
|
|
* |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public function hasIdentity(ModelIdentity $identity): bool |
50
|
|
|
{ |
51
|
|
|
return isset($this->models[$this->getStringIdentity($identity)]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the stored model for the given identity. |
56
|
|
|
* |
57
|
|
|
* @param \Sprocketbox\Eloquence\ModelIdentity $identity |
58
|
|
|
* |
59
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
60
|
|
|
*/ |
61
|
|
|
public function getIdentity(ModelIdentity $identity): ?Model |
62
|
|
|
{ |
63
|
|
|
return $this->models[$this->getStringIdentity($identity)] ?? null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set the model for the current identity. |
68
|
|
|
* |
69
|
|
|
* @param \Sprocketbox\Eloquence\ModelIdentity $identity |
70
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
71
|
|
|
* |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
|
|
public function storeIdentity(ModelIdentity $identity, Model $model): IdentityManager |
75
|
|
|
{ |
76
|
|
|
$this->models[$this->getStringIdentity($identity)] = $model; |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Remove a model from the identity map. |
83
|
|
|
* |
84
|
|
|
* @param \Sprocketbox\Eloquence\ModelIdentity $identity |
85
|
|
|
* |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function removeIdentity(ModelIdentity $identity): IdentityManager |
89
|
|
|
{ |
90
|
|
|
if ($this->hasIdentity($identity)) { |
91
|
|
|
unset($this->models[$this->getStringIdentity($identity)]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Remove all models from the identity map. |
99
|
|
|
* |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function flushIdentities(): IdentityManager |
103
|
|
|
{ |
104
|
|
|
$this->models = []; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get all stored identities. |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
public function allIdentities(): array |
115
|
|
|
{ |
116
|
|
|
return $this->models; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Turn the model identity into a string. |
121
|
|
|
* |
122
|
|
|
* @param \Sprocketbox\Eloquence\ModelIdentity $identity |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
protected function getStringIdentity(ModelIdentity $identity): string |
127
|
|
|
{ |
128
|
|
|
return (string) $identity; |
129
|
|
|
} |
130
|
|
|
} |