Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
Dictionary
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
8 / 8
13
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 create
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 empty
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 throwIfInvalidOffset
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 offsetExists
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 offsetGet
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 offsetSet
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 offsetUnset
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\Core\Collection;
6
7use TypeError;
8use PeServer\Core\Collection\Arr;
9use PeServer\Core\Collection\TypeArrayBase;
10use PeServer\Core\Throws\ArgumentException;
11use PeServer\Core\Throws\ArgumentNullException;
12use PeServer\Core\Throws\IndexOutOfRangeException;
13use PeServer\Core\Throws\KeyNotFoundException;
14use PeServer\Core\Throws\NotSupportedException;
15use PeServer\Core\TypeUtility;
16
17/**
18 * 連想配列(キーは文字列限定)。
19 *
20 * @template TValue
21 * @extends TypeArrayBase<string,TValue>
22 */
23class Dictionary extends TypeArrayBase
24{
25    /**
26     * 生成。
27     *
28     * @param class-string|TypeUtility::TYPE_* $type
29     * @param array $map
30     * @phpstan-param array<string,TValue> $map
31     */
32    public function __construct(string $type, array $map)
33    {
34        parent::__construct($type);
35
36        foreach ($map as $key => $value) {
37            $this->isValidType($value);
38            $this->items[$key] = $value;
39        }
40    }
41
42    #region function
43
44    /**
45     * 配列から生成。
46     *
47     * @template TTValue
48     * @param array $map 配列。
49     * @phpstan-param non-empty-array<string,TTValue> $map
50     * @return self
51     * @phpstan-return self<TTValue>
52     */
53    public static function create(array $map): self
54    {
55        if (Arr::isNullOrEmpty($map)) {
56            throw new ArgumentException('$map');
57        }
58
59        $firstKey = Arr::getFirstKey($map);
60        $firstValue = $map[$firstKey];
61
62        $type = TypeUtility::getType($firstValue);
63
64        return new self($type, $map);
65    }
66
67    /**
68     * 空データの生成。
69     *
70     * @template TTValue
71     * @param class-string|TypeUtility::TYPE_* $type
72     * @return self
73     * @phpstan-return self<TTValue>
74     */
75    public static function empty(string $type): self //@phpstan-ignore-line TTValue
76    {
77        return new self($type, []);
78    }
79
80    protected function throwIfInvalidOffset(mixed $offset): void
81    {
82        if ($offset === null) {
83            throw new TypeError('$offset: null');
84        }
85
86        if (!is_string($offset)) {
87            throw new TypeError('$offset: ' . gettype($offset));
88        }
89    }
90
91    #endregion
92
93
94    #region TypeArrayBase
95
96    /**
97     * @param string $offset
98     */
99    public function offsetExists(mixed $offset): bool
100    {
101        $this->throwIfInvalidOffset($offset);
102
103        return isset($this->items[$offset]);
104    }
105    /**
106     * @param string $offset
107     * @phpstan-return TValue $value
108     */
109    public function offsetGet(mixed $offset): mixed
110    {
111        $this->throwIfInvalidOffset($offset);
112
113        if (!isset($this->items[$offset])) {
114            throw new KeyNotFoundException('$offset: ' . $offset);
115        }
116
117        return $this->items[$offset];
118    }
119    /**
120     * @param string|null $offset
121     * @phpstan-param TValue $value
122     */
123    public function offsetSet(mixed $offset, mixed $value): void
124    {
125        ArgumentNullException::throwIfNull($offset, '$offset');
126
127        $this->isValidType($value);
128
129        $this->items[$offset] = $value;
130    }
131    /**
132     * @param string $offset
133     */
134    public function offsetUnset(mixed $offset): void
135    {
136        $this->throwIfInvalidOffset($offset);
137
138        unset($this->items[$offset]);
139    }
140
141    #endregion
142}