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
viewin theparametersand returns it with theentryif found. - Checks for
templatein theparameters and parses it with theentry` if found. - Checks if the
entryis an instance ofEntryInterfaceand has a field field with slugvalue.- If the
valueis a field it returns the field value. - If the
valueis a relation it returns the relationstitle_column.
- If the
- Decorate the entry.
- Checks if the
valueis like{term}.*and renders the string like:{{ {value}|raw }} - Evaluates the
valuewith\Anomaly\Streams\Platform\Support\Evaluator. - If the
entryisArrayablethen runtoArrayon it. - Wraps the
valuein thewrapperparameter. By default this is simply{value}. Note the value can be an array here - If the
valueis a string. Parse it with the entry again. - If the
valueis*.*.*::*then try translating it. - If the value is parsable then try parsing it.
- Purifies the output unless
is_safeparameter 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'));