Skip to content

Getting started

This page takes you from nothing to your first CommonKit call. It assumes you can run php in a terminal — nothing more.

What you need

  • PHP 8.2 or newer, with the dom, intl and mbstring extensions. Check with php --version, and php -m should list all three — dom is part of every ordinary PHP build.
  • Composer, for a Composer project — the autoloader that finds the classes comes from it. Joomla! and WordPress need none: the library installs as an extension or a plugin, and the platform autoloads it.
  • Any editor. PhpStorm, VS Code or anything else all work.

Get the library

In a Composer project the library is one require away. It is a Composer package served from its own git repository, SafeCoder-Software/SCS-CommonKit-PHP, tagged at every release — not from Packagist — so your composer.json names the repository once:

{
    "repositories": [
        { "type": "vcs", "url": "https://github.com/SafeCoder-Software/SCS-CommonKit-PHP.git" }
    ]
}

then, from your project:

composer require safecoder/commonkit:^1.0

^1.0 follows every release of version 1. The repository is private, so Composer needs to be let in once: on your machine with a GitHub token, composer config --global --auth github-oauth.github.com <token>; on a server that holds a deploy key, with the SSH address instead, git@github.com:SafeCoder-Software/SCS-CommonKit-PHP.git. From here on Composer's autoloader knows every class.

Without a token. A server with no GitHub access, or a project that keeps what it deploys beside it, installs the release's archive instead: each version on the repository's releases page carries PHP-SafeCoder.CommonKit.<version>.zip, the library as a Composer artifact — an archive whose composer.json names its version, so Composer reads it as it would read the repository's tag. Put it in a folder of your project, packages/, say, name the folder as a repository of type artifact, and require the package the same way:

{
    "repositories": [
        { "type": "artifact", "url": "packages/" }
    ]
}
composer require safecoder/commonkit:^1.0

No token, no deploy key. Updating is putting the next version's archive beside it and running composer update safecoder/commonkit.

Where it goes in your app. Laravel, Symfony and any other Composer project take it either way — Using it with Laravel shows where the calls go. Joomla! takes it as an extension: each version on the repository's releases page carries Joomla-SafeCoder.CommonKit.<version>.zip, installed the way Joomla! installs anything, and from then on the classes autoload — Using it with Joomla!. WordPress takes it as a plugin: WordPress-SafeCoder.CommonKit.<version>.zip from the same page, uploaded the way WordPress installs any plugin and activated, and from then on the classes autoload in any theme or plugin — Using it with WordPress. A project with neither Composer nor a package of its own takes PHP-SafeCoder.CommonKit.<version>.zip, the library as a folder — the same archive unpacks to src/, its composer.json, the licence and AGENTS.md — or copies src/ from the repository, and adds one PSR-4 rule, SafeCoder\CommonKit\ to that folder, plus the one files entry the library's composer.json lists.

Your first call

Start a file with <?php and require __DIR__ . '/vendor/autoload.php';, as every file that uses Composer's autoloader does. Add a use line per class, then call any of them:

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

$name    = CkText::Name('  mary-jane  smith ');      // -> 'Mary-Jane Smith'
$price   = CkMoney::Amount('$1,299.99');             // -> 1299.99
$country = CkCountry::Alpha2('U.S.A.');              // -> 'US'

echo "$name from $country pays $price" . PHP_EOL;

Run it with php and the file's name. Notice what you did not write: no try/catch, no isset, no is_numeric dance, no str_replace('$', '', …). That is the whole idea — bad input gives you a calm, predictable value instead of an exception. What those values are, and how to change them, is on How it works.

The four namespaces

Namespace What lives there
SafeCoder\CommonKit the seventeen helper classes, CkRequest (the current request, read once), CkSanitizer (the markup an editor writes, kept safe) and CkMedia (the media a link names), with CkSettings (every limit and default) and CkDay (a calendar day) — you almost always need only this one
SafeCoder\CommonKit\Media the objects CkMedia hands back — CkYouTube, CkVimeo, CkVideo, CkImage, CkAudio and their players and tag — which you name only in a type hint
SafeCoder\CommonKit\Enums small option enums like DateOrder and TextCase
SafeCoder\CommonKit\Exceptions the Ck…Exception types, if you opt into throwing

Where next

  • 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.
  • Or jump straight to a class page — CkText is a good first stop.