blob: 06aa2ad5bc9a61478410b9263a13f5b7c6733ef9 (
plain)
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
|
<?php
namespace ParamProcessor\PackagePrivate;
use ValueParsers\ParseException;
use ValueParsers\ParserOptions;
use ValueParsers\ValueParser;
/**
* Package private
*/
class DimensionParser implements ValueParser {
public const DEFAULT_UNIT = 'defaultunit';
public const PIXELS = 'px';
private $defaultUnit;
public function __construct( ParserOptions $options = null ) {
$options = $options ?? new ParserOptions();
$this->defaultUnit = $options->hasOption( self::DEFAULT_UNIT ) ? $options->getOption( self::DEFAULT_UNIT ) : self::PIXELS;
}
public function parse( $value ) {
if ( !is_string( $value ) ) {
throw new ParseException( 'Not a string' );
}
$value = $this->removeWhitespace( $value );
if ( preg_match( '/^(\d|\.)+$/', $value ) ) {
$value .= $this->defaultUnit;
}
return $value;
}
private function removeWhitespace( string $string ): string {
return preg_replace( '/\s+/', '', $string );
}
}
|