Debian Codegraph @

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

raw
1 <?php
2 /**
3 * @private
4 * @see less-2.5.3.js#Unit.prototype
5 */
6 class Less_Tree_Unit extends Less_Tree {
7
8 /** @var string[] */
9 public $numerator = [];
10 /** @var string[] */
11 public $denominator = [];
12 /** @var string|null */
13 public $backupUnit;
14
15 public function __construct( $numerator = [], $denominator = [], $backupUnit = null ) {
16 $this->numerator = $numerator;
17 $this->denominator = $denominator;
18 sort( $this->numerator );
19 sort( $this->denominator );
20 $this->backupUnit = $backupUnit ?? $numerator[0] ?? null;
21 }
22
23 public function clone() {
24 // we are recreating a new object to trigger logic from constructor
25 return new Less_Tree_Unit( $this->numerator, $this->denominator, $this->backupUnit );
26 }
27
28 /**
29 * @see Less_Tree::genCSS
30 */
31 public function genCSS( $output ) {
32 $strictUnits = Less_Parser::$options['strictUnits'];
33
34 if ( count( $this->numerator ) === 1 ) {
35 $output->add( $this->numerator[0] ); // the ideal situation
36 } elseif ( !$strictUnits && $this->backupUnit ) {
37 $output->add( $this->backupUnit );
38 } elseif ( !$strictUnits && $this->denominator ) {
39 $output->add( $this->denominator[0] );
40 }
41 }
42
43 public function toString() {
44 $returnStr = implode( '*', $this->numerator );
45 foreach ( $this->denominator as $d ) {
46 $returnStr .= '/' . $d;
47 }
48 return $returnStr;
49 }
50
51 public function __toString() {
52 return $this->toString();
53 }
54
55 /**
56 * @param self $other
57 */
58 public function compare( $other ) {
59 return $this->is( $other->toString() ) ? 0 : -1;
60 }
61
62 public function is( $unitString ) {
63 return strtoupper( $this->toString() ) === strtoupper( $unitString );
64 }
65
66 public function isLength() {
67 $css = $this->toCSS();
68 return (bool)preg_match( '/px|em|%|in|cm|mm|pc|pt|ex/', $css );
69 }
70
71 // TODO: Remove unused method
72 public function isAngle() {
73 return isset( Less_Tree_UnitConversions::$angle[$this->toCSS()] );
74 }
75
76 public function isEmpty() {
77 return !$this->numerator && !$this->denominator;
78 }
79
80 public function isSingular() {
81 return count( $this->numerator ) <= 1 && !$this->denominator;
82 }
83
84 public function usedUnits() {
85 $result = [];
86
87 foreach ( Less_Tree_UnitConversions::$groups as $groupName ) {
88 $group = Less_Tree_UnitConversions::${$groupName};
89
90 foreach ( $this->numerator as $atomicUnit ) {
91 if ( isset( $group[$atomicUnit] ) && !isset( $result[$groupName] ) ) {
92 $result[$groupName] = $atomicUnit;
93 }
94 }
95
96 foreach ( $this->denominator as $atomicUnit ) {
97 if ( isset( $group[$atomicUnit] ) && !isset( $result[$groupName] ) ) {
98 $result[$groupName] = $atomicUnit;
99 }
100 }
101 }
102
103 return $result;
104 }
105
106 /**
107 * @see less-2.5.3.js#Unit.prototype.cancel
108 */
109 public function cancel() {
110 $counter = [];
111
112 foreach ( $this->numerator as $atomicUnit ) {
113 $counter[$atomicUnit] = ( $counter[$atomicUnit] ?? 0 ) + 1;
114 }
115
116 foreach ( $this->denominator as $atomicUnit ) {
117 $counter[$atomicUnit] = ( $counter[$atomicUnit] ?? 0 ) - 1;
118 }
119
120 $this->numerator = [];
121 $this->denominator = [];
122
123 foreach ( $counter as $atomicUnit => $count ) {
124 if ( $count > 0 ) {
125 for ( $i = 0; $i < $count; $i++ ) {
126 $this->numerator[] = $atomicUnit;
127 }
128 } elseif ( $count < 0 ) {
129 for ( $i = 0; $i < -$count; $i++ ) {
130 $this->denominator[] = $atomicUnit;
131 }
132 }
133 }
134
135 sort( $this->numerator );
136 sort( $this->denominator );
137 }
138
139 }