-
-
Notifications
You must be signed in to change notification settings - Fork 550
/
Copy pathHasAugmentedInstance.php
164 lines (129 loc) · 4.01 KB
/
HasAugmentedInstance.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Statamic\Data;
use BadMethodCallException;
use Statamic\Contracts\Data\Augmented;
use Statamic\Facades\Compare;
use Statamic\Fields\Value;
use Statamic\Support\Traits\Hookable;
trait HasAugmentedInstance
{
use Hookable;
public function augmentedValue($key)
{
return $this->augmented()->get($key);
}
private function toAugmentedCollectionWithFields($keys, $fields = null)
{
return $this->augmented()
->withRelations($this->defaultAugmentedRelations())
->withBlueprintFields($fields)
->select($keys ?? $this->defaultAugmentedArrayKeys());
}
public function toAugmentedCollection($keys = null)
{
return $this->toAugmentedCollectionWithFields($keys);
}
public function toAugmentedArray($keys = null)
{
return $this->toAugmentedCollection($keys)->all();
}
public function toDeferredAugmentedArray($keys = null)
{
return $this->toAugmentedCollectionWithFields($keys)->deferredAll();
}
public function toDeferredAugmentedArrayUsingFields($keys, $fields)
{
return $this->toAugmentedCollectionWithFields($keys, $fields)->deferredAll();
}
public function toShallowAugmentedCollection()
{
return $this->augmented()->select($this->shallowAugmentedArrayKeys())->withShallowNesting();
}
public function toShallowAugmentedArray()
{
return $this->toShallowAugmentedCollection()->all();
}
public function augmented(): Augmented
{
return $this->runHooks('augmented', $this->newAugmentedInstance());
}
abstract public function newAugmentedInstance(): Augmented;
protected function defaultAugmentedArrayKeys()
{
return null;
}
public function shallowAugmentedArrayKeys()
{
return ['id', 'title', 'api_url'];
}
protected function defaultAugmentedRelations()
{
return [];
}
public function toEvaluatedAugmentedArray($keys = null)
{
$collection = $this->toAugmentedCollection($keys);
// Can't just chain ->except() because it would return a new
// collection and the existing 'withRelations' would be lost.
if ($exceptions = $this->excludedEvaluatedAugmentedArrayKeys()) {
$collection = $collection
->except($exceptions)
->withRelations($collection->getRelations());
}
return $collection->withEvaluation()->toArray();
}
protected function excludedEvaluatedAugmentedArrayKeys()
{
return null;
}
public function toArray()
{
return $this->toEvaluatedAugmentedArray();
}
public function jsonSerialize(): mixed
{
return $this->toArray();
}
public function __get($key)
{
$value = $this->augmentedValue($key);
$value = $value instanceof Value ? $value->value() : $value;
if (Compare::isQueryBuilder($value)) {
$value = $value->get();
}
return $value;
}
public function __call($method, $args)
{
$value = $this->augmentedValue($method);
$value = $value instanceof Value ? $value->value() : $value;
if (Compare::isQueryBuilder($value)) {
return $value;
}
throw new BadMethodCallException(sprintf('Call to undefined method %s::%s()', static::class, $method));
}
#[\ReturnTypeWillChange]
public function offsetGet($key)
{
return $this->__get($key);
}
#[\ReturnTypeWillChange]
public function offsetSet($key, $value)
{
throw new \Exception('Method offsetSet is not currently supported.');
}
#[\ReturnTypeWillChange]
public function offsetExists($key)
{
return ! is_null($this->offsetGet($key));
}
#[\ReturnTypeWillChange]
public function offsetUnset($key)
{
throw new \Exception('Method offsetUnset is not currently supported.');
}
public function __isset($key)
{
return $this->offsetExists($key);
}
}