From 11407539c9406bd295d47ebebff1e69557628cc1 Mon Sep 17 00:00:00 2001 From: addshore Date: Mon, 10 Mar 2014 18:16:17 +0100 Subject: [PATCH 1/2] Add precision and claandar opts to TimeParser --- README.md | 1 + src/DataValues/TimeValue.php | 2 +- src/ValueParsers/TimeParser.php | 41 +++- tests/ValueParsers/TimeParserTest.php | 288 +++++++++++++++++++------- 4 files changed, 245 insertions(+), 87 deletions(-) diff --git a/README.md b/README.md index 0d3ee74..e58743b 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ DataValues Time has been written by the Wikidata team, as [Wikimedia Germany] ### 0.3 (dev) * Renamed CalenderModelParser to CalendarModelParser +* Added Calandar and Precision options to TimeParser ### 0.2 (2014-02-11) diff --git a/src/DataValues/TimeValue.php b/src/DataValues/TimeValue.php index 7c685a1..61ed490 100644 --- a/src/DataValues/TimeValue.php +++ b/src/DataValues/TimeValue.php @@ -105,7 +105,7 @@ public function __construct( $time, $timezone, $before, $after, $precision, $cal } if ( !preg_match( '!^[-+]\d{1,16}-(0\d|1[012])-([012]\d|3[01])T([01]\d|2[0123]):[0-5]\d:([0-5]\d|6[012])Z$!', $time ) ) { - throw new IllegalValueException( '$time needs to be a valid ISO 8601 date' ); + throw new IllegalValueException( '$time needs to be a valid ISO 8601 date, given ' . $time ); } if ( !is_integer( $timezone ) ) { diff --git a/src/ValueParsers/TimeParser.php b/src/ValueParsers/TimeParser.php index dc2d853..77c1717 100644 --- a/src/ValueParsers/TimeParser.php +++ b/src/ValueParsers/TimeParser.php @@ -16,6 +16,19 @@ */ class TimeParser extends StringValueParser { + /** + * @since 0.3 + */ + const OPT_PRECISION = 'precision'; + const OPT_CALENDAR = 'calender'; + + /** + * @since 0.3 + */ + const OPT_CALENDAR_GREGORIAN = 'gregorian'; + const OPT_CALENDER_JULIAN = 'julian'; + const OPT_PRECISION_NONE = 'noprecision'; + /** * Regex pattern constant matching the sign preceding the time */ @@ -37,18 +50,33 @@ class TimeParser extends StringValueParser { * @param ParserOptions|null $options */ public function __construct( CalendarModelParser $calendarModelParser, ParserOptions $options = null ) { + + $options->defaultOption( TimeParser::OPT_CALENDAR, TimeParser::OPT_CALENDAR_GREGORIAN ); + $options->defaultOption( TimeParser::OPT_PRECISION, TimeParser::OPT_PRECISION_NONE ); + parent::__construct( $options ); $this->calendarModelParser = $calendarModelParser; } protected function stringParse( $value ) { list( $sign, $time, $model ) = $this->splitTimeString( $value ); - $time = $this->padTime( $time ); - $precision = $this->getPrecisionFromTime( $sign . $time ); + $time = $sign . $this->padTime( $time ); + + $calendarOpt = $this->getOptions()->getOption( TimeParser::OPT_CALENDAR ); + if( $model === '' && preg_match( '/(' . self::OPT_CALENDAR_GREGORIAN . '|' . self::OPT_CALENDER_JULIAN . ')/i', $calendarOpt ) ) { + $model = $calendarOpt; + } + $model = $this->calendarModelParser->parse( $model ); + + $precisionOpt = $this->getOptions()->getOption( TimeParser::OPT_PRECISION ); + if( is_int( $precisionOpt ) ) { + $precision = $precisionOpt; + } else { + $precision = $this->getPrecisionFromTime( $time ); + } try { - $time = $this->newTimeFromParts( $sign . $time, $model, $precision ); - return $time; + return new TimeValue( $time, 0, 0, 0, $precision, $model ); } catch ( IllegalValueException $ex ) { throw new ParseException( $ex->getMessage() ); } @@ -109,11 +137,6 @@ private function getPrecisionFromYear( $year ) { return $precision; } - private function newTimeFromParts( $time, $model, $precision ) { - $model = $this->calendarModelParser->parse( $model ); - return new TimeValue( $time, 0, 0, 0, $precision, $model ); - } - private function splitTimeString( $value ) { if ( !is_string( $value ) ) { throw new InvalidArgumentException( '$value must be a string' ); diff --git a/tests/ValueParsers/TimeParserTest.php b/tests/ValueParsers/TimeParserTest.php index 06c1a60..4b0230e 100644 --- a/tests/ValueParsers/TimeParserTest.php +++ b/tests/ValueParsers/TimeParserTest.php @@ -4,6 +4,7 @@ use DataValues\TimeValue; use ValueParsers\CalendarModelParser; +use ValueParsers\ParserOptions; use ValueParsers\Test\ValueParserTestBase; use ValueFormatters\TimeFormatter; use ValueParsers\TimeParser; @@ -46,10 +47,23 @@ protected function getInstance() { * @return array */ public function validInputProvider() { - $argLists = array(); + $emptyOpts = new ParserOptions(); + + $julianOpts = clone $emptyOpts; + $julianOpts->setOption( TimeParser::OPT_CALENDAR, TimeParser::OPT_CALENDER_JULIAN ); + + $gregorianOpts = clone $emptyOpts; + $gregorianOpts->setOption( TimeParser::OPT_CALENDAR, TimeParser::OPT_CALENDAR_GREGORIAN ); + + $prec10aOpts = clone $emptyOpts; + $prec10aOpts->setOption( TimeParser::OPT_PRECISION, TimeValue::PRECISION_10a ); + + $noPrecOpts = clone $emptyOpts; + $noPrecOpts->setOption( TimeParser::OPT_PRECISION, TimeParser::OPT_PRECISION_NONE ); $valid = array( - '+0000000000002013-07-16T00:00:00Z' => TimeValue::newFromArray( array( + '+0000000000002013-07-16T00:00:00Z' => array( + TimeValue::newFromArray( array( 'time' => '+0000000000002013-07-16T00:00:00Z', 'timezone' => 0, 'before' => 0, @@ -57,7 +71,10 @@ public function validInputProvider() { 'precision' => TimeValue::PRECISION_DAY, 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '+0000000000002013-07-00T00:00:00Z' => TimeValue::newFromArray( array( + $emptyOpts, + ), + '+0000000000002013-07-00T00:00:00Z' => array( + TimeValue::newFromArray( array( 'time' => '+0000000000002013-07-00T00:00:00Z', 'timezone' => 0, 'before' => 0, @@ -65,7 +82,10 @@ public function validInputProvider() { 'precision' => TimeValue::PRECISION_MONTH, 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '+0000000000002013-00-00T00:00:00Z' => TimeValue::newFromArray( array( + $emptyOpts, + ), + '+0000000000002013-00-00T00:00:00Z' => array( + TimeValue::newFromArray( array( 'time' => '+0000000000002013-00-00T00:00:00Z', 'timezone' => 0, 'before' => 0, @@ -73,7 +93,10 @@ public function validInputProvider() { 'precision' => TimeValue::PRECISION_YEAR, 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '+0000000000002000-00-00T00:00:00Z' => TimeValue::newFromArray( array( + $emptyOpts, + ), + '+0000000000002000-00-00T00:00:00Z' => array( + TimeValue::newFromArray( array( 'time' => '+0000000000002000-00-00T00:00:00Z', 'timezone' => 0, 'before' => 0, @@ -81,7 +104,10 @@ public function validInputProvider() { 'precision' => TimeValue::PRECISION_ka, 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '+0000000000020000-00-00T00:00:00Z' => TimeValue::newFromArray( array( + $emptyOpts, + ), + '+0000000000020000-00-00T00:00:00Z' => array( + TimeValue::newFromArray( array( 'time' => '+0000000000020000-00-00T00:00:00Z', 'timezone' => 0, 'before' => 0, @@ -89,7 +115,10 @@ public function validInputProvider() { 'precision' => TimeValue::PRECISION_10ka, 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '+0000000000200000-00-00T00:00:00Z' => TimeValue::newFromArray( array( + $emptyOpts, + ), + '+0000000000200000-00-00T00:00:00Z' => array( + TimeValue::newFromArray( array( 'time' => '+0000000000200000-00-00T00:00:00Z', 'timezone' => 0, 'before' => 0, @@ -97,7 +126,10 @@ public function validInputProvider() { 'precision' => TimeValue::PRECISION_100ka, 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '+0000000002000000-00-00T00:00:00Z' => TimeValue::newFromArray( array( + $emptyOpts, + ), + '+0000000002000000-00-00T00:00:00Z' => array( + TimeValue::newFromArray( array( 'time' => '+0000000002000000-00-00T00:00:00Z', 'timezone' => 0, 'before' => 0, @@ -105,7 +137,10 @@ public function validInputProvider() { 'precision' => TimeValue::PRECISION_Ma, 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '+0000000020000000-00-00T00:00:00Z' => TimeValue::newFromArray( array( + $emptyOpts, + ), + '+0000000020000000-00-00T00:00:00Z' => array( + TimeValue::newFromArray( array( 'time' => '+0000000020000000-00-00T00:00:00Z', 'timezone' => 0, 'before' => 0, @@ -113,7 +148,10 @@ public function validInputProvider() { 'precision' => TimeValue::PRECISION_10Ma, 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '+0000000200000000-00-00T00:00:00Z' => TimeValue::newFromArray( array( + $emptyOpts, + ), + '+0000000200000000-00-00T00:00:00Z' => array( + TimeValue::newFromArray( array( 'time' => '+0000000200000000-00-00T00:00:00Z', 'timezone' => 0, 'before' => 0, @@ -121,7 +159,10 @@ public function validInputProvider() { 'precision' => TimeValue::PRECISION_100Ma, 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '+0000002000000000-00-00T00:00:00Z' => TimeValue::newFromArray( array( + $emptyOpts, + ), + '+0000002000000000-00-00T00:00:00Z' => array( + TimeValue::newFromArray( array( 'time' => '+0000002000000000-00-00T00:00:00Z', 'timezone' => 0, 'before' => 0, @@ -129,7 +170,10 @@ public function validInputProvider() { 'precision' => TimeValue::PRECISION_Ga, 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '+0000020000000000-00-00T00:00:00Z' => TimeValue::newFromArray( array( + $emptyOpts, + ), + '+0000020000000000-00-00T00:00:00Z' => array( + TimeValue::newFromArray( array( 'time' => '+0000020000000000-00-00T00:00:00Z', 'timezone' => 0, 'before' => 0, @@ -137,7 +181,10 @@ public function validInputProvider() { 'precision' => TimeValue::PRECISION_Ga, 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '+0000200000000000-00-00T00:00:00Z' => TimeValue::newFromArray( array( + $emptyOpts, + ), + '+0000200000000000-00-00T00:00:00Z' => array( + TimeValue::newFromArray( array( 'time' => '+0000200000000000-00-00T00:00:00Z', 'timezone' => 0, 'before' => 0, @@ -145,7 +192,10 @@ public function validInputProvider() { 'precision' => TimeValue::PRECISION_Ga, 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '+0002000000000000-00-00T00:00:00Z' => TimeValue::newFromArray( array( + $emptyOpts, + ), + '+0002000000000000-00-00T00:00:00Z' => array( + TimeValue::newFromArray( array( 'time' => '+0002000000000000-00-00T00:00:00Z', 'timezone' => 0, 'before' => 0, @@ -153,7 +203,10 @@ public function validInputProvider() { 'precision' => TimeValue::PRECISION_Ga, 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '+0020000000000000-00-00T00:00:00Z' => TimeValue::newFromArray( array( + $emptyOpts, + ), + '+0020000000000000-00-00T00:00:00Z' => array( + TimeValue::newFromArray( array( 'time' => '+0020000000000000-00-00T00:00:00Z', 'timezone' => 0, 'before' => 0, @@ -161,7 +214,10 @@ public function validInputProvider() { 'precision' => TimeValue::PRECISION_Ga, 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '+0200000000000000-00-00T00:00:00Z' => TimeValue::newFromArray( array( + $emptyOpts, + ), + '+0200000000000000-00-00T00:00:00Z' => array( + TimeValue::newFromArray( array( 'time' => '+0200000000000000-00-00T00:00:00Z', 'timezone' => 0, 'before' => 0, @@ -169,7 +225,10 @@ public function validInputProvider() { 'precision' => TimeValue::PRECISION_Ga, 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '+2000000000000000-00-00T00:00:00Z' => TimeValue::newFromArray( array( + $emptyOpts, + ), + '+2000000000000000-00-00T00:00:00Z' => array( + TimeValue::newFromArray( array( 'time' => '+2000000000000000-00-00T00:00:00Z', 'timezone' => 0, 'before' => 0, @@ -177,76 +236,151 @@ public function validInputProvider() { 'precision' => TimeValue::PRECISION_Ga, 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '+0000000000002013-07-16T00:00:00Z (Gregorian)' => TimeValue::newFromArray( array( - 'time' => '+0000000000002013-07-16T00:00:00Z', - 'timezone' => 0, - 'before' => 0, - 'after' => 0, - 'precision' => TimeValue::PRECISION_DAY, - 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN + $emptyOpts, + ), + '+0000000000002013-07-16T00:00:00Z (Gregorian)' => array( + TimeValue::newFromArray( array( + 'time' => '+0000000000002013-07-16T00:00:00Z', + 'timezone' => 0, + 'before' => 0, + 'after' => 0, + 'precision' => TimeValue::PRECISION_DAY, + 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN + ) ), + $emptyOpts, + ), + '+0000000000000000-01-01T00:00:00Z (Gregorian)' => array( + TimeValue::newFromArray( array( + 'time' => '+0000000000000000-01-01T00:00:00Z', + 'timezone' => 0, + 'before' => 0, + 'after' => 0, + 'precision' => TimeValue::PRECISION_DAY, + 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN + ) ), + $emptyOpts, + ), + '+0000000000000001-01-14T00:00:00Z (Julian)' => array( + TimeValue::newFromArray( array( + 'time' => '+0000000000000001-01-14T00:00:00Z', + 'timezone' => 0, + 'before' => 0, + 'after' => 0, + 'precision' => TimeValue::PRECISION_DAY, + 'calendarmodel' => TimeFormatter::CALENDAR_JULIAN + ) ), + $emptyOpts, + ), + '+0000000000010000-01-01T00:00:00Z (Gregorian)' => array( + TimeValue::newFromArray( array( + 'time' => '+0000000000010000-01-01T00:00:00Z', + 'timezone' => 0, + 'before' => 0, + 'after' => 0, + 'precision' => TimeValue::PRECISION_DAY, + 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN + ) ), + $emptyOpts, + ), + '-0000000000000001-01-01T00:00:00Z (Gregorian)' => array( + TimeValue::newFromArray( array( + 'time' => '-0000000000000001-01-01T00:00:00Z', + 'timezone' => 0, + 'before' => 0, + 'after' => 0, + 'precision' => TimeValue::PRECISION_DAY, + 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '+0000000000000000-01-01T00:00:00Z (Gregorian)' => TimeValue::newFromArray( array( - 'time' => '+0000000000000000-01-01T00:00:00Z', - 'timezone' => 0, - 'before' => 0, - 'after' => 0, - 'precision' => TimeValue::PRECISION_DAY, - 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN + $emptyOpts, + ), + '-00000000001-01-01T00:00:00Z (Gregorian)' => array( + TimeValue::newFromArray( array( + 'time' => '-0000000000000001-01-01T00:00:00Z', + 'timezone' => 0, + 'before' => 0, + 'after' => 0, + 'precision' => TimeValue::PRECISION_DAY, + 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '+0000000000000001-01-14T00:00:00Z (Julian)' => TimeValue::newFromArray( array( - 'time' => '+0000000000000001-01-14T00:00:00Z', - 'timezone' => 0, - 'before' => 0, - 'after' => 0, - 'precision' => TimeValue::PRECISION_DAY, - 'calendarmodel' => TimeFormatter::CALENDAR_JULIAN + $emptyOpts, + ), + '-000001-01-01T00:00:00Z (Gregorian)' => array( + TimeValue::newFromArray( array( + 'time' => '-0000000000000001-01-01T00:00:00Z', + 'timezone' => 0, + 'before' => 0, + 'after' => 0, + 'precision' => TimeValue::PRECISION_DAY, + 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '+0000000000010000-01-01T00:00:00Z (Gregorian)' => TimeValue::newFromArray( array( - 'time' => '+0000000000010000-01-01T00:00:00Z', - 'timezone' => 0, - 'before' => 0, - 'after' => 0, - 'precision' => TimeValue::PRECISION_DAY, - 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN + $emptyOpts, + ), + '-1-01-01T00:00:00Z (Gregorian)' => array( + TimeValue::newFromArray( array( + 'time' => '-0000000000000001-01-01T00:00:00Z', + 'timezone' => 0, + 'before' => 0, + 'after' => 0, + 'precision' => TimeValue::PRECISION_DAY, + 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '-0000000000000001-01-01T00:00:00Z (Gregorian)' => TimeValue::newFromArray( array( - 'time' => '-0000000000000001-01-01T00:00:00Z', - 'timezone' => 0, - 'before' => 0, - 'after' => 0, - 'precision' => TimeValue::PRECISION_DAY, - 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN + $emptyOpts, + ), + '-1-01-02T00:00:00Z' => array( + TimeValue::newFromArray( array( + 'time' => '-0000000000000001-01-02T00:00:00Z', + 'timezone' => 0, + 'before' => 0, + 'after' => 0, + 'precision' => TimeValue::PRECISION_DAY, + 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '-00000000001-01-01T00:00:00Z (Gregorian)' => TimeValue::newFromArray( array( - 'time' => '-0000000000000001-01-01T00:00:00Z', - 'timezone' => 0, - 'before' => 0, - 'after' => 0, - 'precision' => TimeValue::PRECISION_DAY, - 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN + $gregorianOpts, + ), + '-1-01-03T00:00:00Z' => array( + TimeValue::newFromArray( array( + 'time' => '-0000000000000001-01-03T00:00:00Z', + 'timezone' => 0, + 'before' => 0, + 'after' => 0, + 'precision' => TimeValue::PRECISION_DAY, + 'calendarmodel' => TimeFormatter::CALENDAR_JULIAN ) ), - '-000001-01-01T00:00:00Z (Gregorian)' => TimeValue::newFromArray( array( - 'time' => '-0000000000000001-01-01T00:00:00Z', - 'timezone' => 0, - 'before' => 0, - 'after' => 0, - 'precision' => TimeValue::PRECISION_DAY, - 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN + $julianOpts, + ), + '-1-01-04T00:00:00Z' => array( + TimeValue::newFromArray( array( + 'time' => '-0000000000000001-01-04T00:00:00Z', + 'timezone' => 0, + 'before' => 0, + 'after' => 0, + 'precision' => TimeValue::PRECISION_10a, + 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), - '-1-01-01T00:00:00Z (Gregorian)' => TimeValue::newFromArray( array( - 'time' => '-0000000000000001-01-01T00:00:00Z', - 'timezone' => 0, - 'before' => 0, - 'after' => 0, - 'precision' => TimeValue::PRECISION_DAY, - 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN + $prec10aOpts, + ), + '-1-01-05T00:00:00Z' => array( + TimeValue::newFromArray( array( + 'time' => '-0000000000000001-01-05T00:00:00Z', + 'timezone' => 0, + 'before' => 0, + 'after' => 0, + 'precision' => TimeValue::PRECISION_DAY, + 'calendarmodel' => TimeFormatter::CALENDAR_GREGORIAN ) ), + $noPrecOpts, + ), ); - foreach ( $valid as $value => $expected ) { - // Because PHP turns some of them into ints/floats using black magic - $value = (string)$value; - $argLists[] = array( $value, $expected ); + $argLists = array(); + foreach ( $valid as $key => $value ) { + list( $timeValue, $opts ) = $value; + // Because PHP turns some of them into ints/floats using black magic (string) + $argLists[] = array( + (string)$key, + $timeValue, + new TimeParser( new CalendarModelParser( $opts ), $opts ) + ); } return $argLists; From 9936c7d9aeadb767acc3f4637851a5ab3573fd71 Mon Sep 17 00:00:00 2001 From: addshore Date: Tue, 11 Mar 2014 13:35:47 +0100 Subject: [PATCH 2/2] Use model URIs instead of strings in Parser --- src/ValueParsers/TimeParser.php | 19 ++++++++++++------- tests/ValueParsers/TimeParserTest.php | 6 +++--- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/ValueParsers/TimeParser.php b/src/ValueParsers/TimeParser.php index 77c1717..561ba84 100644 --- a/src/ValueParsers/TimeParser.php +++ b/src/ValueParsers/TimeParser.php @@ -25,9 +25,9 @@ class TimeParser extends StringValueParser { /** * @since 0.3 */ - const OPT_CALENDAR_GREGORIAN = 'gregorian'; - const OPT_CALENDER_JULIAN = 'julian'; - const OPT_PRECISION_NONE = 'noprecision'; + const CALENDAR_GREGORIAN = 'http://www.wikidata.org/entity/Q1985727'; + const CALENDAR_JULIAN = 'http://www.wikidata.org/entity/Q1985786'; + const PRECISION_NONE = 'noprecision'; /** * Regex pattern constant matching the sign preceding the time @@ -51,8 +51,8 @@ class TimeParser extends StringValueParser { */ public function __construct( CalendarModelParser $calendarModelParser, ParserOptions $options = null ) { - $options->defaultOption( TimeParser::OPT_CALENDAR, TimeParser::OPT_CALENDAR_GREGORIAN ); - $options->defaultOption( TimeParser::OPT_PRECISION, TimeParser::OPT_PRECISION_NONE ); + $options->defaultOption( TimeParser::OPT_CALENDAR, TimeParser::CALENDAR_GREGORIAN ); + $options->defaultOption( TimeParser::OPT_PRECISION, TimeParser::PRECISION_NONE ); parent::__construct( $options ); $this->calendarModelParser = $calendarModelParser; @@ -63,10 +63,15 @@ protected function stringParse( $value ) { $time = $sign . $this->padTime( $time ); $calendarOpt = $this->getOptions()->getOption( TimeParser::OPT_CALENDAR ); - if( $model === '' && preg_match( '/(' . self::OPT_CALENDAR_GREGORIAN . '|' . self::OPT_CALENDER_JULIAN . ')/i', $calendarOpt ) ) { + $calanderModelRegex = '/(' . preg_quote( self::CALENDAR_GREGORIAN, '/' ). '|' . preg_quote( self::CALENDAR_JULIAN, '/' ) . ')/i'; + + if( $model === '' && preg_match( $calanderModelRegex, $calendarOpt ) ) { $model = $calendarOpt; + } else if( $model !== '' ) { + $model = $this->calendarModelParser->parse( $model ); + } else { + $model = self::CALENDAR_GREGORIAN; } - $model = $this->calendarModelParser->parse( $model ); $precisionOpt = $this->getOptions()->getOption( TimeParser::OPT_PRECISION ); if( is_int( $precisionOpt ) ) { diff --git a/tests/ValueParsers/TimeParserTest.php b/tests/ValueParsers/TimeParserTest.php index 4b0230e..ed7429c 100644 --- a/tests/ValueParsers/TimeParserTest.php +++ b/tests/ValueParsers/TimeParserTest.php @@ -50,16 +50,16 @@ public function validInputProvider() { $emptyOpts = new ParserOptions(); $julianOpts = clone $emptyOpts; - $julianOpts->setOption( TimeParser::OPT_CALENDAR, TimeParser::OPT_CALENDER_JULIAN ); + $julianOpts->setOption( TimeParser::OPT_CALENDAR, TimeParser::CALENDAR_JULIAN ); $gregorianOpts = clone $emptyOpts; - $gregorianOpts->setOption( TimeParser::OPT_CALENDAR, TimeParser::OPT_CALENDAR_GREGORIAN ); + $gregorianOpts->setOption( TimeParser::OPT_CALENDAR, TimeParser::CALENDAR_GREGORIAN ); $prec10aOpts = clone $emptyOpts; $prec10aOpts->setOption( TimeParser::OPT_PRECISION, TimeValue::PRECISION_10a ); $noPrecOpts = clone $emptyOpts; - $noPrecOpts->setOption( TimeParser::OPT_PRECISION, TimeParser::OPT_PRECISION_NONE ); + $noPrecOpts->setOption( TimeParser::OPT_PRECISION, TimeParser::PRECISION_NONE ); $valid = array( '+0000000000002013-07-16T00:00:00Z' => array(