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 );
87 $timestampObj =
new ConvertibleTimestamp( $value ===
'now' ?
false : $value );
89 $timestamp = ( $format !==
'ConvertibleTimestamp' && $format !==
'DateTime' )
90 ? $timestampObj->getTimestamp( $format )
92 }
catch ( TimestampException $ex ) {
95 $this->
failureMessage(
'badtimestamp' )->plaintextParams( $name, $value ),
96 $name, $value, $settings, $ex
101 case 'ConvertibleTimestamp':
102 return $timestampObj;
106 return $timestampObj->timestamp;
113 public function checkSettings(
string $name, $settings, array $options, array $ret ): array {
114 $ret = parent::
checkSettings( $name, $settings, $options, $ret );
116 $ret[
'allowedKeys'] = array_merge( $ret[
'allowedKeys'], [
117 self::PARAM_TIMESTAMP_FORMAT,
121 if ( $f !==
'ConvertibleTimestamp' && $f !==
'DateTime' &&
122 ConvertibleTimestamp::convert( $f, 0 ) ===
false
130 public function stringifyValue( $name, $value, array $settings, array $options ) {
131 if ( !$value instanceof ConvertibleTimestamp ) {
132 $value =
new ConvertibleTimestamp( $value );
134 return $value->getTimestamp( $this->stringifyFormat );
137 public function getHelpInfo( $name, array $settings, array $options ) {
138 $info = parent::getHelpInfo( $name, $settings, $options );