1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Hydrogen package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace RDS\Hydrogen; |
11
|
|
|
|
12
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
13
|
|
|
use Illuminate\Support\Arr; |
14
|
|
|
use Illuminate\Support\Collection as BaseCollection; |
15
|
|
|
use RDS\Hydrogen\HighOrderMessaging\HigherOrderCollectionProxy; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Collection |
19
|
|
|
* @mixin BaseCollection |
20
|
|
|
*/ |
21
|
|
|
class Collection extends ArrayCollection |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array|string[] |
25
|
|
|
*/ |
26
|
|
|
private static $proxies; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var BaseCollection |
30
|
|
|
*/ |
31
|
|
|
private $inner; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Collection constructor. |
35
|
|
|
* @param array|iterable $elements |
36
|
|
|
*/ |
37
|
2 |
|
public function __construct($elements = []) |
38
|
|
|
{ |
39
|
2 |
|
$this->inner = BaseCollection::wrap($elements); |
40
|
|
|
|
41
|
2 |
|
parent::__construct($this->inner->toArray()); |
42
|
|
|
|
43
|
2 |
|
$this->exportProxies(); |
44
|
2 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
2 |
|
private function exportProxies(): void |
50
|
|
|
{ |
51
|
2 |
|
if (static::$proxies === null) { |
|
|
|
|
52
|
1 |
|
$class = new \ReflectionClass($this->inner); |
53
|
1 |
|
$property = $class->getProperty('proxies'); |
54
|
1 |
|
$property->setAccessible(true); |
55
|
|
|
|
56
|
1 |
|
static::$proxies = $property->getValue(); |
|
|
|
|
57
|
|
|
} |
58
|
2 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $name |
62
|
|
|
* @param array $arguments |
63
|
|
|
* @return mixed |
64
|
|
|
* @throws \BadMethodCallException |
65
|
|
|
*/ |
66
|
|
|
public static function __callStatic(string $name, array $arguments = []) |
67
|
|
|
{ |
68
|
|
|
if (\method_exists(BaseCollection::class, $name)) { |
69
|
|
|
$result = BaseCollection::$name(...$arguments); |
70
|
|
|
|
71
|
|
|
if ($result instanceof BaseCollection) { |
72
|
|
|
return new static($result->toArray()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $result; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$error = \sprintf('Call to undefined method %s::%s', static::class, $name); |
79
|
|
|
throw new \BadMethodCallException($error); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Wrap the given value in a collection if applicable. |
84
|
|
|
* |
85
|
|
|
* @param mixed $value |
86
|
|
|
* @return static |
87
|
|
|
*/ |
88
|
2 |
|
public static function wrap($value): self |
89
|
|
|
{ |
90
|
|
|
switch (true) { |
91
|
2 |
|
case $value instanceof self: |
92
|
|
|
return new static($value); |
|
|
|
|
93
|
|
|
|
94
|
2 |
|
case $value instanceof BaseCollection: |
95
|
|
|
return new static($value); |
96
|
|
|
|
97
|
|
|
default: |
98
|
2 |
|
return new static(Arr::wrap($value)); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $name |
104
|
|
|
* @param array $arguments |
105
|
|
|
* @return mixed |
106
|
|
|
* @throws \BadMethodCallException |
107
|
|
|
*/ |
108
|
|
|
public function __call(string $name, array $arguments = []) |
109
|
|
|
{ |
110
|
|
|
if (\method_exists($this->inner, $name)) { |
111
|
|
|
$result = $this->inner->$name(...$arguments); |
112
|
|
|
|
113
|
|
|
if ($result instanceof BaseCollection) { |
114
|
|
|
return new static($result->toArray()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $result; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$error = \sprintf('Call to undefined method %s::%s', static::class, $name); |
121
|
|
|
throw new \BadMethodCallException($error); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $key |
126
|
|
|
* @return HigherOrderCollectionProxy |
127
|
|
|
* @throws \InvalidArgumentException |
128
|
|
|
*/ |
129
|
|
|
public function __get(string $key): HigherOrderCollectionProxy |
130
|
|
|
{ |
131
|
|
|
if (! \in_array($key, static::$proxies, true)) { |
|
|
|
|
132
|
|
|
$error = "Property [{$key}] does not exist on this collection instance."; |
133
|
|
|
throw new \InvalidArgumentException($error); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return new HigherOrderCollectionProxy($this, $key); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: