forked from DataValues/Common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloatParser.php
More file actions
37 lines (30 loc) · 740 Bytes
/
FloatParser.php
File metadata and controls
37 lines (30 loc) · 740 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 a float.
*
* @since 0.1
*
* @license GPL-2.0+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class FloatParser extends StringValueParser {
const FORMAT_NAME = 'float';
/**
* @see StringValueParser::stringParse
*
* TODO: add options for different group and decimal separators.
*
* @param string $value
*
* @return NumberValue
* @throws ParseException
*/
protected function stringParse( $value ) {
if ( preg_match( '/^(-)?\d+((\.|,)\d+)?$/', $value ) ) {
return new NumberValue( (float)$value );
}
throw new ParseException( 'Not a float', $value, self::FORMAT_NAME );
}
}