@@ -38,24 +38,24 @@ discard block |
||
38 | 38 | */ |
39 | 39 | class NumberInfo |
40 | 40 | { |
41 | - /** |
|
42 | - * @var mixed |
|
43 | - */ |
|
41 | + /** |
|
42 | + * @var mixed |
|
43 | + */ |
|
44 | 44 | protected $rawValue; |
45 | 45 | |
46 | - /** |
|
47 | - * @var array |
|
48 | - */ |
|
46 | + /** |
|
47 | + * @var array |
|
48 | + */ |
|
49 | 49 | protected $info; |
50 | 50 | |
51 | - /** |
|
52 | - * @var bool |
|
53 | - */ |
|
51 | + /** |
|
52 | + * @var bool |
|
53 | + */ |
|
54 | 54 | protected $empty = false; |
55 | 55 | |
56 | - /** |
|
57 | - * @var array |
|
58 | - */ |
|
56 | + /** |
|
57 | + * @var array |
|
58 | + */ |
|
59 | 59 | protected $knownUnits = array( |
60 | 60 | '%' => true, |
61 | 61 | 'rem' => true, |
@@ -95,21 +95,21 @@ discard block |
||
95 | 95 | return $this; |
96 | 96 | } |
97 | 97 | |
98 | - /** |
|
99 | - * Retrieves the raw, internal information array resulting |
|
100 | - * from the parsing of the number. |
|
101 | - * |
|
102 | - * @return array |
|
103 | - */ |
|
98 | + /** |
|
99 | + * Retrieves the raw, internal information array resulting |
|
100 | + * from the parsing of the number. |
|
101 | + * |
|
102 | + * @return array |
|
103 | + */ |
|
104 | 104 | public function getRawInfo() : array |
105 | 105 | { |
106 | 106 | return $this->info; |
107 | 107 | } |
108 | 108 | |
109 | - /** |
|
110 | - * Whether the number was empty (null or empty string). |
|
111 | - * @return boolean |
|
112 | - */ |
|
109 | + /** |
|
110 | + * Whether the number was empty (null or empty string). |
|
111 | + * @return boolean |
|
112 | + */ |
|
113 | 113 | public function isEmpty() : bool |
114 | 114 | { |
115 | 115 | return $this->empty; |
@@ -532,12 +532,12 @@ discard block |
||
532 | 532 | return $cache[$key]; |
533 | 533 | } |
534 | 534 | |
535 | - /** |
|
536 | - * Parses a string number notation with units included, e.g. 14px, 50%... |
|
537 | - * |
|
538 | - * @param string $test |
|
539 | - * @return array |
|
540 | - */ |
|
535 | + /** |
|
536 | + * Parses a string number notation with units included, e.g. 14px, 50%... |
|
537 | + * |
|
538 | + * @param string $test |
|
539 | + * @return array |
|
540 | + */ |
|
541 | 541 | private function parseStringValue(string $test) : array |
542 | 542 | { |
543 | 543 | $number = null; |
@@ -588,13 +588,13 @@ discard block |
||
588 | 588 | return $this->filterInfo($result); |
589 | 589 | } |
590 | 590 | |
591 | - /** |
|
592 | - * Attempts to determine what kind of units are specified |
|
593 | - * in the string. Returns NULL if none could be matched. |
|
594 | - * |
|
595 | - * @param string $value |
|
596 | - * @return array|NULL |
|
597 | - */ |
|
591 | + /** |
|
592 | + * Attempts to determine what kind of units are specified |
|
593 | + * in the string. Returns NULL if none could be matched. |
|
594 | + * |
|
595 | + * @param string $value |
|
596 | + * @return array|NULL |
|
597 | + */ |
|
598 | 598 | private function findUnits(string $value) : ?array |
599 | 599 | { |
600 | 600 | $vlength = strlen($value); |
@@ -622,12 +622,12 @@ discard block |
||
622 | 622 | return null; |
623 | 623 | } |
624 | 624 | |
625 | - /** |
|
626 | - * Creates the cache key for the specified value. |
|
627 | - * |
|
628 | - * @param mixed $value |
|
629 | - * @return string |
|
630 | - */ |
|
625 | + /** |
|
626 | + * Creates the cache key for the specified value. |
|
627 | + * |
|
628 | + * @param mixed $value |
|
629 | + * @return string |
|
630 | + */ |
|
631 | 631 | private function createValueKey($value) : string |
632 | 632 | { |
633 | 633 | if(!is_string($value) && !is_numeric($value)) |
@@ -640,59 +640,59 @@ discard block |
||
640 | 640 | |
641 | 641 | protected $postProcess = false; |
642 | 642 | |
643 | - /** |
|
644 | - * Called if explicitly enabled: allows filtering the |
|
645 | - * number after the detection process has completed. |
|
646 | - * |
|
647 | - * @param string|NULL $number The adjusted number |
|
648 | - * @param string $originalString The original value before it was parsed |
|
649 | - * @return mixed |
|
650 | - */ |
|
643 | + /** |
|
644 | + * Called if explicitly enabled: allows filtering the |
|
645 | + * number after the detection process has completed. |
|
646 | + * |
|
647 | + * @param string|NULL $number The adjusted number |
|
648 | + * @param string $originalString The original value before it was parsed |
|
649 | + * @return mixed |
|
650 | + */ |
|
651 | 651 | protected function postProcess(?string $number, /** @scrutinizer ignore-unused */ string $originalString) |
652 | 652 | { |
653 | 653 | return $number; |
654 | 654 | } |
655 | 655 | |
656 | - /** |
|
657 | - * Filters the value before it is parsed, but only if it is a string. |
|
658 | - * |
|
659 | - * NOTE: This may be overwritten in a subclass, to allow custom filtering |
|
660 | - * the the values. An example of a use case would be a preprocessor for |
|
661 | - * variables in a templating system. |
|
662 | - * |
|
663 | - * @param string $trimmedString The trimmed value. |
|
664 | - * @param array $cache The internal values cache array. |
|
665 | - * @param string $originalValue The original value that the NumberInfo was created for. |
|
666 | - * @return string |
|
667 | - * |
|
668 | - * @see NumberInfo::enablePostProcess() |
|
669 | - */ |
|
656 | + /** |
|
657 | + * Filters the value before it is parsed, but only if it is a string. |
|
658 | + * |
|
659 | + * NOTE: This may be overwritten in a subclass, to allow custom filtering |
|
660 | + * the the values. An example of a use case would be a preprocessor for |
|
661 | + * variables in a templating system. |
|
662 | + * |
|
663 | + * @param string $trimmedString The trimmed value. |
|
664 | + * @param array $cache The internal values cache array. |
|
665 | + * @param string $originalValue The original value that the NumberInfo was created for. |
|
666 | + * @return string |
|
667 | + * |
|
668 | + * @see NumberInfo::enablePostProcess() |
|
669 | + */ |
|
670 | 670 | protected function preProcess(string $trimmedString, /** @scrutinizer ignore-unused */ array &$cache, /** @scrutinizer ignore-unused */ string $originalValue) : string |
671 | 671 | { |
672 | 672 | return str_replace(',', '.', $trimmedString); |
673 | 673 | } |
674 | 674 | |
675 | - /** |
|
676 | - * Enables the post processing so the postProcess method gets called. |
|
677 | - * This should be called in the {@link NumberInfo::preProcess()} |
|
678 | - * method as needed. |
|
679 | - * |
|
680 | - * @return NumberInfo |
|
681 | - * @see NumberInfo::postProcess() |
|
682 | - */ |
|
675 | + /** |
|
676 | + * Enables the post processing so the postProcess method gets called. |
|
677 | + * This should be called in the {@link NumberInfo::preProcess()} |
|
678 | + * method as needed. |
|
679 | + * |
|
680 | + * @return NumberInfo |
|
681 | + * @see NumberInfo::postProcess() |
|
682 | + */ |
|
683 | 683 | private function enablePostProcess() : NumberInfo |
684 | 684 | { |
685 | 685 | $this->postProcess = true; |
686 | 686 | return $this; |
687 | 687 | } |
688 | 688 | |
689 | - /** |
|
690 | - * Filters the number info array to adjust the units |
|
691 | - * and number according to the required rules. |
|
692 | - * |
|
693 | - * @param array $info |
|
694 | - * @return array |
|
695 | - */ |
|
689 | + /** |
|
690 | + * Filters the number info array to adjust the units |
|
691 | + * and number according to the required rules. |
|
692 | + * |
|
693 | + * @param array $info |
|
694 | + * @return array |
|
695 | + */ |
|
696 | 696 | protected function filterInfo(array $info) : array |
697 | 697 | { |
698 | 698 | $useUnits = 'px'; |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | */ |
85 | 85 | public function setValue($value) : NumberInfo |
86 | 86 | { |
87 | - if($value instanceof NumberInfo) { |
|
87 | + if ($value instanceof NumberInfo) { |
|
88 | 88 | $value = $value->getValue(); |
89 | 89 | } |
90 | 90 | |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | |
118 | 118 | public function isPositive() : bool |
119 | 119 | { |
120 | - if(!$this->isEmpty()) { |
|
120 | + if (!$this->isEmpty()) { |
|
121 | 121 | $number = $this->getNumber(); |
122 | 122 | return $number > 0; |
123 | 123 | } |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | */ |
149 | 149 | public function hasValue() : bool |
150 | 150 | { |
151 | - if(!$this->isEmpty() && !$this->isZero()) { |
|
151 | + if (!$this->isEmpty() && !$this->isZero()) { |
|
152 | 152 | return true; |
153 | 153 | } |
154 | 154 | |
@@ -221,7 +221,7 @@ discard block |
||
221 | 221 | */ |
222 | 222 | public function getUnits() |
223 | 223 | { |
224 | - if(!$this->hasUnits()) { |
|
224 | + if (!$this->hasUnits()) { |
|
225 | 225 | return 'px'; |
226 | 226 | } |
227 | 227 | |
@@ -255,15 +255,15 @@ discard block |
||
255 | 255 | */ |
256 | 256 | public function toAttribute() |
257 | 257 | { |
258 | - if($this->isEmpty()) { |
|
258 | + if ($this->isEmpty()) { |
|
259 | 259 | return null; |
260 | 260 | } |
261 | 261 | |
262 | - if($this->isZero()) { |
|
262 | + if ($this->isZero()) { |
|
263 | 263 | return '0'; |
264 | 264 | } |
265 | 265 | |
266 | - if($this->isPercent()) { |
|
266 | + if ($this->isPercent()) { |
|
267 | 267 | return $this->getNumber().$this->getUnits(); |
268 | 268 | } |
269 | 269 | |
@@ -276,11 +276,11 @@ discard block |
||
276 | 276 | */ |
277 | 277 | public function toCSS() |
278 | 278 | { |
279 | - if($this->isEmpty()) { |
|
279 | + if ($this->isEmpty()) { |
|
280 | 280 | return null; |
281 | 281 | } |
282 | 282 | |
283 | - if($this->isZero()) { |
|
283 | + if ($this->isZero()) { |
|
284 | 284 | return '0'; |
285 | 285 | } |
286 | 286 | |
@@ -289,7 +289,7 @@ discard block |
||
289 | 289 | |
290 | 290 | public function __toString() |
291 | 291 | { |
292 | - if($this->isEmpty()) { |
|
292 | + if ($this->isEmpty()) { |
|
293 | 293 | return ''; |
294 | 294 | } |
295 | 295 | |
@@ -307,7 +307,7 @@ discard block |
||
307 | 307 | public function isBiggerThan($number) |
308 | 308 | { |
309 | 309 | $number = parseNumber($number); |
310 | - if($number->getUnits() != $this->getUnits()) { |
|
310 | + if ($number->getUnits() != $this->getUnits()) { |
|
311 | 311 | return false; |
312 | 312 | } |
313 | 313 | |
@@ -325,7 +325,7 @@ discard block |
||
325 | 325 | public function isSmallerThan($number) |
326 | 326 | { |
327 | 327 | $number = parseNumber($number); |
328 | - if($number->getUnits() != $this->getUnits()) { |
|
328 | + if ($number->getUnits() != $this->getUnits()) { |
|
329 | 329 | return false; |
330 | 330 | } |
331 | 331 | |
@@ -335,7 +335,7 @@ discard block |
||
335 | 335 | public function isBiggerEqual($number) |
336 | 336 | { |
337 | 337 | $number = parseNumber($number); |
338 | - if($number->getUnits() != $this->getUnits()) { |
|
338 | + if ($number->getUnits() != $this->getUnits()) { |
|
339 | 339 | return false; |
340 | 340 | } |
341 | 341 | |
@@ -352,14 +352,14 @@ discard block |
||
352 | 352 | */ |
353 | 353 | public function add($value) |
354 | 354 | { |
355 | - if($this->isEmpty()) { |
|
355 | + if ($this->isEmpty()) { |
|
356 | 356 | $this->setValue($value); |
357 | 357 | return $this; |
358 | 358 | } |
359 | 359 | |
360 | 360 | $number = parseNumber($value); |
361 | 361 | |
362 | - if($number->getUnits() == $this->getUnits() || !$number->hasUnits()) |
|
362 | + if ($number->getUnits() == $this->getUnits() || !$number->hasUnits()) |
|
363 | 363 | { |
364 | 364 | $new = $this->getNumber() + $number->getNumber(); |
365 | 365 | $this->setValue($new.$this->getUnits()); |
@@ -378,14 +378,14 @@ discard block |
||
378 | 378 | */ |
379 | 379 | public function subtract($value) |
380 | 380 | { |
381 | - if($this->isEmpty()) { |
|
381 | + if ($this->isEmpty()) { |
|
382 | 382 | $this->setValue($value); |
383 | 383 | return $this; |
384 | 384 | } |
385 | 385 | |
386 | 386 | $number = parseNumber($value); |
387 | 387 | |
388 | - if($number->getUnits() == $this->getUnits() || !$number->hasUnits()) |
|
388 | + if ($number->getUnits() == $this->getUnits() || !$number->hasUnits()) |
|
389 | 389 | { |
390 | 390 | $new = $this->getNumber() - $number->getNumber(); |
391 | 391 | $this->setValue($new.$this->getUnits()); |
@@ -412,25 +412,25 @@ discard block |
||
412 | 412 | |
413 | 413 | protected function percentOperation($operation, $percent) |
414 | 414 | { |
415 | - if($this->isZeroOrEmpty()) { |
|
415 | + if ($this->isZeroOrEmpty()) { |
|
416 | 416 | return $this; |
417 | 417 | } |
418 | 418 | |
419 | 419 | $percent = parseNumber($percent); |
420 | - if($percent->hasUnits() && !$percent->isPercent()) { |
|
420 | + if ($percent->hasUnits() && !$percent->isPercent()) { |
|
421 | 421 | return $this; |
422 | 422 | } |
423 | 423 | |
424 | 424 | $number = $this->getNumber(); |
425 | 425 | $value = $number * $percent->getNumber() / 100; |
426 | 426 | |
427 | - if($operation == '-') { |
|
427 | + if ($operation == '-') { |
|
428 | 428 | $number = $number - $value; |
429 | 429 | } else { |
430 | 430 | $number = $number + $value; |
431 | 431 | } |
432 | 432 | |
433 | - if($this->isUnitInteger()) { |
|
433 | + if ($this->isUnitInteger()) { |
|
434 | 434 | $number = intval($number); |
435 | 435 | } |
436 | 436 | |
@@ -481,7 +481,7 @@ discard block |
||
481 | 481 | |
482 | 482 | $key = $this->createValueKey($value); |
483 | 483 | |
484 | - if(array_key_exists($key, $cache)) { |
|
484 | + if (array_key_exists($key, $cache)) { |
|
485 | 485 | return $cache[$key]; |
486 | 486 | } |
487 | 487 | |
@@ -491,13 +491,13 @@ discard block |
||
491 | 491 | 'number' => null |
492 | 492 | ); |
493 | 493 | |
494 | - if($key === '_EMPTY_') |
|
494 | + if ($key === '_EMPTY_') |
|
495 | 495 | { |
496 | 496 | $cache[$key]['empty'] = true; |
497 | 497 | return $cache[$key]; |
498 | 498 | } |
499 | 499 | |
500 | - if($value === 0 || $value === '0') |
|
500 | + if ($value === 0 || $value === '0') |
|
501 | 501 | { |
502 | 502 | $cache[$key]['number'] = 0; |
503 | 503 | $cache[$key] = $this->filterInfo($cache[$key]); |
@@ -506,20 +506,20 @@ discard block |
||
506 | 506 | |
507 | 507 | $test = trim((string)$value); |
508 | 508 | |
509 | - if($test === '') |
|
509 | + if ($test === '') |
|
510 | 510 | { |
511 | 511 | $cache[$key]['empty'] = true; |
512 | 512 | return $cache[$key]; |
513 | 513 | } |
514 | 514 | |
515 | 515 | // replace comma notation (which is only possible if it's a string) |
516 | - if(is_string($value)) |
|
516 | + if (is_string($value)) |
|
517 | 517 | { |
518 | 518 | $test = $this->preProcess($test, $cache, $value); |
519 | 519 | } |
520 | 520 | |
521 | 521 | // convert to a number if it's numeric |
522 | - if(is_numeric($test)) |
|
522 | + if (is_numeric($test)) |
|
523 | 523 | { |
524 | 524 | $cache[$key]['number'] = $test * 1; |
525 | 525 | $cache[$key] = $this->filterInfo($cache[$key]); |
@@ -545,19 +545,19 @@ discard block |
||
545 | 545 | $empty = false; |
546 | 546 | |
547 | 547 | $found = $this->findUnits($test); |
548 | - if($found !== null) |
|
548 | + if ($found !== null) |
|
549 | 549 | { |
550 | 550 | $number = $found['number']; |
551 | 551 | $units = $found['units']; |
552 | 552 | } |
553 | 553 | |
554 | 554 | // the filters have to restore the value |
555 | - if($this->postProcess) |
|
555 | + if ($this->postProcess) |
|
556 | 556 | { |
557 | 557 | $number = $this->postProcess($number, $test); |
558 | 558 | } |
559 | 559 | // empty number |
560 | - else if($number === '' || $number === null || is_bool($number)) |
|
560 | + else if ($number === '' || $number === null || is_bool($number)) |
|
561 | 561 | { |
562 | 562 | $number = null; |
563 | 563 | $empty = true; |
@@ -568,7 +568,7 @@ discard block |
||
568 | 568 | $number = trim($number); |
569 | 569 | |
570 | 570 | // may be an arbitrary string in some cases |
571 | - if(!is_numeric($number)) |
|
571 | + if (!is_numeric($number)) |
|
572 | 572 | { |
573 | 573 | $number = null; |
574 | 574 | $empty = true; |
@@ -600,17 +600,17 @@ discard block |
||
600 | 600 | $vlength = strlen($value); |
601 | 601 | $names = array_keys($this->knownUnits); |
602 | 602 | |
603 | - foreach($names as $unit) |
|
603 | + foreach ($names as $unit) |
|
604 | 604 | { |
605 | 605 | $ulength = strlen($unit); |
606 | - $start = $vlength-$ulength; |
|
607 | - if($start < 0) { |
|
606 | + $start = $vlength - $ulength; |
|
607 | + if ($start < 0) { |
|
608 | 608 | continue; |
609 | 609 | } |
610 | 610 | |
611 | 611 | $search = substr($value, $start, $ulength); |
612 | 612 | |
613 | - if($search==$unit) |
|
613 | + if ($search == $unit) |
|
614 | 614 | { |
615 | 615 | return array( |
616 | 616 | 'units' => $unit, |
@@ -630,7 +630,7 @@ discard block |
||
630 | 630 | */ |
631 | 631 | private function createValueKey($value) : string |
632 | 632 | { |
633 | - if(!is_string($value) && !is_numeric($value)) |
|
633 | + if (!is_string($value) && !is_numeric($value)) |
|
634 | 634 | { |
635 | 635 | return '_EMPTY_'; |
636 | 636 | } |
@@ -696,12 +696,12 @@ discard block |
||
696 | 696 | protected function filterInfo(array $info) : array |
697 | 697 | { |
698 | 698 | $useUnits = 'px'; |
699 | - if($info['units'] !== null) { |
|
699 | + if ($info['units'] !== null) { |
|
700 | 700 | $useUnits = $info['units']; |
701 | 701 | } |
702 | 702 | |
703 | 703 | // the units are non-decimal: convert decimal values |
704 | - if($useUnits !== null && $this->knownUnits[$useUnits] === false && !$info['empty'] && is_numeric($info['number'])) |
|
704 | + if ($useUnits !== null && $this->knownUnits[$useUnits] === false && !$info['empty'] && is_numeric($info['number'])) |
|
705 | 705 | { |
706 | 706 | $info['number'] = intval($info['number']); |
707 | 707 | } |
@@ -572,8 +572,7 @@ |
||
572 | 572 | { |
573 | 573 | $number = null; |
574 | 574 | $empty = true; |
575 | - } |
|
576 | - else |
|
575 | + } else |
|
577 | 576 | { |
578 | 577 | $number = $number * 1; |
579 | 578 | } |