1
|
|
|
<?php namespace Anomaly\Streams\Platform\Support; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Entry\Contract\EntryInterface; |
4
|
|
|
use Anomaly\Streams\Platform\Model\EloquentModel; |
5
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Value |
9
|
|
|
* |
10
|
|
|
* @link http://pyrocms.com/ |
11
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
12
|
|
|
* @author Ryan Thompson <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Value |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The string parser. |
19
|
|
|
* |
20
|
|
|
* @var Parser |
21
|
|
|
*/ |
22
|
|
|
protected $parser; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The template parser. |
26
|
|
|
* |
27
|
|
|
* @var Template |
28
|
|
|
*/ |
29
|
|
|
protected $template; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The evaluator utility. |
33
|
|
|
* |
34
|
|
|
* @var Evaluator |
35
|
|
|
*/ |
36
|
|
|
protected $evaluator; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The decorator utility. |
40
|
|
|
* |
41
|
|
|
* @var Decorator |
42
|
|
|
*/ |
43
|
|
|
protected $decorator; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Create a new ColumnValue instance. |
47
|
|
|
* |
48
|
|
|
* @param Parser $parser |
49
|
|
|
* @param Template $template |
50
|
|
|
* @param Evaluator $evaluator |
51
|
|
|
* @param Decorator $decorator |
52
|
|
|
*/ |
53
|
|
|
public function __construct(Parser $parser, Template $template, Evaluator $evaluator, Decorator $decorator) |
54
|
|
|
{ |
55
|
|
|
$this->parser = $parser; |
56
|
|
|
$this->template = $template; |
57
|
|
|
$this->evaluator = $evaluator; |
58
|
|
|
$this->decorator = $decorator; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Make a value from the parameters and entry. |
63
|
|
|
* |
64
|
|
|
* @param $parameters |
65
|
|
|
* @param $payload |
66
|
|
|
* @param array $payload |
67
|
|
|
* @return mixed|string |
68
|
|
|
*/ |
69
|
|
|
public function make($parameters, $entry, $term = 'entry', $payload = []) |
70
|
|
|
{ |
71
|
|
|
$payload[$term] = $entry; |
72
|
|
|
|
73
|
|
|
/* |
74
|
|
|
* If a flat value was sent in |
75
|
|
|
* then convert it to an array. |
76
|
|
|
*/ |
77
|
|
|
if (!is_array($parameters)) { |
78
|
|
|
$parameters = [ |
79
|
|
|
'value' => $parameters, |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$value = array_get($parameters, 'value'); |
84
|
|
|
|
85
|
|
|
/* |
86
|
|
|
* If the value is a view path then return a view. |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
if ($view = array_get($parameters, 'view')) { |
|
|
|
|
89
|
|
|
return view($view, ['value' => $value, $term => $entry]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/* |
93
|
|
|
* If the value uses a template then parse it. |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
if ($template = array_get($parameters, 'template')) { |
|
|
|
|
96
|
|
|
return $this->template->render($template, ['value' => $value, $term => $entry]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/* |
100
|
|
|
* If the entry is an instance of EntryInterface |
101
|
|
|
* then try getting the field value from the entry. |
102
|
|
|
*/ |
103
|
|
|
if ($entry instanceof EntryInterface && $entry->getField($value)) { |
104
|
|
|
|
105
|
|
|
/* @var EntryInterface $relation */ |
106
|
|
|
if ($entry->assignmentIsRelationship($value) && $relation = $entry->{camel_case($value)}) { |
107
|
|
|
if ($relation instanceof EloquentModel) { |
108
|
|
|
$value = $relation->getTitle(); |
109
|
|
|
} |
110
|
|
|
} else { |
111
|
|
|
$value = $entry->getFieldValue($value); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/* |
116
|
|
|
* Decorate the entry object before |
117
|
|
|
* sending to decorate so that data_get() |
118
|
|
|
* can get into the presenter methods. |
119
|
|
|
*/ |
120
|
|
|
$payload[$term] = $entry = $this->decorator->decorate($entry); |
121
|
|
|
|
122
|
|
|
/* |
123
|
|
|
* If the value matches a dot notation |
124
|
|
|
* then parse it as a template. |
125
|
|
|
*/ |
126
|
|
|
if (is_string($value) && preg_match("/^{$term}.([a-zA-Z\\_]+)/", $value, $match)) { |
127
|
|
|
$value = $this->template->render("{{ {$value}|raw }}", $payload); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/* |
131
|
|
|
* If the value matches a method in the presenter. |
132
|
|
|
*/ |
133
|
|
|
if (is_string($value) && preg_match("/^{$term}.([a-zA-Z\\_]+)/", $value, $match)) { |
134
|
|
|
if (method_exists($entry, camel_case($match[1]))) { |
135
|
|
|
$value = $entry->{camel_case($match[1])}(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$payload[$term] = $entry; |
140
|
|
|
|
141
|
|
|
/* |
142
|
|
|
* By default we can just pass the value through |
143
|
|
|
* the evaluator utility and be done with it. |
144
|
|
|
*/ |
145
|
|
|
$value = $this->evaluator->evaluate($value, $payload); |
146
|
|
|
|
147
|
|
|
/* |
148
|
|
|
* Lastly, prepare the entry to be |
149
|
|
|
* parsed into the string. |
150
|
|
|
*/ |
151
|
|
|
if ($entry instanceof Arrayable) { |
152
|
|
|
$entry = $entry->toArray(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/* |
156
|
|
|
* Parse the value with the entry. |
157
|
|
|
*/ |
158
|
|
|
if ($wrapper = array_get($parameters, 'wrapper')) { |
159
|
|
|
$value = $this->parser->parse( |
160
|
|
|
$wrapper, |
161
|
|
|
['value' => $value, $term => $entry] |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/* |
166
|
|
|
* Parse the value with the value too. |
167
|
|
|
*/ |
168
|
|
|
if (is_string($value)) { |
169
|
|
|
$value = $this->parser->parse( |
170
|
|
|
$value, |
171
|
|
|
[ |
172
|
|
|
'value' => $value, |
173
|
|
|
$term => $entry, |
174
|
|
|
] |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/* |
179
|
|
|
* If the value looks like a language |
180
|
|
|
* key then try translating it. |
181
|
|
|
*/ |
182
|
|
|
if (is_string($value) && str_is('*.*.*::*', $value)) { |
183
|
|
|
$value = trans($value); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/* |
187
|
|
|
* If the value looks like a render-able |
188
|
|
|
* string then render it. |
189
|
|
|
*/ |
190
|
|
|
if (is_string($value) && str_contains($value, '{{')) { |
191
|
|
|
$value = $this->template->render($value, [$term => $entry]); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $value; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.