Value

Introduction

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.

Valuation is used heavily in the build process for UI builders.

Basic Usage

To get started you will need to include the \Anomaly\Streams\Platform\Support\Value class in your code.

make

The make method makes a compiled value from the parameters and entry provided. By default the entry uses the term entry in the valuation process but can be changed. You can also define additional payload should the view layer be used in valuation.

$value = app(Anomaly\Streams\Platform\Support\Template::class);

$value-make($parameters, $entry, $term = 'entry', $payload = [])
$value = app(Anomaly\Streams\Platform\Support\Template::class);

echo $value->make('name', $entry);
// Ryan Thompson
$value = app(Anomaly\Streams\Platform\Support\Template::class);

$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',
    ],
];

echo $value->make($value, compact('entry'));

Examples

Views

You can define the view parameter to let the view engine handle the output. The entry is passed to the view as well as any additional payload.

$value = app(Anomaly\Streams\Platform\Support\Template::class);

$value = [
    'view' => 'theme::example/view',
];

echo $value->make($value, compact('entry'));

Templates

The template parameter is similar to the view wrapper but uses the entry and not the value definition.

$value = app(Anomaly\Streams\Platform\Support\Template::class);

$value = [
    'template'     => '
            <strong>{entry.name}</strong>
            <br>
            <small class="text-muted">{entry.folder.disk.slug}://{entry.folder.slug}/{entry.name}</small>
            <br>
            <span>{entry.size_label}</span>',
];

echo $value->make($value, compact('entry'));

Field Values

The simplest way to define a value is by the field slug.

$value = app(Anomaly\Streams\Platform\Support\Template::class);

echo $value->make('name', compact('entry'));
// Kal-El

If the field slug is a relation - the related entry's title column will be related.

$value = app(Anomaly\Streams\Platform\Support\Template::class);

echo $value->make('parent', compact('entry'));
// Jor-El

Data Mapping

The main reason to use valuation in the first place is to map potentially complex data to simple string definitions by leveraging the object's structure itself.

To get started with mapping you will always prefix the data map with {term}.*. For these examples we will assume entry is our term.

Relations

You can walk collection methods and relationships.

$value = app(Anomaly\Streams\Platform\Support\Template::class);

echo $value->make('entry.parents.first.name', compact('entry'));
// Jor-El

Presenters

The below example uses the email field type's presenter.

$value = app(Anomaly\Streams\Platform\Support\Template::class);

echo $value->make('entry.email.mailto', compact('entry'));
// <a href="mailto:[email protected]">[email protected]</a>

Closures

You can use closures for the value and leverage the passed entry to define complex or conditional values.

$value = app(Anomaly\Streams\Platform\Support\Template::class);

$value = [
    'value' => function($entry) {
        return $entry->name;
    }
];

echo $value->make($value, compact('entry'));

Safe HTML

By default all output is sanitized using the purifier. To skip sanitation define the html as safe.

$value = app(Anomaly\Streams\Platform\Support\Template::class);

$value = [
    // ...
    'is_safe' => true,
];

echo $value->make($value, compact('entry'));