RockLobsterInc\Swv

InvalidityException{} │ final │ CF7 1.0

Exception that represents a validation error.

Хуков нет.

Использование

$InvalidityException = new InvalidityException();
// use class methods

Методы

  1. public __construct(AbstractRule $rule, array $options = [])
  2. public getField()
  3. private getErrorMessage()

Код InvalidityException{} CF7 6.2.4.1

final class InvalidityException extends \Exception
{
    /**
     * The SWV rule who has thrown this error.
     */
    public readonly AbstractRule $rule;

    /**
     * The specific cause of this exception.
     */
    public readonly mixed $cause;

    /**
     * Constructor.
     *
     * @param AbstractRule $rule SWV rule.
     */
    public function __construct(AbstractRule $rule, array $options = [])
    {
        $this->rule = $rule;
        $this->cause = $options["cause"] ?? null;

        parent::__construct($this->getErrorMessage());
    }

    /**
     * Retrieves the validation error message.
     */
    private function getErrorMessage(): string
    {
        if ($this->cause instanceof self) {
            return $this->cause->getMessage();
        }

        return $this->rule->error ?? "";
    }

    /**
     * Retrieves the field name where the validation error occurs.
     */
    public function getField(): string
    {
        if ($this->cause instanceof self) {
            return $this->cause->rule->field ?? "";
        }

        return $this->rule->field ?? "";
    }
}