Skip to content

CommonKit for PHP

Version 1.0.0 PHP 8.2 or newer 12,900+ tests MIT licence

Small, safe helpers for messy data.

Real-world data is messy. A form sends " U.S.A. " when you wanted "US". A database holds 1900-01-01 because nobody knew the real date. An API sends "$1,299.99" where you wanted a number. And one bad value in the wrong place can crash your whole app.

CommonKit takes those messy values and hands you clean, typed, safe ones — without ever crashing.

Get started Try the 15-minute tutorial

This site documents library version 1.0.0; the changelog says what each version brought.

use SafeCoder\CommonKit\CkCountry;
use SafeCoder\CommonKit\CkDate;
use SafeCoder\CommonKit\CkEmail;
use SafeCoder\CommonKit\CkMoney;
use SafeCoder\CommonKit\CkText;

CkText::Name('  mary-jane  smith ');       // -> 'Mary-Jane Smith'
CkMoney::Amount('$1,299.99');              // -> 1299.99
CkCountry::Alpha2('U.S.A.');               // -> 'US'
CkDate::ToCarbonOrNull('1900-01-01');      // -> null   (a placeholder, not a real date)
CkEmail::Address('not an email');          // -> ''     (rejected — but no crash)

The one promise

Nothing throws by default. Every method accepts anything — null, garbage text, the wrong type, an array where a string was expected, a ten-megabyte string — and answers with a calm, predictable value: string methods answer '', converters answer your fallback, the …OrNull readers answer null and never throw at all. Want an exception instead? Nearly every method takes throwIfInvalid: true. Want the reason in words, for a form? Every class that checks a value has an Explain. The full rules are on How it works.

What's inside

  • Text people type


    Names, emails, phones, identity numbers, postal codes and countries — cleaned, checked, masked for display, judged with a verdict for validation code — the markup an editor writes, kept safe, and the media a link names: a video, an image, a sound.

    CkText · CkEmail · CkUS · CkIntl · CkCountry · CkSanitizer · CkMedia

  • Numbers, money, time and place


    Every number a column holds, yes-or-no values that keep "unknown" apart from "no", amounts with symbols, dates in every shape, coordinates with real math.

    CkNumber · CkBool · CkMoney · CkDate · CkGeo

  • Straight off the wire


    JSON, forms, query strings and the request itself into one bag; typed values out of it, or straight off the current request; identifiers and network gates.

    CkPayload · CkDictionary · CkRequest · CkUuid · CkIP · CkWeb · CkFile

  • Honest storage


    One call turns every placeholder — empty text, zeros, 1900-01-01 — into a real NULL on the way to the database.

    CkSentinel · Laravel · Joomla! · WordPress

The pipeline

The classes are built to work as a chain, from the wire to the database:

wire text ──CkPayload──▶ the bag (an array) ──CkDictionary──▶ typed values
              ──domain classes (CkEmail, CkMoney, CkDate, …)──▶ clean values
              ──CkSentinel::ToNull──▶ honest NULLs for storage

In a handler, the first two steps are one call: CkRequest reads the current request on first use and hands out typed values, CkRequest::Number('id')->Integer(). Follow one order through all of it in Your first payload — and see the whole handler it becomes at the end.

Start here

Guides

Built to be trusted

  • 12,900+ tests run on every build — a test class per public method, and four suites that run every public method automatically: one with hostile input — arrays, objects, NAN, invalid UTF-8, 70,000-character strings — to prove nothing throws, not even a warning; one against the library's promises (valid text out, cleaning twice is cleaning once, a validator agrees with its reader, every call bounded in time, no call leaves a trace in the interpreter's state); one proving that a reader, its Explain, its exception and its …OrNull twin never disagree about a value; and one running every call under five foreign locales to prove the answer never changes.
  • Static analysis at its strictest — PHPStan at its highest level over the library and its tests, with no suppressions and no baseline: a finding is fixed at its cause.
  • Parsers checked value for value — the readers for email addresses, IP addresses, URLs, base64 and paths are written out in the library, and each was checked against a reference implementation over hundreds of thousands of values.
  • The store clock, in seven time zones — the end-to-end run that sets the library's time-zone constants and asks every date method for its answer, once per zone, from Chicago and Newfoundland to Lord Howe Island and Kiritimati.
  • Carried, not required — the packages the library stands on — brick/math, brick/money, Carbon, ramsey/uuid, masterminds/html5 — travel inside it under its own namespace, so it runs on the versions it chose whatever the host application ships, and its composer.json requires no package at all.
  • PHP 8.2 or newer, with the dom, intl and mbstring extensions — and the server's locale and time zone are never consulted: the same output on every machine.
  • Every result comment on this site is checked against the library before it is published.

Where next

  • Getting started — from nothing to your first call: the requirements, the autoloader, the use line.
  • Why not just cast? — the traps (bool), (int), strtotime, json_decode and the locale lay for you, and the library's answer to each.
  • How it works — the five rules, and which reader to call.
  • Your first payload — a fifteen-minute tutorial: a messy order payload to clean, typed values, and the whole handler at the end.
  • Setting up your file places — the platform's folders for every kind of file, or one root of your own, and the rules each place keeps.
  • Changelog — what each version brings.