1 | <?php |
||
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 | protected abstract 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 | protected final function getOption( $option ) { |
||
94 | |||
95 | /** |
||
96 | * Shortcut to $this->options->requireOption. |
||
97 | * |
||
98 | * @since 0.1 |
||
99 | * |
||
100 | * @param string $option |
||
101 | * |
||
102 | * @throws RuntimeException |
||
103 | */ |
||
104 | protected final function requireOption( $option ) { |
||
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 | protected final function defaultOption( $option, $default ) { |
|
119 | |||
120 | } |
||
121 |