aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/bakame/http-structured-fields/src/Validation/ValidatedParameters.php
blob: c7f5b5eb6574350d88486704372e9ddcd17fbbfd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php

declare(strict_types=1);

namespace Bakame\Http\StructuredFields\Validation;

use ArrayAccess;
use Bakame\Http\StructuredFields\ForbiddenOperation;
use Bakame\Http\StructuredFields\InvalidOffset;
use Bakame\Http\StructuredFields\StructuredFieldProvider;
use Countable;
use Iterator;
use IteratorAggregate;

/**
 * @phpstan-import-type SfType from StructuredFieldProvider
 *
 * @implements ArrayAccess<array-key, array{0:string, 1:SfType}|array{}|SfType|null>
 * @implements IteratorAggregate<array-key, array{0:string, 1:SfType}|array{}|SfType|null>
 */
final class ValidatedParameters implements ArrayAccess, Countable, IteratorAggregate
{
    /**
     * @param array<array-key, array{0:string, 1:SfType}|array{}|SfType|null> $values
     */
    public function __construct(
        private readonly array $values = [],
    ) {
    }

    public function count(): int
    {
        return count($this->values);
    }

    public function getIterator(): Iterator
    {
        yield from $this->values;
    }

    public function offsetExists($offset): bool
    {
        return array_key_exists($offset, $this->values);
    }

    public function offsetGet($offset): mixed
    {
        return $this->offsetExists($offset) ? $this->values[$offset] : throw InvalidOffset::dueToMemberNotFound($offset);
    }

    public function offsetUnset(mixed $offset): void
    {
        throw new ForbiddenOperation(self::class.' instance can not be updated using '.ArrayAccess::class.' methods.');
    }

    public function offsetSet(mixed $offset, mixed $value): void
    {
        throw new ForbiddenOperation(self::class.' instance can not be updated using '.ArrayAccess::class.' methods.');
    }

    /**
     * @return array<array-key, array{0:string, 1:SfType}|array{}|SfType|null>
     */
    public function all(): array
    {
        return $this->values;
    }
}