AbstractSectionCollection::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ICanBoogie\CLDR;
4
5
use ArrayAccess;
6
use ICanBoogie\CLDR\Provider\ResourceNotFound;
7
use LogicException;
8
9
/**
10
 * @template TKey of array-key
11
 *
12
 * @implements ArrayAccess<TKey, mixed>
13
 */
14
abstract class AbstractSectionCollection implements ArrayAccess
15
{
16
    use CollectionTrait;
17
18
    public function __construct(
19
        public readonly Repository $repository
20
    ) {
21
    }
22
23
    abstract public function offsetExists(mixed $offset): bool;
24
25
    /**
26
     * @var array<string, array>
27
     *     Loaded sections, where _key_ is a section name and _value_ its data.
28
     *
29
     * @phpstan-ignore-next-line
30
     */
31
    private array $sections = [];
32
33
    /**
34
     * @throws LogicException
35
     * @throws ResourceNotFound
36
     */
37
    #[\ReturnTypeWillChange]
38
    public function offsetGet(mixed $offset)
39
    {
40
        if (!$this->offsetExists($offset)) {
41
            throw new LogicException("Offset '$offset' does not exist");
42
        }
43
44
        return $this->sections[$offset] ??= $this->repository->fetch(
45
            $this->path_for($offset),
46
            $this->data_path_for($offset)
47
        );
48
    }
49
50
    /**
51
     * Returns the CLDR path for the offset.
52
     */
53
    abstract protected function path_for(string $offset): string;
54
55
    /**
56
     * Returns the data path for the offset.
57
     */
58
    abstract protected function data_path_for(string $offset): string;
59
}
60