defaultFormat = $options['defaultFormat'] ?? 'ConvertibleTimestamp'; $this->stringifyFormat = $options['stringifyFormat'] ?? TS_ISO_8601; // Check values by trying to convert 0 if ( $this->defaultFormat !== 'ConvertibleTimestamp' && $this->defaultFormat !== 'DateTime' && ConvertibleTimestamp::convert( $this->defaultFormat, 0 ) === false ) { throw new InvalidArgumentException( 'Invalid value for $options[\'defaultFormat\']' ); } if ( ConvertibleTimestamp::convert( $this->stringifyFormat, 0 ) === false ) { throw new InvalidArgumentException( 'Invalid value for $options[\'stringifyFormat\']' ); } } public function validate( $name, $value, array $settings, array $options ) { // Confusing synonyms for the current time accepted by ConvertibleTimestamp if ( !$value ) { $this->failure( 'unclearnowtimestamp', $name, $value, $settings, $options, false ); $value = 'now'; } try { $timestamp = new ConvertibleTimestamp( $value === 'now' ? false : $value ); } catch ( TimestampException $ex ) { // $this->failure() doesn't handle passing a previous exception throw new ValidationException( $this->failureMessage( 'badtimestamp' )->plaintextParams( $name, $value ), $name, $value, $settings, $ex ); } $format = $settings[self::PARAM_TIMESTAMP_FORMAT] ?? $this->defaultFormat; switch ( $format ) { case 'ConvertibleTimestamp': return $timestamp; case 'DateTime': // Eew, no getter. return $timestamp->timestamp; default: return $timestamp->getTimestamp( $format ); } } public function checkSettings( string $name, $settings, array $options, array $ret ) : array { $ret = parent::checkSettings( $name, $settings, $options, $ret ); $ret['allowedKeys'] = array_merge( $ret['allowedKeys'], [ self::PARAM_TIMESTAMP_FORMAT, ] ); $f = $settings[self::PARAM_TIMESTAMP_FORMAT] ?? $this->defaultFormat; if ( $f !== 'ConvertibleTimestamp' && $f !== 'DateTime' && ConvertibleTimestamp::convert( $f, 0 ) === false ) { $ret['issues'][self::PARAM_TIMESTAMP_FORMAT] = 'Value for PARAM_TIMESTAMP_FORMAT is not valid'; } return $ret; } public function stringifyValue( $name, $value, array $settings, array $options ) { if ( !$value instanceof ConvertibleTimestamp ) { $value = new ConvertibleTimestamp( $value ); } return $value->getTimestamp( $this->stringifyFormat ); } public function getHelpInfo( $name, array $settings, array $options ) { $info = parent::getHelpInfo( $name, $settings, $options ); $info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-timestamp' ) ->params( empty( $settings[ParamValidator::PARAM_ISMULTI] ) ? 1 : 2 ); return $info; } }