Condition.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Condition
  4. *
  5. * @package Less
  6. * @subpackage tree
  7. */
  8. class Less_Tree_Condition extends Less_Tree {
  9. public $op;
  10. public $lvalue;
  11. public $rvalue;
  12. public $index;
  13. public $negate;
  14. public $type = 'Condition';
  15. public function __construct( $op, $l, $r, $i = 0, $negate = false ) {
  16. $this->op = trim( $op );
  17. $this->lvalue = $l;
  18. $this->rvalue = $r;
  19. $this->index = $i;
  20. $this->negate = $negate;
  21. }
  22. public function accept( $visitor ) {
  23. $this->lvalue = $visitor->visitObj( $this->lvalue );
  24. $this->rvalue = $visitor->visitObj( $this->rvalue );
  25. }
  26. public function compile( $env ) {
  27. $a = $this->lvalue->compile( $env );
  28. $b = $this->rvalue->compile( $env );
  29. switch ( $this->op ) {
  30. case 'and':
  31. $result = $a && $b;
  32. break;
  33. case 'or':
  34. $result = $a || $b;
  35. break;
  36. default:
  37. if ( Less_Parser::is_method( $a, 'compare' ) ) {
  38. $result = $a->compare( $b );
  39. } elseif ( Less_Parser::is_method( $b, 'compare' ) ) {
  40. $result = $b->compare( $a );
  41. } else {
  42. throw new Less_Exception_Compiler( 'Unable to perform comparison', null, $this->index );
  43. }
  44. switch ( $result ) {
  45. case -1:
  46. $result = $this->op === '<' || $this->op === '=<' || $this->op === '<=';
  47. break;
  48. case 0:
  49. $result = $this->op === '=' || $this->op === '>=' || $this->op === '=<' || $this->op === '<=';
  50. break;
  51. case 1:
  52. $result = $this->op === '>' || $this->op === '>=';
  53. break;
  54. }
  55. break;
  56. }
  57. return $this->negate ? !$result : $result;
  58. }
  59. }