Skip to content

Validation Results

All four identifier classes (PeselIdentifier, NipIdentifier, RegonIdentifier, KrsIdentifier) expose the same two methods: isValid() and validate(). Neither ever throws an exception.

validate() returns a SlashLab\Numerik\Result\ValidationResult readonly class.

PropertyTypeDescription
$isValidbooltrue when validation passed.
$failureslist<ValidationFailure>Empty array on success; one or more failures on failure.
MethodReturn typeDescription
isFailed()boolInverse of $isValid.
getFailures()list<ValidationFailure>Returns the failures array.
getFirstFailure()ValidationFailure|nullFirst failure, or null if valid.
hasFailureReason(ValidationFailureReason $reason)booltrue if any failure matches the given reason.
use SlashLab\Numerik\Numerik;
use SlashLab\Numerik\Enums\ValidationFailureReason;
// Passing result
$result = Numerik::pesel()->validate('92060512186');
$result->isValid; // true
$result->isFailed(); // false
$result->failures; // []
$result->getFirstFailure(); // null
// Failing result
$result = Numerik::nip()->validate('0000000000');
$result->isValid; // false
$result->isFailed(); // true
// Inspect the first (and usually only) failure
$failure = $result->getFirstFailure();
$failure->reason; // ValidationFailureReason::InvalidFormat
$failure->message; // 'NIP tax office code cannot be 000.'
// Check for a specific reason
$result->hasFailureReason(ValidationFailureReason::InvalidChecksum); // false
$result->hasFailureReason(ValidationFailureReason::InvalidFormat); // true

Each item in $failures is a SlashLab\Numerik\Result\ValidationFailure readonly class.

PropertyTypeDescription
$reasonValidationFailureReasonEnum case identifying the failure category.
$messagestringHuman-readable description in English.

SlashLab\Numerik\Enums\ValidationFailureReason is a backed string enum.

CaseValueDescription
InvalidLengthinvalid_lengthInput has the wrong number of digits.
InvalidCharactersinvalid_charactersUnexpected characters are present after stripping allowed separators.
InvalidFormatinvalid_formatCorrect length and characters, but a structural rule is violated (e.g. NIP tax office code 000).
CaseValueDescription
InvalidChecksuminvalid_checksumThe computed checksum does not match the checksum digit.
CaseValueDescription
InvalidDateinvalid_dateThe date encoded inside the identifier is not a real calendar date.
FutureDatefuture_dateThe encoded birth date is in the future.
InvalidMonthinvalid_monthThe month encoding does not correspond to any known century range.
CaseValueDescription
AllZerosall_zerosAll digits are zero — structurally plausible but semantically invalid.
AllSameDigitall_same_digitAll digits are the same non-zero value.

ValidationResult exposes three static constructors used internally and in tests:

// Success
ValidationResult::pass();
// Failure with a list of failures
ValidationResult::fail([
new ValidationFailure(ValidationFailureReason::InvalidChecksum, 'Checksum mismatch.'),
]);
// Failure with a single reason — shorthand
ValidationResult::failWithReason(
ValidationFailureReason::InvalidLength,
'Expected 11 digits, got 10.',
);
If this saved you time → ☕ Buy me a coffee