-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathStringParser.php
More file actions
50 lines (41 loc) · 1 KB
/
StringParser.php
File metadata and controls
50 lines (41 loc) · 1 KB
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
<?php
declare( strict_types = 1 );
namespace ValueParsers;
use DataValues\StringValue;
use ValueParsers\Normalizers\NullStringNormalizer;
use ValueParsers\Normalizers\StringNormalizer;
/**
* Implementation of the ValueParser interface for StringValues.
*
* @since 0.3
*
* @license GPL-2.0-or-later
* @author Daniel Kinzler
*/
class StringParser implements ValueParser {
/**
* @var StringNormalizer
*/
private $normalizer;
/**
* @param StringNormalizer|null $normalizer
*/
public function __construct( ?StringNormalizer $normalizer = null ) {
$this->normalizer = $normalizer ?: new NullStringNormalizer();
}
/**
* @see ValueParser::parse
*
* @param string $value
*
* @throws ParseException if the provided value is not a string
* @return StringValue
*/
public function parse( $value ) {
if ( !is_string( $value ) ) {
throw new ParseException( 'Parameter $value must be a string' );
}
$value = $this->normalizer->normalize( $value );
return new StringValue( $value );
}
}