File: Unit.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 (136 lines) | stat: -rw-r--r-- 3,283 bytes parent folder | download | duplicates (2)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
 * @private
 * @see less-2.5.3.js#Unit.prototype
 */
class Less_Tree_Unit extends Less_Tree {

	public $numerator = [];
	public $denominator = [];
	public $backupUnit;

	public function __construct( $numerator = [], $denominator = [], $backupUnit = null ) {
		$this->numerator = $numerator;
		$this->denominator = $denominator;
		sort( $this->numerator );
		sort( $this->denominator );
		$this->backupUnit = $backupUnit ?? $numerator[0] ?? null;
	}

	public function clone() {
		// we are recreating a new object to trigger logic from constructor
		return new Less_Tree_Unit( $this->numerator, $this->denominator, $this->backupUnit );
	}

	/**
	 * @see Less_Tree::genCSS
	 */
	public function genCSS( $output ) {
		$strictUnits = Less_Parser::$options['strictUnits'];

		if ( count( $this->numerator ) === 1 ) {
			$output->add( $this->numerator[0] ); // the ideal situation
		} elseif ( !$strictUnits && $this->backupUnit ) {
			$output->add( $this->backupUnit );
		} elseif ( !$strictUnits && $this->denominator ) {
			$output->add( $this->denominator[0] );
		}
	}

	public function toString() {
		$returnStr = implode( '*', $this->numerator );
		foreach ( $this->denominator as $d ) {
			$returnStr .= '/' . $d;
		}
		return $returnStr;
	}

	public function __toString() {
		return $this->toString();
	}

	/**
	 * @param self $other
	 */
	public function compare( $other ) {
		return $this->is( $other->toString() ) ? 0 : -1;
	}

	public function is( $unitString ) {
		return strtoupper( $this->toString() ) === strtoupper( $unitString );
	}

	public function isLength() {
		$css = $this->toCSS();
		return (bool)preg_match( '/px|em|%|in|cm|mm|pc|pt|ex/', $css );
	}

	// TODO: Remove unused method
	public function isAngle() {
		return isset( Less_Tree_UnitConversions::$angle[$this->toCSS()] );
	}

	public function isEmpty() {
		return !$this->numerator && !$this->denominator;
	}

	public function isSingular() {
		return count( $this->numerator ) <= 1 && !$this->denominator;
	}

	public function usedUnits() {
		$result = [];

		foreach ( Less_Tree_UnitConversions::$groups as $groupName ) {
			$group = Less_Tree_UnitConversions::${$groupName};

			foreach ( $this->numerator as $atomicUnit ) {
				if ( isset( $group[$atomicUnit] ) && !isset( $result[$groupName] ) ) {
					$result[$groupName] = $atomicUnit;
				}
			}

			foreach ( $this->denominator as $atomicUnit ) {
				if ( isset( $group[$atomicUnit] ) && !isset( $result[$groupName] ) ) {
					$result[$groupName] = $atomicUnit;
				}
			}
		}

		return $result;
	}

	/**
	 * @see less-2.5.3.js#Unit.prototype.cancel
	 */
	public function cancel() {
		$counter = [];

		foreach ( $this->numerator as $atomicUnit ) {
			$counter[$atomicUnit] = ( $counter[$atomicUnit] ?? 0 ) + 1;
		}

		foreach ( $this->denominator as $atomicUnit ) {
			$counter[$atomicUnit] = ( $counter[$atomicUnit] ?? 0 ) - 1;
		}

		$this->numerator = [];
		$this->denominator = [];

		foreach ( $counter as $atomicUnit => $count ) {
			if ( $count > 0 ) {
				for ( $i = 0; $i < $count; $i++ ) {
					$this->numerator[] = $atomicUnit;
				}
			} elseif ( $count < 0 ) {
				for ( $i = 0; $i < -$count; $i++ ) {
					$this->denominator[] = $atomicUnit;
				}
			}
		}

		sort( $this->numerator );
		sort( $this->denominator );
	}

}