Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| ArrayDefine | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
19 / 19 |
|
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\Exchange; |
| 10 | |
| 11 | use LogicException; |
| 12 | |
| 13 | class ArrayDefine |
| 14 | { |
| 15 | /** @var Define[] key定義 */ |
| 16 | public $defines; |
| 17 | |
| 18 | /** |
| 19 | * ArrayDefineインスタンスを生成します |
| 20 | * |
| 21 | * @param Define $defines 定義を渡します |
| 22 | * |
| 23 | * @return ArrayDefine |
| 24 | * |
| 25 | * @throws LogicException Defineクラスの定義が不正なときに発生します |
| 26 | */ |
| 27 | public function __construct(Define ...$defines) |
| 28 | { |
| 29 | $indexArrayDefineExists = false; |
| 30 | $assocArrayDefineExists = false; |
| 31 | |
| 32 | foreach ($defines as $define) { |
| 33 | /** @var bool $isIndexDefineDuplicate index配列の定義が複数存在するか */ |
| 34 | $isIndexDefineDuplicate = $indexArrayDefineExists && $define->isIndexArrayDefine(); |
| 35 | |
| 36 | $errMes = 'Illegal combination of Define classes.'; |
| 37 | |
| 38 | if ($isIndexDefineDuplicate) { |
| 39 | $reasonMes = 'Multiple definitions without key cannot be specified.'; |
| 40 | $mes = \sprintf('%s %s', $errMes, $reasonMes); |
| 41 | throw new LogicException($mes); |
| 42 | } |
| 43 | |
| 44 | if ($define->isIndexArrayDefine()) { |
| 45 | $indexArrayDefineExists = true; |
| 46 | } |
| 47 | |
| 48 | if ($define->isAssocArrayDefine()) { |
| 49 | $assocArrayDefineExists = true; |
| 50 | } |
| 51 | |
| 52 | $isDefineConflict = $indexArrayDefineExists && $assocArrayDefineExists; |
| 53 | |
| 54 | if ($isDefineConflict) { |
| 55 | $reasonMes = 'Definitions with and without key are mixed.'; |
| 56 | $mes = \sprintf('%s %s', $errMes, $reasonMes); |
| 57 | throw new LogicException($mes); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | $this->defines = $defines; |
| 62 | } |
| 63 | } |