forked from statamic/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValues.php
130 lines (103 loc) · 2.92 KB
/
Values.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace Statamic\Fields;
use ArrayAccess;
use BadMethodCallException;
use Exception;
use Illuminate\Contracts\Support\Arrayable;
use IteratorAggregate;
use JsonSerializable;
use Statamic\Contracts\GraphQL\ResolvesValues as ResolvesGqlValues;
use Statamic\Facades\Compare;
use Traversable;
class Values implements ArrayAccess, Arrayable, IteratorAggregate, JsonSerializable, ResolvesGqlValues
{
protected $instance;
public function __construct($instance)
{
if (is_array($instance)) {
$instance = collect($instance);
}
$this->instance = $instance;
}
public function getProxiedInstance()
{
return $this->instance;
}
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return $this->getProxiedInstance()->has($offset);
}
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
$value = $this->getNormalizedValueFromProxiedCollection($offset);
if (Compare::isQueryBuilder($value)) {
return $value->get();
}
return $value;
}
private function getNormalizedValueFromProxiedCollection($key)
{
$value = $this->getProxiedInstance()->get($key);
return $value instanceof Value ? $value->value() : $value;
}
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
throw new Exception('Cannot set values by array access.');
}
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
throw new Exception('Cannot unset values by array access.');
}
public function getIterator(): Traversable
{
return $this->getProxiedInstance();
}
public function raw($key)
{
$value = $this->getProxiedInstance()->get($key);
return $value instanceof Value ? $value->raw() : $value;
}
public function __get($key)
{
return $this->offsetGet($key);
}
public function __set($key, $value)
{
throw new Exception('Cannot set values by property access.');
}
public function __call($method, $args)
{
if ($this->getProxiedInstance()->has($method)) {
$value = $this->getNormalizedValueFromProxiedCollection($method);
if (Compare::isQueryBuilder($value)) {
return $value;
}
}
throw new BadMethodCallException(sprintf('Method %s::%s does not exist.', static::class, $method));
}
public function toArray()
{
return $this->getProxiedInstance()->toArray();
}
public function all()
{
return $this->getProxiedInstance()->all();
}
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return $this->all();
}
public function resolveGqlValue($field)
{
return $this->$field;
}
public function resolveRawGqlValue($field)
{
return $this->raw($field);
}
}