c9s / CodeGen

Transform your dynamic calls to static calls!
MIT License
38 stars 3 forks source link

add not expression #16

Open dafik opened 9 years ago

dafik commented 9 years ago

new NotExpr($expr)

should produce

!$expr

c9s commented 9 years ago

I think this can be done by an UnaryExpr:

$expr = new UnaryExpr('!', $expr);
dafik commented 9 years ago

How about this:

<?php
namespace CodeGen\Expr;

use CodeGen\Renderable;

class UnaryExpr implements Renderable
{
    /**
     * @var string|Renderable
     */
    public $operand;
    /**
     * @var string|Renderable
     */
    public $expression;

    protected $op = '->';

    public function __construct($operand, $expression)
    {
        $this->operand = $operand;
        $this->expression = $expression;
    }

    public function render(array $args = array())
    {
        $out = '';

        if ($this->operand instanceof Renderable) {
            $out .= $this->operand->render($args);
        } else {
            $out .= $this->operand;
        }
        if ($this->expression instanceof Renderable) {
            $out .= $this->expression->render($args);
        } else {
            $out .= $this->expression;
        }
        return $out;
    }

    public function __toString()
    {
        return $this->render();
    }

}
c9s commented 8 years ago

Looks good,

but protected $op is not used.