Valuation
The value
service is a powerful class that determines the value of a target based on a predictable procedure of processing.
While you may not use this service on your own much it's important to understand as it's used heavily in the Streams Platform specifically where UI is concerned.
Basic Usage
To get started you will need to include the \Anomaly\Streams\Platform\Support\Value
class in your own.
Value::make()
The make
method makes the model value from the target value definition. Below are the steps taken in order from first to last:
- Checks for
view
in theparameters
and returns it with theentry
if found. - Checks for
template
in theparameters and parses it with the
entry` if found. - Checks if the
entry
is an instance ofEntryInterface
and has a field field with slugvalue
.- If the
value
is a field it returns the field value. - If the
value
is a relation it returns the relationstitle_column
.
- If the
- Decorate the entry.
- Checks if the
value
is like{term}.*
and renders the string like:{{ {value}|raw }}
- Evaluates the
value
with\Anomaly\Streams\Platform\Support\Evaluator
. - If the
entry
isArrayable
then runtoArray
on it. - Wraps the
value
in thewrapper
parameter. By default this is simply{value}
. Note the value can be an array here - If the
value
is a string. Parse it with the entry again. - If the
value
is*.*.*::*
then try translating it. - If the value is parsable then try parsing it.
- Purifies the output unless
is_safe
parameter istrue
.
As you can see this flow and built in manipulation can allow for very powerful values with only an array. Compound this with resolvers and evaluators and you can start deferring logic for values or parts of the value to closures and handlers.. pretty cool right?
Returns: mixed
Arguments
Key | Required | Type | Default | Description |
---|---|---|---|---|
$paramters |
true |
string|array |
none |
The value definition. If a string the parameters become the value string. |
$entry |
true |
string|array |
none |
The subject model. |
$term |
false |
string |
entry |
The term to use in valuation strings. |
$payload |
false |
array |
null |
Additional payload to pass along during the process. |
Example
// A simple example
$value->make('name', $entry); // Ryan Thompson
// A more complex example
$value = [
'wrapper' => '
<strong>{value.file}</strong>
<br>
<small class="text-muted">{value.disk}://{value.folder}/{value.file}</small>
<br>
<span>{value.size} {value.keywords}</span>',
'value' => [
'file' => 'entry.name',
'folder' => 'entry.folder.slug',
'keywords' => 'entry.keywords.labels|join',
'disk' => 'entry.folder.disk.slug',
'size' => 'entry.size_label',
],
];
$value->make($value, compact('entry'));