Operation.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Operation
  4. *
  5. * @package Less
  6. * @subpackage tree
  7. */
  8. class Less_Tree_Operation extends Less_Tree {
  9. public $op;
  10. public $operands;
  11. public $isSpaced;
  12. public $type = 'Operation';
  13. /**
  14. * @param string $op
  15. */
  16. public function __construct( $op, $operands, $isSpaced = false ) {
  17. $this->op = trim( $op );
  18. $this->operands = $operands;
  19. $this->isSpaced = $isSpaced;
  20. }
  21. public function accept( $visitor ) {
  22. $this->operands = $visitor->visitArray( $this->operands );
  23. }
  24. public function compile( $env ) {
  25. $a = $this->operands[0]->compile( $env );
  26. $b = $this->operands[1]->compile( $env );
  27. if ( Less_Environment::isMathOn() ) {
  28. if ( $a instanceof Less_Tree_Dimension && $b instanceof Less_Tree_Color ) {
  29. $a = $a->toColor();
  30. } elseif ( $b instanceof Less_Tree_Dimension && $a instanceof Less_Tree_Color ) {
  31. $b = $b->toColor();
  32. }
  33. if ( !method_exists( $a, 'operate' ) ) {
  34. throw new Less_Exception_Compiler( "Operation on an invalid type" );
  35. }
  36. return $a->operate( $this->op, $b );
  37. }
  38. return new Less_Tree_Operation( $this->op, array( $a, $b ), $this->isSpaced );
  39. }
  40. /**
  41. * @see Less_Tree::genCSS
  42. */
  43. public function genCSS( $output ) {
  44. $this->operands[0]->genCSS( $output );
  45. if ( $this->isSpaced ) {
  46. $output->add( " " );
  47. }
  48. $output->add( $this->op );
  49. if ( $this->isSpaced ) {
  50. $output->add( ' ' );
  51. }
  52. $this->operands[1]->genCSS( $output );
  53. }
  54. }