| Total Complexity | 23 |
| Total Lines | 194 |
| Duplicated Lines | 0 % |
| Coverage | 88.24% |
| Changes | 0 | ||
| 1 | <?php |
||
| 21 | class MixedRule extends BaseRule implements RuleInterface |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected string $name; |
||
| 27 | /** |
||
| 28 | * @var array<CheckWrapperInterface> |
||
| 29 | */ |
||
| 30 | protected array $checks = []; |
||
| 31 | /** |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | protected bool $isNullable = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param string $name |
||
| 38 | */ |
||
| 39 | 221 | public function __construct(string $name) |
|
| 40 | { |
||
| 41 | 221 | $this->name = $name; |
|
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritDoc} |
||
| 46 | * |
||
| 47 | * @return static |
||
| 48 | */ |
||
| 49 | 23 | public function nullable(): self |
|
| 50 | { |
||
| 51 | 23 | $this->isNullable = true; |
|
| 52 | 23 | return $this; |
|
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritDoc} |
||
| 57 | * |
||
| 58 | * @return static |
||
| 59 | */ |
||
| 60 | 4 | public function truthy(): self |
|
| 61 | { |
||
| 62 | 4 | return $this->check( |
|
| 63 | 4 | CheckBuilder::create(CheckName::TRUTHY) |
|
| 64 | 4 | ->withPredicate(fn ($value) => \boolval($value)) |
|
| 65 | 4 | ->build() |
|
| 66 | 4 | ); |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritDoc} |
||
| 71 | * |
||
| 72 | * @return static |
||
| 73 | */ |
||
| 74 | 4 | public function falsy(): self |
|
| 75 | { |
||
| 76 | 4 | return $this->check( |
|
| 77 | 4 | CheckBuilder::create(CheckName::FALSY) |
|
| 78 | 4 | ->withPredicate(fn ($value) => !\boolval($value)) |
|
| 79 | 4 | ->build() |
|
| 80 | 4 | ); |
|
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritDoc} |
||
| 85 | * |
||
| 86 | * @return static |
||
| 87 | */ |
||
| 88 | 11 | public function equal($value): self |
|
| 95 | 11 | ); |
|
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritDoc} |
||
| 100 | * |
||
| 101 | * @return static |
||
| 102 | */ |
||
| 103 | 6 | public function same($value): self |
|
| 104 | { |
||
| 105 | 6 | return $this->check( |
|
| 106 | 6 | CheckBuilder::create(CheckName::SAME) |
|
| 107 | 6 | ->withPredicate(fn ($actual, $expected) => $actual === $expected) |
|
| 108 | 6 | ->withParams([Param::EXPECTED => $value]) |
|
| 109 | 6 | ->build() |
|
| 110 | 6 | ); |
|
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritDoc} |
||
| 115 | * |
||
| 116 | * @return static |
||
| 117 | */ |
||
| 118 | 217 | public function check(CheckInterface $check, bool $isInterrupting = false): self |
|
| 119 | { |
||
| 120 | 217 | $this->checks[] = new CheckWrapper($check, $isInterrupting); |
|
| 121 | 217 | return $this; |
|
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritDoc} |
||
| 126 | * |
||
| 127 | * @return static |
||
| 128 | */ |
||
| 129 | 3 | public function stopOnViolation(): self |
|
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * {@inheritDoc} |
||
| 136 | * |
||
| 137 | * @return static |
||
| 138 | */ |
||
| 139 | 2 | public function stopOnAnyPriorViolation(): self |
|
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritDoc} |
||
| 149 | */ |
||
| 150 | 221 | public function validate($value): void |
|
| 151 | { |
||
| 152 | 221 | $this->execute($value); |
|
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritDoc} |
||
| 157 | */ |
||
| 158 | public function getName(): string |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritDoc} |
||
| 165 | */ |
||
| 166 | public function isValid($value): bool |
||
| 167 | { |
||
| 168 | try { |
||
| 169 | $this->validate($value); |
||
| 170 | return true; |
||
| 171 | } catch (ValidationError $e) { |
||
| 172 | return false; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * {@inheritDoc} |
||
| 178 | */ |
||
| 179 | 221 | protected function execute($value): ValidationResultInterface |
|
| 215 | } |
||
| 216 | } |
||
| 217 |