Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
ArrayDefine | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
8 |
1 | <?php |
2 | |
3 | /** |
4 | * @license MIT |
5 | * @author hazuki3417<hazuki3417@gmail.com> |
6 | * @copyright 2022 hazuki3417 all rights reserved. |
7 | */ |
8 | |
9 | namespace Selen\Schema\Validate; |
10 | |
11 | use LogicException; |
12 | |
13 | class ArrayDefine |
14 | { |
15 | /** @var Define[] key定義 */ |
16 | public $defines; |
17 | |
18 | /** @var array<int,int|string> key名一覧 */ |
19 | public $keys = []; |
20 | |
21 | /** @var bool Index配列定義の存在有無 */ |
22 | public bool $indexArrayDefineExists = false; |
23 | |
24 | /** @var bool Assoc配列定義の存在有無 */ |
25 | public bool $assocArrayDefineExists = false; |
26 | |
27 | /** |
28 | * ArrayDefineインスタンスを生成します |
29 | * |
30 | * @param Define $defines 定義を渡します |
31 | * |
32 | * @return ArrayDefine |
33 | * |
34 | * @throws LogicException Defineクラスの定義が不正なときに発生します |
35 | */ |
36 | public function __construct(Define ...$defines) |
37 | { |
38 | foreach ($defines as $define) { |
39 | /** @var bool $isIndexDefineDuplicate index配列の定義が複数存在するか */ |
40 | $isIndexDefineDuplicate = $this->indexArrayDefineExists && $define->isIndexArrayDefine(); |
41 | |
42 | $errMes = 'Illegal combination of Define classes.'; |
43 | |
44 | if ($isIndexDefineDuplicate) { |
45 | $reasonMes = 'Multiple definitions without key cannot be specified.'; |
46 | $mes = \sprintf('%s %s', $errMes, $reasonMes); |
47 | throw new LogicException($mes); |
48 | } |
49 | |
50 | if ($define->isIndexArrayDefine()) { |
51 | $this->indexArrayDefineExists = true; |
52 | // NOTE: Index配列の場合はkey名の一覧を作らない |
53 | } |
54 | |
55 | if ($define->isAssocArrayDefine()) { |
56 | $this->assocArrayDefineExists = true; |
57 | $this->keys[] = $define->key->getName(); |
58 | } |
59 | |
60 | $isDefineConflict = $this->indexArrayDefineExists && $this->assocArrayDefineExists; |
61 | |
62 | if ($isDefineConflict) { |
63 | $reasonMes = 'Definitions with and without key are mixed.'; |
64 | $mes = \sprintf('%s %s', $errMes, $reasonMes); |
65 | throw new LogicException($mes); |
66 | } |
67 | } |
68 | |
69 | $this->defines = $defines; |
70 | } |
71 | } |