Debian Codegraph @

vendor/wikimedia/less.php/lib/Less/Tree/Condition.php

raw
1 <?php
2 /**
3 * @private
4 */
5 class Less_Tree_Condition extends Less_Tree {
6
7 /** @var string */
8 public $op;
9 /** @var Less_Tree */
10 public $lvalue;
11 /** @var Less_Tree */
12 public $rvalue;
13 /** @var int */
14 public $index;
15 /** @var bool */
16 public $negate;
17
18 public function __construct( $op, $l, $r, $i = 0, $negate = false ) {
19 $this->op = trim( $op );
20 $this->lvalue = $l;
21 $this->rvalue = $r;
22 $this->index = $i;
23 $this->negate = $negate;
24 }
25
26 public function accept( $visitor ) {
27 $this->lvalue = $visitor->visitObj( $this->lvalue );
28 $this->rvalue = $visitor->visitObj( $this->rvalue );
29 }
30
31 /**
32 * @param Less_Environment $env
33 * @return bool
34 * @see less-2.5.3.js#Condition.prototype.eval
35 */
36 public function compile( $env ) {
37 $a = $this->lvalue->compile( $env );
38 $b = $this->rvalue->compile( $env );
39
40 switch ( $this->op ) {
41 case 'and':
42 $result = $a && $b;
43 break;
44
45 case 'or':
46 $result = $a || $b;
47 break;
48
49 default:
50 $res = Less_Tree::nodeCompare( $a, $b );
51 // In JS, switch(undefined) with -1,0,-1,defaults goes to `default`.
52 // In PHP, switch(null) would go to case 0. Use if/else instead.
53 if ( $res === -1 ) {
54 $result = $this->op === '<' || $this->op === '=<' || $this->op === '<=';
55 } elseif ( $res === 0 ) {
56 $result = $this->op === '=' || $this->op === '>=' || $this->op === '=<' || $this->op === '<=';
57 } elseif ( $res === 1 ) {
58 $result = $this->op === '>' || $this->op === '>=';
59 } else {
60 // null, NAN
61 $result = false;
62 }
63 }
64
65 return $this->negate ? !$result : $result;
66 }
67
68 }