Skip to content

Commit

Permalink
Merge pull request #6 from DataValues/timeOpts
Browse files Browse the repository at this point in the history
Add precision and calendar opts to TimeParser
  • Loading branch information
Daniel Kinzler committed Mar 11, 2014
2 parents 8461d7e + 9936c7d commit ba7da7e
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 87 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/DataValues/TimeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down
46 changes: 37 additions & 9 deletions src/ValueParsers/TimeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@
*/
class TimeParser extends StringValueParser {

/**
* @since 0.3
*/
const OPT_PRECISION = 'precision';
const OPT_CALENDAR = 'calender';

/**
* @since 0.3
*/
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 @@ -37,18 +50,38 @@ class TimeParser extends StringValueParser {
* @param ParserOptions|null $options
*/
public function __construct( CalendarModelParser $calendarModelParser, ParserOptions $options = null ) {

$options->defaultOption( TimeParser::OPT_CALENDAR, TimeParser::CALENDAR_GREGORIAN );
$options->defaultOption( TimeParser::OPT_PRECISION, TimeParser::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 );
$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;
}

$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() );
}
Expand Down Expand Up @@ -109,11 +142,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' );
Expand Down
Loading

0 comments on commit ba7da7e

Please sign in to comment.