| Total Complexity | 14 |
| Total Lines | 63 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 11 | class ExactlyOne implements IRuleWithArgs, IRuleWithErrorPlaceholders |
||
| 12 | { |
||
| 13 | /** @var array The name of the fields to compare to */ |
||
| 14 | protected $fieldNames = []; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @inheritdoc |
||
| 18 | */ |
||
| 19 | public function getErrorPlaceholders(): array |
||
| 20 | { |
||
| 21 | $placeholders = []; |
||
| 22 | |||
| 23 | foreach ($this->fieldNames as $idx => $fieldName) { |
||
| 24 | $placeholders['other' . ($idx + 1)] = $fieldName; |
||
| 25 | } |
||
| 26 | |||
| 27 | return $placeholders; |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @inheritdoc |
||
| 32 | */ |
||
| 33 | public function getSlug(): string |
||
| 34 | { |
||
| 35 | return 'exactlyOne'; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @inheritdoc |
||
| 40 | */ |
||
| 41 | public function passes($value, array $allValues = []): bool |
||
| 42 | { |
||
| 43 | $count = 0; |
||
| 44 | |||
| 45 | if (is_string($value) && $value) { |
||
| 46 | $count++; |
||
| 47 | } |
||
| 48 | |||
| 49 | foreach ($this->fieldNames as $fieldName) { |
||
| 50 | if (isset($allValues[$fieldName]) && is_string($allValues[$fieldName]) && $allValues[$fieldName]) { |
||
| 51 | $count++; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | return $count === 1; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @inheritdoc |
||
| 60 | */ |
||
| 61 | public function setArgs(array $args) |
||
| 74 | } |
||
| 75 | } |
||
| 76 |