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 (30 loc) · 698 Bytes
/
IntParser.php
File metadata and controls
37 lines (30 loc) · 698 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
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class IntParser extends StringValueParser {
/**
* @see StringValueParser::stringParse
*
* @since 0.1
*
* @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' );
}
}