|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File was created 22.06.2016 06:01 |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace PeekAndPoke\Component\Slumber\Data; |
|
7
|
|
|
|
|
8
|
|
|
use PeekAndPoke\Types\ValueHolder; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Karsten J. Gerber <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class LazyDbReference implements ValueHolder |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var Repository */ |
|
16
|
|
|
private $repository; |
|
17
|
|
|
/** @var mixed */ |
|
18
|
|
|
private $referencedId; |
|
19
|
|
|
/** @var mixed */ |
|
20
|
|
|
private $referenced; |
|
21
|
|
|
/** @var bool */ |
|
22
|
|
|
private $alreadyTriedToLoad = false; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param Repository $repository |
|
26
|
|
|
* @param mixed $referencedId |
|
27
|
|
|
* |
|
28
|
|
|
* @return static |
|
29
|
|
|
*/ |
|
30
|
17 |
|
public static function create(Repository $repository, $referencedId) |
|
31
|
|
|
{ |
|
32
|
17 |
|
$result = new static(null); |
|
33
|
17 |
|
$result->repository = $repository; |
|
34
|
17 |
|
$result->referencedId = $referencedId; |
|
35
|
|
|
|
|
36
|
17 |
|
return $result; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param mixed $subject |
|
41
|
|
|
* |
|
42
|
|
|
* @return mixed |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public static function unwrap($subject) |
|
45
|
|
|
{ |
|
46
|
1 |
|
if ($subject instanceof self) { |
|
47
|
1 |
|
return $subject->getValue(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
return $subject; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* LazyDbReference constructor. |
|
55
|
|
|
* |
|
56
|
|
|
* @param mixed $referenced |
|
57
|
|
|
*/ |
|
58
|
17 |
|
public function __construct($referenced) |
|
59
|
|
|
{ |
|
60
|
17 |
|
$this->referenced = $referenced; |
|
61
|
17 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return mixed |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function getReferencedId() |
|
67
|
|
|
{ |
|
68
|
1 |
|
return $this->referencedId; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return mixed |
|
73
|
|
|
*/ |
|
74
|
4 |
|
public function getValue() |
|
75
|
|
|
{ |
|
76
|
4 |
|
if ($this->referenced !== null) { |
|
77
|
3 |
|
return $this->referenced; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// are we set up and did not try to load yet? |
|
81
|
4 |
|
if ($this->alreadyTriedToLoad === false) { |
|
82
|
|
|
// remember that we already tried to load |
|
83
|
4 |
|
$this->alreadyTriedToLoad = true; |
|
84
|
|
|
|
|
85
|
4 |
|
$this->referenced = $this->repository->findById($this->referencedId); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
4 |
|
return $this->referenced; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $name |
|
93
|
|
|
* @param array $arguments |
|
94
|
|
|
* |
|
95
|
|
|
* @throws \RuntimeException always |
|
96
|
|
|
*/ |
|
97
|
|
|
public function __call($name, $arguments) |
|
98
|
|
|
{ |
|
99
|
|
|
$value = $this->getValue(); |
|
100
|
|
|
|
|
101
|
|
|
if ($value === null) { |
|
102
|
|
|
throw new \RuntimeException( |
|
103
|
|
|
"The referenced object was not found in repo '{$this->repository->getName()}'::'{$this->referencedId}'" |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$type = \is_object($value) ? \get_class($value) : \gettype($value); |
|
108
|
|
|
|
|
109
|
|
|
throw new \RuntimeException( |
|
110
|
|
|
'Tried to access a DbReference without previously unwrapping it. Call $...->getValue() first. ' . |
|
111
|
|
|
"The wrapped object is of type '$type' and you called the method $name()" |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|