|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleAsFuck\ApiToolkit\Data\Server; |
| 6 | + |
| 7 | +use SimpleAsFuck\Validator\Factory\Exception; |
| 8 | +use SimpleAsFuck\Validator\Model\RuleChain; |
| 9 | +use SimpleAsFuck\Validator\Model\Validated; |
| 10 | +use SimpleAsFuck\Validator\Rule\ArrayRule\Collection; |
| 11 | +use SimpleAsFuck\Validator\Rule\ArrayRule\TypedKey; |
| 12 | +use SimpleAsFuck\Validator\Rule\String\RegexMatch; |
| 13 | +use SimpleAsFuck\Validator\Rule\String\StringRule; |
| 14 | + |
| 15 | +/** @phpstan-ignore-next-line */ |
| 16 | +final class HeaderRule extends \SimpleAsFuck\ApiToolkit\Model\Server\HeaderRule |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @param Validated<array<array<string>>> $validated |
| 20 | + */ |
| 21 | + public function __construct( |
| 22 | + private readonly Exception $exceptionFactory, |
| 23 | + private readonly Validated $validated, |
| 24 | + ) { |
| 25 | + /** @phpstan-ignore-next-line */ |
| 26 | + parent::__construct($exceptionFactory, $validated); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @param non-empty-string $key |
| 31 | + */ |
| 32 | + public function key(string $key, bool $caseSensitive = false): StringRule |
| 33 | + { |
| 34 | + $values = $this->headerValues($caseSensitive); |
| 35 | + $values = $values[$caseSensitive ? $key : strtolower($key)] ?? []; |
| 36 | + $value = array_pop($values); |
| 37 | + return new StringRule($this->exceptionFactory, new RuleChain(), new Validated($value), 'Request header: '.$key); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @template TMapped |
| 42 | + * @param non-empty-string $key |
| 43 | + * @param callable(StringRule): TMapped $callable |
| 44 | + * @return Collection<TMapped> |
| 45 | + */ |
| 46 | + public function keyOf(string $key, callable $callable, bool $caseSensitive = false): Collection |
| 47 | + { |
| 48 | + $values = $this->headerValues($caseSensitive); |
| 49 | + $values = $values[$caseSensitive ? $key : strtolower($key)] ?? null; |
| 50 | + return new Collection( |
| 51 | + $this->exceptionFactory, |
| 52 | + /** @phpstan-ignore-next-line */ |
| 53 | + new RuleChain(), |
| 54 | + new Validated($values), |
| 55 | + 'Request header: '.$key, |
| 56 | + fn (TypedKey $index) => $callable($index->string()) |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * method will return rule with regex match for value of http standard authorization header |
| 62 | + * |
| 63 | + * @param non-empty-string $type |
| 64 | + */ |
| 65 | + public function authorization(string $type): RegexMatch |
| 66 | + { |
| 67 | + return $this |
| 68 | + ->key('Authorization') |
| 69 | + ->parseRegex('/^' . preg_quote($type, '/') . ' (?P<value>.+)$/') |
| 70 | + ->match('value') |
| 71 | + ; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @return array<array<string>>|null |
| 76 | + */ |
| 77 | + private function headerValues(bool $caseSensitive): ?array |
| 78 | + { |
| 79 | + $values = $this->validated->value; |
| 80 | + if ($caseSensitive) { |
| 81 | + return $values; |
| 82 | + } |
| 83 | + |
| 84 | + return $values !== null ? array_change_key_case($values, CASE_LOWER) : $values; |
| 85 | + } |
| 86 | +} |
0 commit comments