Unit.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Unit
  4. *
  5. * @package Less
  6. * @subpackage tree
  7. */
  8. class Less_Tree_Unit extends Less_Tree {
  9. var $numerator = array();
  10. var $denominator = array();
  11. public $backupUnit;
  12. public $type = 'Unit';
  13. public function __construct( $numerator = array(), $denominator = array(), $backupUnit = null ) {
  14. $this->numerator = $numerator;
  15. $this->denominator = $denominator;
  16. $this->backupUnit = $backupUnit;
  17. }
  18. public function __clone() {
  19. }
  20. /**
  21. * @see Less_Tree::genCSS
  22. */
  23. public function genCSS( $output ) {
  24. if ( $this->numerator ) {
  25. $output->add( $this->numerator[0] );
  26. } elseif ( $this->denominator ) {
  27. $output->add( $this->denominator[0] );
  28. } elseif ( !Less_Parser::$options['strictUnits'] && $this->backupUnit ) {
  29. $output->add( $this->backupUnit );
  30. return;
  31. }
  32. }
  33. public function toString() {
  34. $returnStr = implode( '*', $this->numerator );
  35. foreach ( $this->denominator as $d ) {
  36. $returnStr .= '/'.$d;
  37. }
  38. return $returnStr;
  39. }
  40. public function __toString() {
  41. return $this->toString();
  42. }
  43. /**
  44. * @param Less_Tree_Unit $other
  45. */
  46. public function compare( $other ) {
  47. return $this->is( $other->toString() ) ? 0 : -1;
  48. }
  49. public function is( $unitString ) {
  50. return $this->toString() === $unitString;
  51. }
  52. public function isLength() {
  53. $css = $this->toCSS();
  54. return !!preg_match( '/px|em|%|in|cm|mm|pc|pt|ex/', $css );
  55. }
  56. public function isAngle() {
  57. return isset( Less_Tree_UnitConversions::$angle[$this->toCSS()] );
  58. }
  59. public function isEmpty() {
  60. return !$this->numerator && !$this->denominator;
  61. }
  62. public function isSingular() {
  63. return count( $this->numerator ) <= 1 && !$this->denominator;
  64. }
  65. public function usedUnits() {
  66. $result = array();
  67. foreach ( Less_Tree_UnitConversions::$groups as $groupName ) {
  68. $group = Less_Tree_UnitConversions::${$groupName};
  69. foreach ( $this->numerator as $atomicUnit ) {
  70. if ( isset( $group[$atomicUnit] ) && !isset( $result[$groupName] ) ) {
  71. $result[$groupName] = $atomicUnit;
  72. }
  73. }
  74. foreach ( $this->denominator as $atomicUnit ) {
  75. if ( isset( $group[$atomicUnit] ) && !isset( $result[$groupName] ) ) {
  76. $result[$groupName] = $atomicUnit;
  77. }
  78. }
  79. }
  80. return $result;
  81. }
  82. public function cancel() {
  83. $counter = array();
  84. $backup = null;
  85. foreach ( $this->numerator as $atomicUnit ) {
  86. if ( !$backup ) {
  87. $backup = $atomicUnit;
  88. }
  89. $counter[$atomicUnit] = ( isset( $counter[$atomicUnit] ) ? $counter[$atomicUnit] : 0 ) + 1;
  90. }
  91. foreach ( $this->denominator as $atomicUnit ) {
  92. if ( !$backup ) {
  93. $backup = $atomicUnit;
  94. }
  95. $counter[$atomicUnit] = ( isset( $counter[$atomicUnit] ) ? $counter[$atomicUnit] : 0 ) - 1;
  96. }
  97. $this->numerator = array();
  98. $this->denominator = array();
  99. foreach ( $counter as $atomicUnit => $count ) {
  100. if ( $count > 0 ) {
  101. for ( $i = 0; $i < $count; $i++ ) {
  102. $this->numerator[] = $atomicUnit;
  103. }
  104. } elseif ( $count < 0 ) {
  105. for ( $i = 0; $i < -$count; $i++ ) {
  106. $this->denominator[] = $atomicUnit;
  107. }
  108. }
  109. }
  110. if ( !$this->numerator && !$this->denominator && $backup ) {
  111. $this->backupUnit = $backup;
  112. }
  113. sort( $this->numerator );
  114. sort( $this->denominator );
  115. }
  116. }