nystudio107 /
craft-emptycoalesce
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Empty Coalesce plugin for Craft CMS 3.x |
||
| 4 | * |
||
| 5 | * Empty Coalesce adds the ??? operator to Twig that will return the first thing |
||
| 6 | * that is defined, not null, and not empty. |
||
| 7 | * |
||
| 8 | * @link https://nystudio107.com/ |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 9 | * @copyright Copyright (c) 2018 nystudio107 |
||
|
0 ignored issues
–
show
|
|||
| 10 | */ |
||
|
0 ignored issues
–
show
|
|||
| 11 | |||
| 12 | namespace nystudio107\emptycoalesce\Node\Expression; |
||
| 13 | |||
| 14 | use Twig\Compiler; |
||
| 15 | use Twig\Node\Expression\AbstractExpression; |
||
| 16 | use Twig\Node\Node; |
||
| 17 | |||
| 18 | /** |
||
|
0 ignored issues
–
show
|
|||
| 19 | * @author nystudio107 |
||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 20 | * @package EmptyCoalesce |
||
|
0 ignored issues
–
show
|
|||
| 21 | * @since 1.0.0 |
||
|
0 ignored issues
–
show
|
|||
| 22 | * |
||
| 23 | */ |
||
|
0 ignored issues
–
show
|
|||
| 24 | class EmptyCoalesceExpression extends AbstractExpression |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Checks if a variable is empty. |
||
| 28 | * |
||
| 29 | * {# evaluates to true if the foo variable is null, false, or the empty string #} |
||
| 30 | * {% if foo is empty %} |
||
| 31 | * {# ... #} |
||
| 32 | * {% endif %} |
||
| 33 | * |
||
| 34 | * @param mixed $value A variable |
||
| 35 | * |
||
| 36 | * @return bool true if the value is empty, false otherwise |
||
| 37 | */ |
||
| 38 | public static function empty($value): bool |
||
| 39 | { |
||
| 40 | if ($value instanceof \Countable) { |
||
| 41 | return 0 == \count($value); |
||
| 42 | } |
||
| 43 | |||
| 44 | if (\is_object($value) && method_exists($value, '__toString')) { |
||
| 45 | return '' === (string) $value; |
||
| 46 | } |
||
| 47 | |||
| 48 | return '' === $value || false === $value || null === $value || [] === $value; |
||
| 49 | } |
||
| 50 | |||
| 51 | public function __construct(Node $left, Node $right, int $lineno) |
||
|
0 ignored issues
–
show
|
|||
| 52 | { |
||
| 53 | $left->setAttribute('ignore_strict_check', true); |
||
| 54 | $left->setAttribute('is_defined_test', false); |
||
| 55 | |||
| 56 | $right->setAttribute('ignore_strict_check', true); |
||
| 57 | $right->setAttribute('is_defined_test', false); |
||
| 58 | parent::__construct( |
||
| 59 | ['left' => $left, 'right' => $right], |
||
| 60 | ['ignore_strict_check' => true, 'is_defined_test' => false], |
||
| 61 | $lineno |
||
| 62 | ); |
||
| 63 | } |
||
| 64 | |||
| 65 | public function compile(Compiler $compiler): void |
||
|
0 ignored issues
–
show
|
|||
| 66 | { |
||
| 67 | //$this->getNode('expr1')->setAttribute('always_defined', true); |
||
| 68 | $compiler |
||
| 69 | ->raw('((' . self::class . '::empty(') |
||
| 70 | ->subcompile($this->getNode('left')) |
||
| 71 | ->raw(') ? null : ') |
||
| 72 | ->subcompile($this->getNode('left')) |
||
| 73 | ->raw(') ?? (' . self::class . '::empty(') |
||
| 74 | ->subcompile($this->getNode('right')) |
||
| 75 | ->raw(') ? null : ') |
||
| 76 | ->subcompile($this->getNode('right')) |
||
| 77 | ->raw('))') |
||
|
0 ignored issues
–
show
|
|||
| 78 | ; |
||
| 79 | } |
||
| 80 | } |
||
| 81 |