forked from DataValues/Common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntParser.php
More file actions
37 lines (29 loc) · 732 Bytes
/
IntParser.php
File metadata and controls
37 lines (29 loc) · 732 Bytes
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
<?php
namespace ValueParsers;
use DataValues\NumberValue;
/**
* ValueParser that parses the string representation of an integer.
*
* @since 0.1
*
* @license GPL-2.0+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class IntParser extends StringValueParser {
const FORMAT_NAME = 'int';
/**
* @see StringValueParser::stringParse
*
* @param string $value
*
* @return NumberValue
* @throws ParseException
*/
protected function stringParse( $value ) {
$positiveValue = strpos( $value, '-' ) === 0 ? substr( $value, 1 ) : $value;
if ( ctype_digit( $positiveValue ) ) {
return new NumberValue( (int)$value );
}
throw new ParseException( 'Not an integer', $value, self::FORMAT_NAME );
}
}