CodeGen icon indicating copy to clipboard operation
CodeGen copied to clipboard

add new Boolean Expression

Open dafik opened this issue 9 years ago • 4 comments

new BoolExp(array $args, $operateor)

shold produce

$arg1 $operateor $arg2 operateor $argN

dafik avatar Nov 10 '15 13:11 dafik

I think this can be done by adding a BinaryExpr:

$expr = new BinaryExpr('AND', $expr, $expr2);

c9s avatar Nov 11 '15 05:11 c9s

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();
    }

}

dafik avatar Nov 12 '15 09:11 dafik

Why do we need FOETWEHNAQNO09XI5OL0?

c9s avatar Nov 12 '15 10:11 c9s

Please replace FOETWEHNAQNO09XI5OL0 with NULL, I think Null would work

c9s avatar Mar 14 '16 09:03 c9s