Skip to content

CkTextException

What CkText throws — only when asked.

Raised by any CkText method called with throwIfInvalid: true when neither the value nor the fallback can be used. Never thrown otherwise: by default every CkText method answers empty, null or a mask's placeholder instead — and ToStringOrNull cannot raise it at all: it takes no throwIfInvalid and never throws, null is its answer for any error. Final, and extends CkException.

The message names the method, shows the value with its type, says what it was meant to be and why it failed:

CkText.Digits: cannot use "abc" (String) as digits — not between 1 and 64 digits.

Properties

Property Type What it holds
Expected string What the value was meant to be — 'usable text', 'digits', 'a number to write as text'.
Input mixed From CkException: the value that failed, untouched.
Reason string From CkException: why, in a few words.

All three are public and read-only: $error->Expected, $error->Input, $error->Reason.

Quick look

use SafeCoder\CommonKit\CkText;
use SafeCoder\CommonKit\Exceptions\CkTextException;

try {
    CkText::Digits(
        input: 'abc',
        throwIfInvalid: true
    );
} catch (CkTextException $error) {
    echo $error->Reason;      // -> 'not between 1 and 64 digits'
    echo $error->Expected;    // -> 'digits'
}