-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBoolParser.php
More file actions
56 lines (45 loc) · 1.02 KB
/
BoolParser.php
File metadata and controls
56 lines (45 loc) · 1.02 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
51
52
53
54
55
56
<?php
declare( strict_types = 1 );
namespace ValueParsers;
use DataValues\BooleanValue;
/**
* ValueParser that parses the string representation of a boolean.
*
* @since 0.1
*
* @license GPL-2.0-or-later
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class BoolParser extends StringValueParser {
private const FORMAT_NAME = 'bool';
/**
* @var Mapping from possible string values to their
* boolean equivalents
*/
private static $values = [
'yes' => true,
'on' => true,
'1' => true,
'true' => true,
'no' => false,
'off' => false,
'0' => false,
'false' => false,
];
/**
* @see StringValueParser::stringParse
*
* @param string $value
*
* @return BooleanValue
* @throws ParseException
*/
protected function stringParse( $value ) {
$rawValue = $value;
$value = strtolower( $value );
if ( array_key_exists( $value, self::$values ) ) {
return new BooleanValue( self::$values[$value] );
}
throw new ParseException( 'Not a boolean', $rawValue, self::FORMAT_NAME );
}
}