Skip to content

CkSentinel

Job: turn the library's stand-ins for "nothing" into a real null, for every type at once — and pass everything real through completely untouched.

A sentinel is a placeholder value that means "nothing here": an empty string, a zero amount, a 1900-01-01 date, an all-zeros UUID. In memory they are handy, but written to a database or a JSON payload they become lies — a member who never left should have "terminatedAt": null, not a year-one date. CkSentinel::ToNull is the one call you make at that seam. It answers null for every sentinel and hands anything real back exactly as it came in — the same instance, the same type, nothing converted or cleaned. It is built for the hook that runs before a model is saved — an Eloquent saving observer, say — which nulls the nullable columns in one place; the framework plumbing itself stays in the application (see Using it with Laravel, Joomla! and WordPress).

Quick look

use SafeCoder\CommonKit\CkMoney;
use SafeCoder\CommonKit\CkSentinel;
use SafeCoder\CommonKit\CkUuid;

CkSentinel::ToNull('');                              // -> null
CkSentinel::ToNull('0.00');                          // -> null   (a zero amount is "no amount" by default)
CkSentinel::ToNull(CkMoney::ToMoney('0.00'));        // -> null   (a Money of nothing, likewise)
CkSentinel::ToNull(CkUuid::ToUuid('junk'));          // -> null   (the all-zeros UUID a rejected id comes back as)
CkSentinel::ToNull('Mary');                          // -> 'Mary'  (real values pass through untouched)

Here null is not a rejection — it is the honest answer for a value that was only pretending to be there. Nothing else about the value is ever changed.

Methods

ToNull

Every sentinel to an honest null, one verdict.

static function ToNull(
    mixed $input = null,
    bool $treatZerosAsMissing = true
): mixed
Parameter Type Default What it does
input mixed null The value, of any type.
treatZerosAsMissing bool true True (the default) reads a zero number or amount as missing — written as a number or as plain numeric text — so it comes back as null too; pass false to keep zeros while text, dates and identifiers are still checked.

Returns

When You get
The value is a sentinel: null itself, empty or whitespace-only text, a zero number or amount (while the flag is on), NAN or an infinity, a placeholder or year-one date, the all-zeros UUID null
The value is real — or any type not on the list The very same value, untouched, the same instance: 42 stays 42

It never throws — every input has an answer, so null here is honest, not a rejection.

Give it a value of any type (the parameter is input, mixed). If the value is one of the library's sentinels, you get null; otherwise you get the very same value back. What counts as a sentinel:

  • null itself — which is also what a database driver hands you for a NULL cell;
  • empty and whitespace-only text — and, because an amount usually travels as text here, a zero written as plain numeric text, '0' or '0.00' (see the zeros flag below). Text that is not a plain number — '0,00', '$0', 'false', a date — is text, and passes through;
  • a zero int or float, and NAN or an infinity — no database column holds those;
  • a zero in the exact-number objects of the library's own brick/math copy, BigDecimal and BigInteger, judged digit by digit, so no number is missing for its size;
  • a Money whose amount is zero — what CkMoney::ToMoney hands back;
  • placeholder dates such as 1900-01-01 and the year-one date, in any date object — a Carbon, a CkDay, a DateTime, a DateTimeImmutable, in any zone — the same dates CkDate treats as missing;
  • the all-zeros UUID, as a UuidInterface — the identifier CkUuid hands out.
use SafeCoder\CommonKit\CkDate;
use SafeCoder\CommonKit\CkSentinel;

CkSentinel::ToNull('   ');                                   // -> null  (whitespace-only text)
CkSentinel::ToNull(null);                                    // -> null  (a database NULL)
CkSentinel::ToNull(0);                                       // -> null
CkSentinel::ToNull('0');                                     // -> null  (a zero written as text)
CkSentinel::ToNull(NAN);                                     // -> null  (never a real stored number)
CkSentinel::ToNull(new \DateTimeImmutable('1900-01-01'));    // -> null  (an SQL placeholder date)
CkSentinel::ToNull(CkDate::ToCarbon('junk'));                // -> null  (the year-one sentinel)
CkSentinel::ToNull(42);                                      // -> 42
CkSentinel::ToNull(false);                                   // -> false  (a boolean has no sentinel)
CkSentinel::ToNull('0,00');                                  // -> '0,00'  (not a plain number — text, kept)
CkSentinel::ToNull('00:00');                                 // -> '00:00'  (a time of day is text, kept)

$arrived = new \DateTimeImmutable('2026-08-31 14:05:00', new \DateTimeZone('Europe/Bucharest'));
CkSentinel::ToNull($arrived) === $arrived;                   // -> true  (a real date: the same instance, still in its zone)

The result type is mixed. That fits its natural home — a saving hook, where the values already travel untyped through the model's attributes — so you rarely need to cast.

The zeros flag

Sometimes a zero is real — a balance of exactly 0.00, a count of zero items. Pass treatZerosAsMissing: false (it defaults to true) and zeros are kept — as a number, as an amount and as numeric text alike — while text, dates and identifiers are still checked.

use SafeCoder\CommonKit\CkSentinel;

CkSentinel::ToNull(
    input: 0,
    treatZerosAsMissing: false
);   // -> 0  (a real zero, kept on request)

CkSentinel::ToNull(
    input: '0.00',
    treatZerosAsMissing: false
);   // -> '0.00'  (kept as the text it is)

NAN and the infinities stay null even with the flag off — they can never be a real stored value.

Good to know

  • Real values come back untouched — the same instance, the same type. A date keeps the zone it arrived in, a Money its currency, digits stay exactly as they were. ToNull only answers the question "is this a sentinel?" — it never converts. That is the difference from CkDate::ToCarbonOrNull, which reads the value and hands back a new Carbon in UTC: use the reader for parsing, the sentinel for save hooks. The last line of the example above shows it: ToNull($arrived) === $arrived.
  • Each type is judged by its own reader. Text by CkText::NullIfEmpty and CkNumber::IsZero, a number by the CkNumber OrNull readers, a Money by CkMoney::AmountOrNull, a date by CkDate::ToCarbonOrNull, an identifier by CkUuid::ToUuidOrNull — so the verdict is never a second rule: what CkNumber, CkMoney, CkDate and CkUuid call missing is what is missing here.
  • A zero written as text is missing — but only a plain one. '0', '0.0', '-0', '0.00' are what CkNumber reads as exactly zero, and an amount is written that way on its way to a DECIMAL column, so a zero amount is "no amount" however it is spelled. Every other string is text and passes through — '0,00', '$0', 'false', a date, a time.
  • Booleans have no sentinel. false is a real value and always passes through — there is no boolean that means "nothing". There is no time-of-day type either: a time written as text is text, and '00:00' is kept.
  • Types it does not recognize pass through unharmed. An array, a closure, a resource, one of your own classes — anything not on the list above comes back as is; an object that writes itself as text is not read, its text never judged. ToNull can never corrupt a value.
  • A file's bytes are a string here. Real bytes pass through untouched, and no bytes at all — '' — is missing, as empty text is.

If you want errors instead

There is nothing to opt into here. ToNull cannot fail — every input has an answer — so it takes no throwIfInvalid and has no exception type. It is one of the library's few exception-free members by nature; the classes that clean and convert values, like CkUuid, are where throwIfInvalid: true lives.