Skip to content

Commit

Permalink
Use model URIs instead of strings in Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
addshore committed Mar 11, 2014
1 parent 1140753 commit 9936c7d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
19 changes: 12 additions & 7 deletions src/ValueParsers/TimeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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 ) ) {
Expand Down
6 changes: 3 additions & 3 deletions tests/ValueParsers/TimeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 9936c7d

Please sign in to comment.