|
15 | 15 | * Import classes |
16 | 16 | */ |
17 | 17 | use ArrayIterator; |
18 | | -use IteratorAggregate; |
| 18 | +use DateTime; |
| 19 | +use DateTimeImmutable; |
| 20 | +use DateTimeInterface; |
| 21 | +use DateTimeZone; |
| 22 | +use InvalidArgumentException; |
19 | 23 | use Traversable; |
20 | 24 |
|
21 | 25 | /** |
22 | 26 | * Import functions |
23 | 27 | */ |
| 28 | +use function gettype; |
| 29 | +use function is_int; |
| 30 | +use function is_string; |
| 31 | +use function preg_match; |
24 | 32 | use function sprintf; |
25 | 33 |
|
26 | 34 | /** |
27 | 35 | * AbstractHeader |
28 | | - * |
29 | | - * @template-implements IteratorAggregate<int, string> |
30 | 36 | */ |
31 | | -abstract class AbstractHeader implements HeaderInterface, IteratorAggregate |
| 37 | +abstract class AbstractHeader implements HeaderInterface |
32 | 38 | { |
33 | 39 |
|
34 | 40 | /** |
35 | 41 | * {@inheritdoc} |
36 | 42 | */ |
37 | | - public function __toString() : string |
| 43 | + final public function __toString() : string |
38 | 44 | { |
39 | | - return sprintf( |
40 | | - '%s: %s', |
41 | | - $this->getFieldName(), |
42 | | - $this->getFieldValue() |
43 | | - ); |
| 45 | + return sprintf('%s: %s', $this->getFieldName(), $this->getFieldValue()); |
44 | 46 | } |
45 | 47 |
|
46 | 48 | /** |
47 | 49 | * {@inheritdoc} |
48 | 50 | */ |
49 | | - public function getIterator() : Traversable |
| 51 | + final public function getIterator() : Traversable |
| 52 | + { |
| 53 | + return new ArrayIterator([$this->getFieldName(), $this->getFieldValue()]); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Gets Regular Expression for a token validation |
| 58 | + * |
| 59 | + * @return string |
| 60 | + */ |
| 61 | + protected function getTokenValidationRegularExpression() : string |
| 62 | + { |
| 63 | + return HeaderInterface::RFC7230_TOKEN; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Gets Regular Expression for a parameter name validation |
| 68 | + * |
| 69 | + * @return string |
| 70 | + */ |
| 71 | + protected function getParameterNameValidationRegularExpression() : string |
| 72 | + { |
| 73 | + return HeaderInterface::RFC7230_TOKEN; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Gets Regular Expression for a parameter value validation |
| 78 | + * |
| 79 | + * @return string |
| 80 | + */ |
| 81 | + protected function getParameterValueValidationRegularExpression() : string |
| 82 | + { |
| 83 | + return HeaderInterface::RFC7230_QUOTED_STRING; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Checks if the given string is token |
| 88 | + * |
| 89 | + * @param string $token |
| 90 | + * |
| 91 | + * @return bool |
| 92 | + */ |
| 93 | + final protected function isToken(string $token) : bool |
| 94 | + { |
| 95 | + return preg_match($this->getTokenValidationRegularExpression(), $token) === 1; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Validates the given token(s) |
| 100 | + * |
| 101 | + * @param string ...$tokens |
| 102 | + * |
| 103 | + * @return void |
| 104 | + * |
| 105 | + * @throws InvalidArgumentException |
| 106 | + * If one of the tokens isn't valid. |
| 107 | + */ |
| 108 | + final protected function validateToken(string ...$tokens) : void |
| 109 | + { |
| 110 | + foreach ($tokens as $token) { |
| 111 | + if (!$this->isToken($token)) { |
| 112 | + throw new InvalidArgumentException(sprintf( |
| 113 | + 'The value "%2$s" for the header "%1$s" is not valid', |
| 114 | + $this->getFieldName(), |
| 115 | + $token |
| 116 | + )); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Validates the given quoted string(s) |
| 123 | + * |
| 124 | + * @param string ...$quotedStrings |
| 125 | + * |
| 126 | + * @return void |
| 127 | + * |
| 128 | + * @throws InvalidArgumentException |
| 129 | + * If one of the quoted strings isn't valid. |
| 130 | + */ |
| 131 | + final protected function validateQuotedString(string ...$quotedStrings) : void |
| 132 | + { |
| 133 | + foreach ($quotedStrings as $quotedString) { |
| 134 | + if (!preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $quotedString)) { |
| 135 | + throw new InvalidArgumentException(sprintf( |
| 136 | + 'The value "%2$s" for the header "%1$s" is not valid', |
| 137 | + $this->getFieldName(), |
| 138 | + $quotedString |
| 139 | + )); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Validates and normalizes the given parameters |
| 146 | + * |
| 147 | + * @param array<array-key, mixed> $parameters |
| 148 | + * |
| 149 | + * @return array |
| 150 | + * |
| 151 | + * @throws InvalidArgumentException |
| 152 | + * If one of the parameters isn't valid. |
| 153 | + */ |
| 154 | + final protected function validateParameters(array $parameters) : array |
| 155 | + { |
| 156 | + foreach ($parameters as $name => $value) { |
| 157 | + // e.g. Cache-Control: max-age=31536000 |
| 158 | + if (is_int($value)) { |
| 159 | + $parameters[$name] = $value = (string) $value; |
| 160 | + } |
| 161 | + |
| 162 | + if (!is_string($name) || !preg_match($this->getParameterNameValidationRegularExpression(), $name)) { |
| 163 | + throw new InvalidArgumentException(sprintf( |
| 164 | + 'The parameter-name "%2$s" for the header "%1$s" is not valid', |
| 165 | + $this->getFieldName(), |
| 166 | + (is_string($name) ? $name : ('<' . gettype($name) . '>')) |
| 167 | + )); |
| 168 | + } |
| 169 | + |
| 170 | + /** @psalm-suppress MixedArgument */ |
| 171 | + if (!is_string($value) || !preg_match($this->getParameterValueValidationRegularExpression(), $value)) { |
| 172 | + throw new InvalidArgumentException(sprintf( |
| 173 | + 'The parameter-value "%2$s" for the header "%1$s" is not valid', |
| 174 | + $this->getFieldName(), |
| 175 | + (is_string($value) ? $value : ('<' . gettype($value) . '>')) |
| 176 | + )); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + return $parameters; |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Validates the given header field-name |
| 185 | + * |
| 186 | + * @param mixed $fieldName |
| 187 | + * |
| 188 | + * @return void |
| 189 | + * |
| 190 | + * @throws InvalidArgumentException |
| 191 | + * If the header field-name isn't valid. |
| 192 | + */ |
| 193 | + final protected function validateFieldName($fieldName) : void |
| 194 | + { |
| 195 | + if (!is_string($fieldName)) { |
| 196 | + throw new InvalidArgumentException('Header field-name must be a string'); |
| 197 | + } |
| 198 | + |
| 199 | + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $fieldName)) { |
| 200 | + throw new InvalidArgumentException('Header field-name is invalid'); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Validates the given header field-value |
| 206 | + * |
| 207 | + * @param mixed $fieldValue |
| 208 | + * |
| 209 | + * @return void |
| 210 | + * |
| 211 | + * @throws InvalidArgumentException |
| 212 | + * If the header field-value isn't valid. |
| 213 | + */ |
| 214 | + final protected function validateFieldValue($fieldValue) : void |
| 215 | + { |
| 216 | + if (!is_string($fieldValue)) { |
| 217 | + throw new InvalidArgumentException('Header field-value must be a string'); |
| 218 | + } |
| 219 | + |
| 220 | + // a header's field value can be empty... |
| 221 | + if ($fieldValue === '') { |
| 222 | + return; |
| 223 | + } |
| 224 | + |
| 225 | + if (!preg_match(HeaderInterface::RFC7230_FIELD_VALUE, $fieldValue)) { |
| 226 | + throw new InvalidArgumentException('Header field-value is invalid'); |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + /** |
| 231 | + * Normalizes the given date-time object |
| 232 | + * |
| 233 | + * @param T $dateTime |
| 234 | + * |
| 235 | + * @return T |
| 236 | + * |
| 237 | + * @template T as DateTimeInterface |
| 238 | + */ |
| 239 | + final protected function normalizeDateTime(DateTimeInterface $dateTime) : DateTimeInterface |
| 240 | + { |
| 241 | + if ($dateTime instanceof DateTime) { |
| 242 | + return (clone $dateTime)->setTimezone(new DateTimeZone('GMT')); |
| 243 | + } |
| 244 | + |
| 245 | + if ($dateTime instanceof DateTimeImmutable) { |
| 246 | + return $dateTime->setTimezone(new DateTimeZone('GMT')); |
| 247 | + } |
| 248 | + |
| 249 | + // @codeCoverageIgnoreStart |
| 250 | + return $dateTime; |
| 251 | + // @codeCoverageIgnoreEnd |
| 252 | + } |
| 253 | + |
| 254 | + /** |
| 255 | + * Formats the given date-time object |
| 256 | + * |
| 257 | + * @param DateTimeInterface $dateTime |
| 258 | + * |
| 259 | + * @return string |
| 260 | + * |
| 261 | + * @link https://datatracker.ietf.org/doc/html/rfc822 |
| 262 | + */ |
| 263 | + final protected function formatDateTime(DateTimeInterface $dateTime) : string |
50 | 264 | { |
51 | | - return new ArrayIterator([ |
52 | | - $this->getFieldName(), |
53 | | - $this->getFieldValue(), |
54 | | - ]); |
| 265 | + return $this->normalizeDateTime($dateTime)->format(DateTime::RFC822); |
55 | 266 | } |
56 | 267 | } |
0 commit comments