File: Condition.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (63 lines) | stat: -rw-r--r-- 1,481 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/**
 * @private
 */
class Less_Tree_Condition extends Less_Tree {

	public $op;
	public $lvalue;
	public $rvalue;
	public $index;
	public $negate;

	public function __construct( $op, $l, $r, $i = 0, $negate = false ) {
		$this->op = trim( $op );
		$this->lvalue = $l;
		$this->rvalue = $r;
		$this->index = $i;
		$this->negate = $negate;
	}

	public function accept( $visitor ) {
		$this->lvalue = $visitor->visitObj( $this->lvalue );
		$this->rvalue = $visitor->visitObj( $this->rvalue );
	}

	/**
	 * @param Less_Environment $env
	 * @return bool
	 * @see less-2.5.3.js#Condition.prototype.eval
	 */
	public function compile( $env ) {
		$a = $this->lvalue->compile( $env );
		$b = $this->rvalue->compile( $env );

		switch ( $this->op ) {
			case 'and':
				$result = $a && $b;
				break;

			case 'or':
				$result = $a || $b;
				break;

			default:
				$res = Less_Tree::nodeCompare( $a, $b );
				// In JS, switch(undefined) with -1,0,-1,defaults goes to `default`.
				// In PHP, switch(null) would go to case 0. Use if/else instead.
				if ( $res === -1 ) {
					$result = $this->op === '<' || $this->op === '=<' || $this->op === '<=';
				} elseif ( $res === 0 ) {
					$result = $this->op === '=' || $this->op === '>=' || $this->op === '=<' || $this->op === '<=';
				} elseif ( $res === 1 ) {
					$result = $this->op === '>' || $this->op === '>=';
				} else {
					// null, NAN
					$result = false;
				}
		}

		return $this->negate ? !$result : $result;
	}

}