MediaWiki  1.34.0
TimestampDef.php
Go to the documentation of this file.
1 <?php
2 
4 
8 use Wikimedia\Timestamp\ConvertibleTimestamp;
9 use Wikimedia\Timestamp\TimestampException;
10 
29 class TimestampDef extends TypeDef {
30 
41  const PARAM_TIMESTAMP_FORMAT = 'param-timestamp-format';
42 
44  protected $defaultFormat;
45 
47  protected $stringifyFormat;
48 
57  public function __construct( Callbacks $callbacks, array $options = [] ) {
58  parent::__construct( $callbacks );
59 
60  $this->defaultFormat = $options['defaultFormat'] ?? 'ConvertibleTimestamp';
61  $this->stringifyFormat = $options['stringifyFormat'] ?? TS_ISO_8601;
62  }
63 
64  public function validate( $name, $value, array $settings, array $options ) {
65  // Confusing synonyms for the current time accepted by ConvertibleTimestamp
66  if ( !$value ) {
67  $this->callbacks->recordCondition(
68  new ValidationException( $name, $value, $settings, 'unclearnowtimestamp', [] ),
69  $options
70  );
71  $value = 'now';
72  }
73 
74  try {
75  $timestamp = new ConvertibleTimestamp( $value === 'now' ? false : $value );
76  } catch ( TimestampException $ex ) {
77  throw new ValidationException( $name, $value, $settings, 'badtimestamp', [], $ex );
78  }
79 
80  $format = $settings[self::PARAM_TIMESTAMP_FORMAT] ?? $this->defaultFormat;
81  switch ( $format ) {
82  case 'ConvertibleTimestamp':
83  return $timestamp;
84 
85  case 'DateTime':
86  // Eew, no getter.
87  return $timestamp->timestamp;
88 
89  default:
90  return $timestamp->getTimestamp( $format );
91  }
92  }
93 
94  public function stringifyValue( $name, $value, array $settings, array $options ) {
95  if ( !$value instanceof ConvertibleTimestamp ) {
96  $value = new ConvertibleTimestamp( $value );
97  }
98  return $value->getTimestamp( $this->stringifyFormat );
99  }
100 
101 }
Wikimedia\ParamValidator\TypeDef\TimestampDef\PARAM_TIMESTAMP_FORMAT
const PARAM_TIMESTAMP_FORMAT
(string|int) Timestamp format to return from validate()
Definition: TimestampDef.php:41
Wikimedia\ParamValidator\TypeDef\TimestampDef\$defaultFormat
string int $defaultFormat
Definition: TimestampDef.php:44
Wikimedia\ParamValidator\ValidationException
Error reporting for ParamValidator.
Definition: ValidationException.php:15
Wikimedia\ParamValidator\Callbacks
Interface defining callbacks needed by ParamValidator.
Definition: Callbacks.php:20
Wikimedia\ParamValidator\TypeDef\TimestampDef\__construct
__construct(Callbacks $callbacks, array $options=[])
Definition: TimestampDef.php:57
Wikimedia\ParamValidator\TypeDef
Base definition for ParamValidator types.
Definition: TypeDef.php:15
Wikimedia\ParamValidator\TypeDef\TimestampDef\stringifyValue
stringifyValue( $name, $value, array $settings, array $options)
Convert a value to a string representation.
Definition: TimestampDef.php:94
Wikimedia\ParamValidator\TypeDef\TimestampDef\$stringifyFormat
int $stringifyFormat
Definition: TimestampDef.php:47
Wikimedia\ParamValidator\TypeDef\TimestampDef
Type definition for timestamp types.
Definition: TimestampDef.php:29
Wikimedia\ParamValidator\TypeDef\TimestampDef\validate
validate( $name, $value, array $settings, array $options)
Validate the value.
Definition: TimestampDef.php:64
Wikimedia\ParamValidator\TypeDef
Definition: BooleanDef.php:3
Wikimedia\ParamValidator\TypeDef\$callbacks
Callbacks $callbacks
Definition: TypeDef.php:18