forked from DataValues/Common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringParser.php
More file actions
49 lines (41 loc) · 1 KB
/
StringParser.php
File metadata and controls
49 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
<?php
namespace ValueParsers;
use DataValues\StringValue;
use InvalidArgumentException;
use ValueParsers\Normalizers\NullStringNormalizer;
use ValueParsers\Normalizers\StringNormalizer;
/**
* Implementation of the ValueParser interface for StringValues.
*
* @since 0.3
*
* @license GPL-2.0+
* @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 InvalidArgumentException if $value is not a string
* @return StringValue
*/
public function parse( $value ) {
if ( !is_string( $value ) ) {
throw new InvalidArgumentException( 'Parameter $value must be a string' );
}
$value = $this->normalizer->normalize( $value );
return new StringValue( $value );
}
}