Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
22 / 22 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
| Define | |
100.00% |
22 / 22 |
|
100.00% |
10 / 10 |
14 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| noKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| key | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| value | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| arrayDefine | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| isIndexArrayDefine | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isAssocArrayDefine | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isKeyValidate | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| isValueValidate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| nestedTypeDefineExists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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 Selen\Schema\Validate\Define\Key; |
| 12 | |
| 13 | class Define |
| 14 | { |
| 15 | /** @var Key */ |
| 16 | public $key; |
| 17 | |
| 18 | /** @var ArrayDefine|null */ |
| 19 | public $arrayDefine; |
| 20 | |
| 21 | /** @var (\Selen\Schema\Validate\ValueValidateInterface|callable)[] */ |
| 22 | public $valueValidateExecutes; |
| 23 | |
| 24 | /** @var bool */ |
| 25 | private $haveCalledValue = false; |
| 26 | |
| 27 | /** @var bool */ |
| 28 | private $haveCalledArrayDefine = false; |
| 29 | |
| 30 | /** |
| 31 | * インスタンスを生成します |
| 32 | * |
| 33 | * @return Define |
| 34 | */ |
| 35 | private function __construct(Key $key) |
| 36 | { |
| 37 | $this->key = $key; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * 添字配列(index)の定義を生成します |
| 42 | */ |
| 43 | public static function noKey(): Define |
| 44 | { |
| 45 | return new self(new Key(null, false)); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * 連想配列(assoc)の定義を生成します |
| 50 | */ |
| 51 | public static function key(string|int $name, bool $require): Define |
| 52 | { |
| 53 | return new self(new Key($name, $require)); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @param ValueValidateInterface|callable $executes 検証値を渡します |
| 58 | * |
| 59 | * @throws \LogicException メソッドの呼び出し順が不正なときに発生します |
| 60 | */ |
| 61 | public function value(ValueValidateInterface|callable ...$executes): Define |
| 62 | { |
| 63 | if ($this->haveCalledValue) { |
| 64 | // value()->value()という呼び出し方をしたときに発生する |
| 65 | throw new \LogicException('Invalid method call. value method cannot be called back to back.'); |
| 66 | } |
| 67 | |
| 68 | if ($this->haveCalledArrayDefine) { |
| 69 | // arrayDefine()->value()という呼び出し方をしたときに発生する |
| 70 | throw new \LogicException('Invalid method call. cannot call value method after arrayDefine.'); |
| 71 | } |
| 72 | |
| 73 | $this->haveCalledValue = true; |
| 74 | $this->valueValidateExecutes = $executes; |
| 75 | return $this; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @throws \LogicException メソッドの呼び出し順が不正なときに発生します |
| 80 | * |
| 81 | * @param Define $define 定義を指定します |
| 82 | */ |
| 83 | public function arrayDefine(Define ...$define): Define |
| 84 | { |
| 85 | if ($this->haveCalledArrayDefine) { |
| 86 | // arrayDefine()->arrayDefine()という呼び出し方をしたときに発生する |
| 87 | throw new \LogicException('Invalid method call. arrayDefine method cannot be called back to back.'); |
| 88 | } |
| 89 | |
| 90 | // NOTE: 引数の指定がない場合はそのまま通す(エラーにしない) |
| 91 | |
| 92 | $this->haveCalledArrayDefine = true; |
| 93 | $this->arrayDefine = new ArrayDefine(...$define); |
| 94 | return $this; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * IndexArray(keyなし)か確認します |
| 99 | * |
| 100 | * @return bool IndexArrayの場合はtrueを、それ以外の場合はfalseを返します |
| 101 | */ |
| 102 | public function isIndexArrayDefine(): bool |
| 103 | { |
| 104 | return $this->key->getName() === null; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * AssocArray(keyあり)か確認します |
| 109 | * |
| 110 | * @return bool AssocArrayの場合はtrueを、それ以外の場合はfalseを返します |
| 111 | */ |
| 112 | public function isAssocArrayDefine(): bool |
| 113 | { |
| 114 | return $this->key->getName() !== null; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * keyの検証処理を実行するか判定します。 |
| 119 | * |
| 120 | * @return bool 変換する場合はtrueを、それ以外の場合はfalseを返します |
| 121 | */ |
| 122 | public function isKeyValidate(): bool |
| 123 | { |
| 124 | if ($this->key->getName() === null) { |
| 125 | return false; |
| 126 | } |
| 127 | return $this->key->getRequire(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * valueの検証処理を実行するか判定します。 |
| 132 | * |
| 133 | * @return bool 変換する場合はtrueを、それ以外の場合はfalseを返します |
| 134 | */ |
| 135 | public function isValueValidate(): bool |
| 136 | { |
| 137 | return $this->valueValidateExecutes !== null; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * ネストされた定義が存在するか確認します |
| 142 | * |
| 143 | * @return bool 存在する場合はtrueを、それ以外の場合はfalseを返します |
| 144 | */ |
| 145 | public function nestedTypeDefineExists(): bool |
| 146 | { |
| 147 | return $this->arrayDefine !== null; |
| 148 | } |
| 149 | } |