[null, 'Boolean value', 'boolean|string|integer'], 'notTrueMessage' => [null, 'Translation key or message for not true value', 'string'], 'notFalseMessage' => [null, 'Translation key or message for not false value', 'string'], ]; /** * Check if $value matches the expectation given to the validator. * If it does not match, the function adds an error to the result. * * Also testing for '1' (true), '0' and '' (false) because casting varies between * tests and actual usage. This makes the validator loose but still keeping functionality. */ public function isValid(mixed $value): void { // see comment above, check if expectation is NULL, then nothing to do! if ($this->options['is'] === null) { return; } switch (strtolower((string)$this->options['is'])) { case 'true': case '1': $expectation = true; break; case 'false': case '': case '0': $expectation = false; break; default: $this->addError('The given expectation is not valid.', 1361959227); return; } if ($value !== $expectation) { if (!is_bool($value)) { $this->addError($this->translateErrorMessage($this->notTrueMessage), 1361959230); } else { if ($expectation) { $this->addError($this->translateErrorMessage($this->notTrueMessage), 1361959228); } else { $this->addError($this->translateErrorMessage($this->notFalseMessage), 1361959229); } } } } }