Normalizer::getNormalizerClassName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * Normalizer class.
6
 *
7
 * @package   YetiForcePDF\Style\Normalizer
8
 *
9
 * @copyright YetiForce Sp. z o.o
10
 * @license   MIT
11
 * @author    Rafal Pospiech <[email protected]>
12
 */
13
14
namespace YetiForcePDF\Style\Normalizer;
15
16
use YetiForcePDF\Style\NumericValue;
17
use YetiForcePDF\Style\Style;
18
19
/**
20
 * Class Normalizer.
21
 */
22
class Normalizer extends \YetiForcePDF\Base
23
{
24
	/**
25
	 * @var Style
26
	 */
27
	protected $style;
28
	/**
29
	 * @var array|null normalized value
30
	 */
31
	protected $normalized;
32
	/**
33
	 * Regex used to parse numeric values.
34
	 *
35
	 * @var string
36
	 */
37
	public static $numericRegex = '/(([0-9.?]+)([a-z%]+)?\s?)/ui';
38
39
	/**
40
	 * Set style.
41
	 *
42
	 * @param \YetiForcePDF\Style\Style $style
43
	 *
44
	 * @return $this
45
	 */
46
	public function setStyle(Style $style)
47
	{
48
		$this->style = $style;
49
		return $this;
50
	}
51
52
	/**
53
	 * Get normalizer class name.
54
	 *
55
	 * @param string $ruleName
56
	 *
57
	 * @return string
58
	 */
59
	public static function getNormalizerClassName(string $ruleName)
60
	{
61
		$ucRuleName = str_replace('-', '', ucwords($ruleName, '-'));
62
		$normalizerClassName = "YetiForcePDF\\Style\\Normalizer\\$ucRuleName";
63
		if (class_exists($normalizerClassName)) {
64
			return $normalizerClassName;
65
		}
66
		return 'YetiForcePDF\\Style\\Normalizer\\Normalizer';
67
	}
68
69
	/**
70
	 * Get number value from style.
71
	 *
72
	 * @param NumericValue|string $ruleValue
73
	 * @param bool                $isFont
74
	 *
75
	 * @return string[]
76
	 */
77
	public function getNumberValues($ruleValue, bool $isFont = false)
78
	{
79
		if ($ruleValue instanceof NumericValue) {
80
			return $ruleValue;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ruleValue returns the type YetiForcePDF\Style\NumericValue which is incompatible with the documented return type string[].
Loading history...
81
		}
82
		preg_match_all(static::$numericRegex, $ruleValue, $matches, PREG_SET_ORDER);
83
		if (!$matches) {
84
			$matches = [['0', '0', '0']];
85
		}
86
		$originalSize = $matches[0][2];
87
		$originalUnit = 'em';
88
		if (isset($matches[0][3])) {
89
			$originalUnit = $matches[0][3];
90
		}
91
		$multi = [
92
			(new NumericValue())
93
				->setUnit($originalUnit)
94
				->setValue($originalSize)
95
				->setOriginal($originalSize . $originalUnit)
96
				->setIsFont($isFont)
97
				->convert($this->style),
98
		];
99
		$matchesCount = \count($matches);
100
		if ($matchesCount >= 2) {
101
			$multi[] = (new NumericValue())
102
				->setUnit($matches[1][3] ?? $originalUnit)
103
				->setValue($matches[1][2])
104
				->setOriginal($matches[1][2] . ($matches[1][3] ?? $originalUnit))
105
				->setIsFont($isFont)
106
				->convert($this->style);
107
		}
108
		if ($matchesCount >= 3) {
109
			$multi[] = (new NumericValue())
110
				->setUnit($matches[2][3] ?? $originalUnit)
111
				->setValue($matches[2][2])
112
				->setOriginal($matches[2][2] . ($matches[2][3] ?? $originalUnit))
113
				->setIsFont($isFont)
114
				->convert($this->style);
115
		}
116
		if (4 === $matchesCount) {
117
			$multi[] = (new NumericValue())
118
				->setUnit($matches[3][3] ?? $originalUnit)
119
				->setValue($matches[3][2])
120
				->setOriginal($matches[3][2] . ($matches[3][3] ?? $originalUnit))
121
				->setIsFont($isFont)
122
				->convert($this->style);
123
		}
124
		return $multi;
125
	}
126
127
	/**
128
	 * Get numeric unit from css value.
129
	 *
130
	 * @param string $value       value from css 12px for example
131
	 * @param string $defaultUnit
132
	 *
133
	 * @return string
134
	 */
135
	public static function getNumericUnit(string $value, string $defaultUnit): string
136
	{
137
		$matches = [];
138
		preg_match_all(static::$numericRegex, $value, $matches, PREG_SET_ORDER);
139
		$unit = $defaultUnit;
140
		if (isset($matches[0][3])) {
141
			$unit = $matches[0][3];
142
		}
143
		return $unit;
144
	}
145
146
	/**
147
	 * Is numeric value.
148
	 *
149
	 * @param string $value
150
	 *
151
	 * @return bool|string
152
	 */
153
	public static function getNumericValue(string $value)
154
	{
155
		$matches = [];
156
		preg_match_all(static::$numericRegex, $value, $matches, PREG_SET_ORDER);
157
		if (isset($matches[0][2])) {
158
			return $matches[0][2];
159
		}
160
		return false;
161
	}
162
163
	/**
164
	 * Normalize css rule.
165
	 *
166
	 * @param mixed  $ruleValue
167
	 * @param string $ruleName
168
	 *
169
	 * @return array
170
	 */
171
	public function normalize($ruleValue, string $ruleName = ''): array
172
	{
173
		return [$ruleName => $ruleValue];
174
	}
175
176
	/**
177
	 * One value.
178
	 *
179
	 * @param array $ruleNames
180
	 * @param array $numberValues
181
	 *
182
	 * @return array
183
	 */
184
	protected function oneValue(array $ruleNames, array $numberValues)
185
	{
186
		$normalized = [];
187
		$normalized[$ruleNames[0]] = $numberValues[0];
188
		$normalized[$ruleNames[1]] = $numberValues[0];
189
		$normalized[$ruleNames[2]] = $numberValues[0];
190
		$normalized[$ruleNames[3]] = $numberValues[0];
191
		return $normalized;
192
	}
193
194
	/**
195
	 * Two values.
196
	 *
197
	 * @param array $ruleNames
198
	 * @param array $numberValues
199
	 *
200
	 * @return array
201
	 */
202
	protected function twoValues(array $ruleNames, array $numberValues)
203
	{
204
		$normalized = [];
205
		$normalized[$ruleNames[0]] = $numberValues[0];
206
		$normalized[$ruleNames[1]] = $numberValues[1];
207
		$normalized[$ruleNames[2]] = $numberValues[0];
208
		$normalized[$ruleNames[3]] = $numberValues[1];
209
		return $normalized;
210
	}
211
212
	/**
213
	 * Three values.
214
	 *
215
	 * @param array $ruleNames
216
	 * @param array $numberValues
217
	 *
218
	 * @return array
219
	 */
220
	protected function threeValues(array $ruleNames, array $numberValues)
221
	{
222
		$normalized = [];
223
		$normalized[$ruleNames[0]] = $numberValues[0];
224
		$normalized[$ruleNames[1]] = $numberValues[1];
225
		$normalized[$ruleNames[2]] = $numberValues[2];
226
		$normalized[$ruleNames[3]] = $numberValues[1];
227
		return $normalized;
228
	}
229
230
	/**
231
	 * Four values.
232
	 *
233
	 * @param array $ruleNames
234
	 * @param array $numberValues
235
	 *
236
	 * @return array
237
	 */
238
	protected function fourValues(array $ruleNames, array $numberValues)
239
	{
240
		$normalized = [];
241
		$normalized[$ruleNames[0]] = $numberValues[0];
242
		$normalized[$ruleNames[1]] = $numberValues[1];
243
		$normalized[$ruleNames[2]] = $numberValues[2];
244
		$normalized[$ruleNames[3]] = $numberValues[3];
245
		return $normalized;
246
	}
247
248
	/**
249
	 * Normalize multi number values.
250
	 *
251
	 * @param string[] $ruleNames ['margin-top','margin-right','margin-bottom','margin-left']
252
	 * @param string   $ruleValue
253
	 *                            return array
254
	 */
255
	public function normalizeMultiValues(array $ruleNames, $ruleValue): array
256
	{
257
		$numberValues = $this->getNumberValues($ruleValue);
258
		switch (\count($numberValues)) {
259
			case 1:
260
				return $this->oneValue($ruleNames, $numberValues);
261
			case 2:
262
				return $this->twoValues($ruleNames, $numberValues);
263
			case 3:
264
				return $this->threeValues($ruleNames, $numberValues);
265
			case 4:
0 ignored issues
show
introduced by
The function implicitly returns null when this case condition does not match. This is incompatible with the type-hinted return array. Consider adding a default case to the switch.
Loading history...
266
				return $this->fourValues($ruleNames, $numberValues);
267
		}
268
	}
269
}
270