CodeGen
CodeGen copied to clipboard
add new Boolean Expression
new BoolExp(array $args, $operateor)
shold produce
$arg1 $operateor $arg2 operateor $argN
I think this can be done by adding a BinaryExpr:
$expr = new BinaryExpr('AND', $expr, $expr2);
How about this:
<?php
namespace CodeGen\Expr;
use CodeGen\Renderable;
use CodeGen\Variable;
use LogicException;
class BinaryExpr implements Renderable
{
/**
* @var Variable|string
*/
protected $operator;
protected $args;
protected $op = '->';
public function __construct($operator, $arg1, $arg2 = 'FOETWEHNAQNO09XI5OL0')
{
$this->operator = $operator;
if (is_array($arg1)) {
$this->args = $arg1;
} else {
if ($arg2 === 'FOETWEHNAQNO09XI5OL0') { //RANDOM - no second arg provided
throw new LogicException('second argument missing');
}
$this->args = [$arg1, $arg2];
}
}
public function render(array $args = array())
{
if ($this->operator instanceof Renderable) {
$operator = $this->operator->render($args);
} else {
$operator = $this->operator;
}
$arguments = [];
foreach ($this->args as $arg) {
if ($arg instanceof Renderable) {
$arguments[] = $arg->render($args);
} else {
$arguments[] = $arg;
}
}
return implode(' ' . $operator . ' ', $arguments);
}
public function __toString()
{
return $this->render();
}
}
Why do we need FOETWEHNAQNO09XI5OL0?
Please replace FOETWEHNAQNO09XI5OL0 with NULL, I think Null would work