|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ValueParsers; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use RuntimeException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* ValueParser that parses the string representation of something. |
|
10
|
|
|
* |
|
11
|
|
|
* @since 0.1 |
|
12
|
|
|
* |
|
13
|
|
|
* @license GPL-2.0+ |
|
14
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class StringValueParser implements ValueParser { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @since 0.1 |
|
20
|
|
|
* |
|
21
|
|
|
* @var ParserOptions |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $options; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @since 0.1 |
|
27
|
|
|
* |
|
28
|
|
|
* @param ParserOptions|null $options |
|
29
|
|
|
*/ |
|
30
|
87 |
|
public function __construct( ParserOptions $options = null ) { |
|
31
|
87 |
|
$this->options = $options ?: new ParserOptions(); |
|
32
|
|
|
|
|
33
|
87 |
|
$this->defaultOption( ValueParser::OPT_LANG, 'en' ); |
|
34
|
87 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @see ValueParser::parse |
|
38
|
|
|
* |
|
39
|
|
|
* @param mixed $value |
|
40
|
|
|
* |
|
41
|
|
|
* @return mixed |
|
42
|
|
|
* @throws ParseException |
|
43
|
|
|
*/ |
|
44
|
84 |
|
public function parse( $value ) { |
|
45
|
84 |
|
if ( is_string( $value ) ) { |
|
46
|
66 |
|
return $this->stringParse( $value ); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
18 |
|
throw new ParseException( 'Not a string' ); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Parses the provided string and returns the result. |
|
54
|
|
|
* |
|
55
|
|
|
* @since 0.1 |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $value |
|
58
|
|
|
* |
|
59
|
|
|
* @return mixed |
|
60
|
|
|
*/ |
|
61
|
|
|
abstract protected function stringParse( $value ); |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @since 0.1 |
|
65
|
|
|
* |
|
66
|
|
|
* @param ParserOptions $options |
|
67
|
|
|
*/ |
|
68
|
3 |
|
public function setOptions( ParserOptions $options ) { |
|
69
|
3 |
|
$this->options = $options; |
|
70
|
3 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @since 0.1 |
|
74
|
|
|
* |
|
75
|
|
|
* @return ParserOptions |
|
76
|
|
|
*/ |
|
77
|
3 |
|
public function getOptions() { |
|
78
|
3 |
|
return $this->options; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Shortcut to $this->options->getOption. |
|
83
|
|
|
* |
|
84
|
|
|
* @since 0.1 |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $option |
|
87
|
|
|
* |
|
88
|
|
|
* @throws InvalidArgumentException |
|
89
|
|
|
* @return mixed |
|
90
|
|
|
*/ |
|
91
|
|
|
final protected function getOption( $option ) { |
|
92
|
|
|
return $this->options->getOption( $option ); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Shortcut to $this->options->requireOption. |
|
97
|
|
|
* |
|
98
|
|
|
* @since 0.1 |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $option |
|
101
|
|
|
* |
|
102
|
|
|
* @throws RuntimeException |
|
103
|
|
|
*/ |
|
104
|
|
|
final protected function requireOption( $option ) { |
|
105
|
|
|
$this->options->requireOption( $option ); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Shortcut to $this->options->defaultOption. |
|
110
|
|
|
* |
|
111
|
|
|
* @since 0.1 |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $option |
|
114
|
|
|
* @param mixed $default |
|
115
|
|
|
*/ |
|
116
|
87 |
|
final protected function defaultOption( $option, $default ) { |
|
117
|
87 |
|
$this->options->defaultOption( $option, $default ); |
|
118
|
87 |
|
} |
|
119
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|