5use InvalidArgumentException;
11use Wikimedia\Timestamp\ConvertibleTimestamp;
12use Wikimedia\Timestamp\TimestampException;
63 $this->defaultFormat = $options[
'defaultFormat'] ??
'ConvertibleTimestamp';
64 $this->stringifyFormat = $options[
'stringifyFormat'] ?? TS_ISO_8601;
67 if ( $this->defaultFormat !==
'ConvertibleTimestamp' && $this->defaultFormat !==
'DateTime' &&
68 ConvertibleTimestamp::convert( $this->defaultFormat, 0 ) ===
false
70 throw new InvalidArgumentException(
'Invalid value for $options[\'defaultFormat\']' );
72 if ( ConvertibleTimestamp::convert( $this->stringifyFormat, 0 ) ===
false ) {
73 throw new InvalidArgumentException(
'Invalid value for $options[\'stringifyFormat\']' );
77 public function validate( $name, $value, array $settings, array $options ) {
80 $this->
failure(
'unclearnowtimestamp', $name, $value, $settings, $options,
false );
85 $timestamp =
new ConvertibleTimestamp( $value ===
'now' ?
false : $value );
86 }
catch ( TimestampException $ex ) {
89 $this->
failureMessage(
'badtimestamp' )->plaintextParams( $name, $value ),
90 $name, $value, $settings, $ex
96 case 'ConvertibleTimestamp':
101 return $timestamp->timestamp;
104 return $timestamp->getTimestamp( $format );
108 public function checkSettings(
string $name, $settings, array $options, array $ret ) : array {
109 $ret = parent::
checkSettings( $name, $settings, $options, $ret );
111 $ret[
'allowedKeys'] = array_merge( $ret[
'allowedKeys'], [
112 self::PARAM_TIMESTAMP_FORMAT,
116 if ( $f !==
'ConvertibleTimestamp' && $f !==
'DateTime' &&
117 ConvertibleTimestamp::convert( $f, 0 ) ===
false
125 public function stringifyValue( $name, $value, array $settings, array $options ) {
126 if ( !$value instanceof ConvertibleTimestamp ) {
127 $value =
new ConvertibleTimestamp( $value );
129 return $value->getTimestamp( $this->stringifyFormat );
132 public function getHelpInfo( $name, array $settings, array $options ) {
133 $info = parent::getHelpInfo( $name, $settings, $options );