1. Go to this page and download the library: Download setherator/variables library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
setherator / variables example snippets
use Setherator\Variables\Variables;
use function Setherator\Variables\all;
use function Setherator\Variables\context;
use function Setherator\Variables\factory;
use function Setherator\Variables\logic;
use function Setherator\Variables\passthrough;
use function Setherator\Variables\ref;
$variables = new Variables();
$variables->setContext(['Prefix: ']);
$globalLogicState = true;
$providedVariables = [
'scalar' => 'foo',
// will execute closure and catch value
'closure' => fn() => 'closure Foo ' . random_int(1, 10),
// Will be fetched at code load
'ref' => ref('scalar', 'Not found'),
// Will be only fetched when 'ref_on_get' is fetched
'ref_on_get' => fn() => ref('scalar'),
// will return number increasing every time its fetched and 'Number: ' will be passed as argument to
'factory' => factory(
function ($prefix) {
static $i = 0;
return $prefix . $i++;
},
function () {
static $i = 0;
return 'Number ' . $i++ . ': ';
},
),
// Will inject context values as arguments from Variables::getContext();
'context' => context(fn(string $prefix) => $prefix . random_int(1, 10)),
'factory_context' => factory(context(fn(string $prefix) => $prefix . random_int(1, 10))),
'all' => all(
fn() => 'First',
fn() => 'Second',
'Third'
),
'logic' => logic(
fn() => 1,
fn() => 'Condition: true',
fn() => 'Condition: false',
),
'logic_strict' => logic(
fn() => 1,
fn() => 'Condition: true',
fn() => 'Condition: false',
true
),
'logic_factory' => factory(
logic(
function () use (&$globalLogicState) {
return $globalLogicState;
},
function () use (&$globalLogicState) {
$globalLogicState = !$globalLogicState;
return 'Factory Condition: true';
},
function () use (&$globalLogicState) {
$globalLogicState = !$globalLogicState;
return 'Factory Condition: false';
},
)
),
'passthrough' => passthrough(
Closure::fromCallable('strtoupper'),
fn() => 'Passthrough: ' . ref('logic_factory'),
),
'passthrough_factory' => factory(
passthrough(
Closure::fromCallable('strtoupper'),
fn() => 'Passthrough Factory: ' . ref('logic_factory'),
)
),
'first' => first(
0,
'value'
),
];
$variables->add($providedVariables);
echo $variables->get('scalar') . PHP_EOL;
echo $variables->get('closure') . PHP_EOL;
echo $variables->get('closure') . PHP_EOL;
echo $variables->get('ref') . PHP_EOL;
echo $variables->get('ref_on_get') . PHP_EOL;
echo $variables->get('factory') . PHP_EOL;
echo $variables->get('factory') . PHP_EOL;
echo $variables->get('context') . PHP_EOL;
echo $variables->get('context') . PHP_EOL;
echo $variables->get('factory_context') . PHP_EOL;
echo $variables->get('factory_context') . PHP_EOL;
echo json_encode($variables->get('all')) . PHP_EOL;
echo $variables->get('logic') . PHP_EOL;
echo $variables->get('logic_strict') . PHP_EOL;
echo $variables->get('logic_factory') . PHP_EOL;
echo $variables->get('logic_factory') . PHP_EOL;
echo $variables->get('logic_factory') . PHP_EOL;
echo $variables->get('passthrough') . PHP_EOL;
echo $variables->get('passthrough') . PHP_EOL;
echo $variables->get('passthrough_factory') . PHP_EOL;
echo $variables->get('passthrough_factory') . PHP_EOL;
echo $variables->get('passthrough_factory') . PHP_EOL;
echo $variables->get('first') . PHP_EOL . PHP_EOL;
// Print all computed values
echo json_encode($variables->all(), JSON_PRETTY_PRINT) . PHP_EOL;
// Print all non computed values (Closures do not json encode)
echo json_encode($variables->all(false), JSON_PRETTY_PRINT) . PHP_EOL;
function ref(string $name, $default = null, bool $raw = false);
function reference(string $name, $default = null, bool $raw = false);
function refFn(string $name, $default = null, bool $raw = false);
function referenceFn(string $name, $default = null, bool $raw = false);
function env(string $name, $default = null): string;
function context(Closure $closure, ...$args): Closure;Cacheable
[
'all' => all(
ask('Please provide project name'),
ask('Please provide folder name')
)
]
function logic($condition, $true, $false, bool $strict = false): Closure;
[
'logic_based_value' => logic(
fn() => askChoice('Please select app env', ['dev', 'prod']) !== 'dev',
'Environement is production',
fn() => 'Enviroment is dev and time is: ' . time()
)
]
function passthrough(Closure $closure, ...$args): Closure;
[
'passthough_value' => passthrough(
function (string $projectName, string $projectFolderName) {
return 'Project "' . $projectName . '" at "' . $projectFolderName . '"';
}
ask('Please provide project name'),
ask('Please provide project folder name')
)
]
function first(...$args);
class JsonEncodeValue
{
private $value;
public function __construct($value)
{
$this->value = $value;
}
public function getValue()
{
return $this->value;
}
}
class JsonEncodeValueParser implements ValueParserInterface
{
public function supports($value): bool
{
return $value instanceof JsonEncodeValue;
}
public function parseValue($value, Variables $variables)
{
return json_encode($value->getValue());
}
}
$variables = new Variables();
$variables->addValueParser(new JsonEncodeValueParser());
$variables->set('json', new JsonEncodeValue(['foo' => 'bar']);
$variables->get('json') // => {"foo":"bar"}