1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\Model\Reference; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\Model\ModelInterface; |
8
|
|
|
use Chubbyphp\Model\ResolverInterface; |
9
|
|
|
|
10
|
|
|
final class LazyModelReference implements ModelReferenceInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var ResolverInterface |
14
|
|
|
*/ |
15
|
|
|
private $resolver; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $modelClass; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string|null |
24
|
|
|
*/ |
25
|
|
|
private $id; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
private $resolved = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ModelInterface|null |
34
|
|
|
*/ |
35
|
|
|
private $initialModel; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ModelInterface|null |
39
|
|
|
*/ |
40
|
|
|
private $model; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param ResolverInterface $resolver |
44
|
|
|
* @param string $modelClass |
45
|
|
|
* @param string|null $id |
46
|
|
|
*/ |
47
|
8 |
|
public function __construct(ResolverInterface $resolver, string $modelClass, string $id = null) |
48
|
|
|
{ |
49
|
8 |
|
$this->resolver = $resolver; |
50
|
8 |
|
$this->modelClass = $modelClass; |
51
|
8 |
|
$this->id = $id; |
52
|
8 |
|
} |
53
|
|
|
|
54
|
8 |
|
private function resolveModel() |
55
|
|
|
{ |
56
|
8 |
|
if ($this->resolved) { |
57
|
4 |
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
8 |
|
$this->resolved = true; |
61
|
|
|
|
62
|
8 |
|
$this->initialModel = $this->resolver->find($this->modelClass, $this->id); |
63
|
8 |
|
$this->model = $this->initialModel; |
64
|
8 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param ModelInterface|null $model |
68
|
|
|
* |
69
|
|
|
* @return ModelReferenceInterface |
70
|
|
|
*/ |
71
|
1 |
View Code Duplication |
public function setModel(ModelInterface $model = null): ModelReferenceInterface |
72
|
|
|
{ |
73
|
1 |
|
$this->resolveModel(); |
74
|
|
|
|
75
|
1 |
|
$this->model = $model; |
76
|
1 |
|
$this->id = null !== $model ? $model->getId() : null; |
77
|
|
|
|
78
|
1 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return ModelInterface|null |
83
|
|
|
*/ |
84
|
1 |
|
public function getModel() |
85
|
|
|
{ |
86
|
1 |
|
$this->resolveModel(); |
87
|
|
|
|
88
|
1 |
|
return $this->model; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return null|string |
93
|
|
|
*/ |
94
|
2 |
|
public function getId() |
95
|
|
|
{ |
96
|
2 |
|
return $this->id; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return ModelInterface|null |
101
|
|
|
*/ |
102
|
1 |
|
public function getInitialModel() |
103
|
|
|
{ |
104
|
1 |
|
$this->resolveModel(); |
105
|
|
|
|
106
|
1 |
|
return $this->initialModel; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return array|null |
111
|
|
|
*/ |
112
|
3 |
|
public function jsonSerialize() |
113
|
|
|
{ |
114
|
3 |
|
$this->resolveModel(); |
115
|
|
|
|
116
|
3 |
|
if (null === $this->model) { |
117
|
1 |
|
return null; |
118
|
|
|
} |
119
|
|
|
|
120
|
2 |
|
$this->jsonSerializableOrException(); |
121
|
|
|
|
122
|
1 |
|
return $this->model->jsonSerialize(); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @throws \LogicException |
127
|
|
|
*/ |
128
|
2 |
View Code Duplication |
private function jsonSerializableOrException() |
129
|
|
|
{ |
130
|
2 |
|
if (!$this->model instanceof \JsonSerializable) { |
|
|
|
|
131
|
1 |
|
throw new \LogicException( |
132
|
1 |
|
sprintf('Model %s does not implement %s', get_class($this->model), \JsonSerializable::class) |
133
|
|
|
); |
134
|
|
|
} |
135
|
1 |
|
} |
136
|
|
|
} |
137
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.