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, $lineno) |
||
0 ignored issues
–
show
|
|||
52 | { |
||
53 | $left->setAttribute('ignore_strict_check', true); |
||
54 | $left->setAttribute('is_defined_test', false); |
||
55 | $right->setAttribute('ignore_strict_check', true); |
||
56 | $right->setAttribute('is_defined_test', false); |
||
57 | parent::__construct( |
||
58 | ['left' => $left, 'right' => $right], |
||
59 | ['ignore_strict_check' => true, 'is_defined_test' => false], |
||
60 | $lineno |
||
61 | ); |
||
62 | } |
||
63 | |||
64 | public function compile(Compiler $compiler) |
||
0 ignored issues
–
show
|
|||
65 | { |
||
66 | //$this->getNode('expr1')->setAttribute('always_defined', true); |
||
67 | $compiler |
||
68 | ->raw('((' . self::class . '::empty(') |
||
69 | ->subcompile($this->getNode('left')) |
||
70 | ->raw(') ? null : ') |
||
71 | ->subcompile($this->getNode('left')) |
||
72 | ->raw(') ?? (' . self::class . '::empty(') |
||
73 | ->subcompile($this->getNode('right')) |
||
74 | ->raw(') ? null : ') |
||
75 | ->subcompile($this->getNode('right')) |
||
76 | ->raw('))') |
||
0 ignored issues
–
show
|
|||
77 | ; |
||
78 | } |
||
79 | } |
||
80 |