Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 641
Expand file tree
/
Copy pathAbstractAugmented.php
More file actions
Latest commit
125 lines (97 loc) · 3.03 KB
/
Copy pathAbstractAugmented.php
File metadata and controls
125 lines (97 loc) · 3.03 KB
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
<?php
namespaceStatamic\Data;
useStatamic\Contracts\Data\Augmented;
useStatamic\Fields\Value;
useStatamic\Statamic;
useStatamic\Support\Arr;
useStatamic\Support\Str;
abstractclass AbstractAugmented implements Augmented
{
protected$data;
protected$blueprintFields;
protected$relations = [];
publicfunction__construct($data)
{
$this->data = $data;
}
publicfunctionall()
{
return$this->select();
}
publicfunctionexcept($keys)
{
return$this->select(array_diff($this->keys(), Arr::wrap($keys)));
}
publicfunctionselect($keys = null)
{
$arr = [];
$keys = $this->filterKeys(Arr::wrap($keys ?: $this->keys()));
foreach ($keysas$key) {
$arr[$key] = $this->get($key);
}
return (newAugmentedCollection($arr))->withRelations($this->relations);
}
abstractpublicfunctionkeys();
publicfunctionget($handle): Value
{
$method = Str::camel($handle);
if ($this->methodExistsOnThisClass($method)) {
$value = $this->$method();
return$valueinstanceof Value
? $value
: newValue($value, $method, null, $this->data);
}
if (method_exists($this->data, $method) && collect($this->keys())->contains(Str::snake($handle))) {
return$this->wrapValue($this->data->$method(), $handle);
}
return$this->wrapValue($this->getFromData($handle), $handle);
}
protectedfunctionfilterKeys($keys)
{
returnarray_diff($keys, $this->excludedKeys());
}
protectedfunctionexcludedKeys()
{
return Statamic::isApiRoute()
? config('statamic.api.excluded_keys', [])
: [];
}
privatefunctionmethodExistsOnThisClass($method)
{
returnmethod_exists($this, $method) && ! in_array($method, ['select', 'except']);
}
protectedfunctiongetFromData($handle)
{
$value = method_exists($this->data, 'value') ? $this->data->value($handle) : $this->data->get($handle);
if (method_exists($this->data, 'getSupplement')) {
$value = $this->data->hasSupplement($handle)
? $this->data->getSupplement($handle)
: $value;
}
return$value;
}
protectedfunctionwrapValue($value, $handle)
{
$fields = $this->blueprintFields();
returnnewValue(
$value,
$handle,
optional($fields->get($handle))->fieldtype(),
$this->data
);
}
protectedfunctionblueprintFields()
{
if (! isset($this->blueprintFields)) {
$this->blueprintFields = (method_exists($this->data, 'blueprint') && $blueprint = $this->data->blueprint())
? $blueprint->fields()->all()
: collect();
}
return$this->blueprintFields;
}
publicfunctionwithRelations($relations)
{
$this->relations = $relations;
return$this;
}
}