Passed
Push — trimNormalizer ( b2284d...0e0038 )
by no
05:06
created

StringValueParser   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 76.19%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 86
ccs 16
cts 21
cp 0.7619
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A parse() 0 7 2
stringParse() 0 1 ?
A setOptions() 0 3 1
A getOptions() 0 3 1
A getOption() 0 3 1
A requireOption() 0 3 1
A defaultOption() 0 3 1
1
<?php
2
3
namespace ValueParsers;
4
5
use InvalidArgumentException;
6
use RuntimeException;
7
8
/**
9
 * Basic implementation for DataValue parsers that share one or more of the following aspects:
10
 * - The provided input must be a string.
11
 * - The parser utilizes ParserOptions.
12
 * - The parser utilizes a "lang" option, which defaults to "en".
13
 *
14
 * @since 0.1
15
 *
16
 * @license GPL-2.0+
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
abstract class StringValueParser implements ValueParser {
20
21
	/**
22
	 * @var ParserOptions
23
	 */
24
	protected $options;
25
26
	/**
27
	 * @param ParserOptions|null $options
28
	 */
29 87
	public function __construct( ParserOptions $options = null ) {
30 87
		$this->options = $options ?: new ParserOptions();
31
32 87
		$this->defaultOption( ValueParser::OPT_LANG, 'en' );
33 87
	}
34
35
	/**
36
	 * @see ValueParser::parse
37
	 *
38
	 * @param string $value
39
	 *
40
	 * @return mixed
41
	 * @throws ParseException if the provided value is not a string
42
	 */
43 84
	public function parse( $value ) {
44 84
		if ( is_string( $value ) ) {
45 66
			return $this->stringParse( $value );
46
		}
47
48 18
		throw new ParseException( 'Not a string' );
49
	}
50
51
	/**
52
	 * Parses the provided string and returns the result.
53
	 *
54
	 * @param string $value
55
	 *
56
	 * @return mixed
57
	 */
58
	abstract protected function stringParse( $value );
59
60 3
	public function setOptions( ParserOptions $options ) {
61 3
		$this->options = $options;
62 3
	}
63
64
	/**
65
	 * @return ParserOptions
66
	 */
67 3
	public function getOptions() {
68 3
		return $this->options;
69
	}
70
71
	/**
72
	 * Shortcut to $this->options->getOption.
73
	 *
74
	 * @param string $option
75
	 *
76
	 * @throws InvalidArgumentException
77
	 * @return mixed
78
	 */
79
	final protected function getOption( $option ) {
80
		return $this->options->getOption( $option );
81
	}
82
83
	/**
84
	 * Shortcut to $this->options->requireOption.
85
	 *
86
	 * @param string $option
87
	 *
88
	 * @throws RuntimeException
89
	 */
90
	final protected function requireOption( $option ) {
91
		$this->options->requireOption( $option );
92
	}
93
94
	/**
95
	 * Shortcut to $this->options->defaultOption.
96
	 *
97
	 * @param string $option
98
	 * @param mixed $default
99
	 */
100 87
	final protected function defaultOption( $option, $default ) {
101 87
		$this->options->defaultOption( $option, $default );
102 87
	}
103
104
}
105