PHP code example of arekx / array-expression-engine
1. Go to this page and download the library: Download arekx/array-expression-engine 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/ */
use ArekX\ArrayExpression\Interfaces\ExpressionParser;
use ArekX\ArrayExpression\Interfaces\Operator;
use ArekX\ArrayExpression\Interfaces\ValueParser;
class DogOperator implements Operator
{
/** @var ExpressionParser */
public $parser;
/** @var string */
public $name;
/** @var Operator */
public $subExpression;
public function configure(array $config)
{
$this->name = $config[0];
$this->assertIsExpression($config[1]); // Assert that the value is an expression.
$this->subExpression = $this->parser->parse($config[1]);
}
public function getName(): string
{
return $this->name;
}
public function setParser(ExpressionParser $parser)
{
$this->parser = $parser;
}
public function evaluate(ValueParser $value)
{
return str_ireplace('cat', 'dog', $this->subExpression->evaluate($value));
}
}
$evaluator = \ArekX\ArrayExpression\Evaluator::create();
$evaluator->getExpressionParser()->setType('dog', DogOperator::class);
$test = ['dog', ['get', 'sentence']];
$result = $evaluator->run($test, ['sentence' => 'Hello this is cat.']); // Returns: Hello this is dog.
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.