| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\ParamValidator\TypeDef; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use Wikimedia\Message\DataMessageValue; |
| 7 |
use Wikimedia\Message\MessageValue; |
| 8 |
use Wikimedia\ParamValidator\ParamValidator; |
| 9 |
use Wikimedia\ParamValidator\TypeDef; |
| 10 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class ExpiryDef extends TypeDef { |
| 18 |
|
| 19 |
|
| 20 |
public const INFINITY_VALS = [ 'infinite', 'indefinite', 'infinity', 'never' ]; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public const PARAM_USE_MAX = 'param-use-max'; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public const PARAM_MAX = 'param-max'; |
| 34 |
|
| 35 |
public function validate( $name, $value, array $settings, array $options ) { |
| 36 |
$this->failIfNotString( $name, $value, $settings, $options ); |
| 37 |
|
| 38 |
try { |
| 39 |
$expiry = self::normalizeExpiry( $value, TS_ISO_8601 ); |
| 40 |
} catch ( InvalidArgumentException $e ) { |
| 41 |
$this->failure( 'badexpiry', $name, $value, $settings, $options ); |
| 42 |
} |
| 43 |
|
| 44 |
if ( $expiry !== 'infinity' && $expiry < ConvertibleTimestamp::now( TS_ISO_8601 ) ) { |
| 45 |
$this->failure( 'badexpiry-past', $name, $value, $settings, $options ); |
| 46 |
} |
| 47 |
|
| 48 |
$max = $settings[self::PARAM_MAX] ?? null; |
| 49 |
|
| 50 |
if ( self::expiryExceedsMax( $expiry, $max ) ) { |
| 51 |
$dontUseMax = empty( $settings[self::PARAM_USE_MAX] ); |
| 52 |
|
| 53 |
$msg = DataMessageValue::new( |
| 54 |
$dontUseMax |
| 55 |
? 'paramvalidator-badexpiry-duration' |
| 56 |
: 'paramvalidator-badexpiry-duration-max', |
| 57 |
[ $max ] |
| 58 |
); |
| 59 |
$this->failure( $msg, $name, $value, $settings, $options, $dontUseMax ); |
| 60 |
|
| 61 |
return self::normalizeExpiry( $max, TS_ISO_8601 ); |
| 62 |
} |
| 63 |
|
| 64 |
return $expiry; |
| 65 |
} |
| 66 |
|
| 67 |
public function getHelpInfo( $name, array $settings, array $options ) { |
| 68 |
$info = parent::getHelpInfo( $name, $settings, $options ); |
| 69 |
|
| 70 |
$info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-expiry' ) |
| 71 |
->params( empty( $settings[ParamValidator::PARAM_ISMULTI] ) ? 1 : 2 ) |
| 72 |
->textListParams( |
| 73 |
|
| 74 |
|
| 75 |
array_map( static function ( $val ) { |
| 76 |
return "\"$val\""; |
| 77 |
}, self::INFINITY_VALS ) |
| 78 |
); |
| 79 |
|
| 80 |
return $info; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public static function normalizeExpiry( ?string $expiry = null, ?int $style = null ) { |
| 95 |
if ( $expiry === null ) { |
| 96 |
return null; |
| 97 |
} |
| 98 |
if ( in_array( $expiry, self::INFINITY_VALS, true ) ) { |
| 99 |
return 'infinity'; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
$unix = strtotime( $expiry, ConvertibleTimestamp::time() ); |
| 104 |
if ( $unix === false ) { |
| 105 |
|
| 106 |
throw new InvalidArgumentException( "Invalid expiry value: {$expiry}" ); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
$expiryConvertibleTimestamp = new ConvertibleTimestamp( $unix === 0 ? '00' : $unix ); |
| 113 |
|
| 114 |
if ( $style !== null ) { |
| 115 |
return $expiryConvertibleTimestamp->getTimestamp( $style ); |
| 116 |
} |
| 117 |
|
| 118 |
return $expiryConvertibleTimestamp; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public static function normalizeUsingMaxExpiry( ?string $expiry, ?string $maxExpiryDuration, ?int $style ) { |
| 131 |
if ( self::expiryExceedsMax( $expiry, $maxExpiryDuration ) ) { |
| 132 |
return self::normalizeExpiry( $maxExpiryDuration, $style ); |
| 133 |
} |
| 134 |
return self::normalizeExpiry( $expiry, $style ); |
| 135 |
} |
| 136 |
|
| 137 |
public function checkSettings( string $name, $settings, array $options, array $ret ): array { |
| 138 |
$ret = parent::checkSettings( $name, $settings, $options, $ret ); |
| 139 |
|
| 140 |
$ret['allowedKeys'][] = self::PARAM_USE_MAX; |
| 141 |
$ret['allowedKeys'][] = self::PARAM_MAX; |
| 142 |
|
| 143 |
return $ret; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
private static function expiryExceedsMax( ?string $expiry, ?string $max = null ): bool { |
| 154 |
$expiry = self::normalizeExpiry( $expiry ); |
| 155 |
$max = self::normalizeExpiry( $max ); |
| 156 |
|
| 157 |
if ( !$max || !$expiry || $expiry === 'infinity' ) { |
| 158 |
|
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
return $expiry > $max; |
| 163 |
} |
| 164 |
|
| 165 |
} |