PHP code example of simp / php-toolkit

1. Go to this page and download the library: Download simp/php-toolkit library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

simp / php-toolkit example snippets


$name  = new Str("Chance Nyasulu");
$price = new Num(1234.5);
$list  = new Arr([3, 1, 2]);
$phone = new Phone("0987678987", "MW");
$email = new Email("[email protected]");
$country = new Country("MW");

$name = new Str("Chance Nyasulu");
$name = Str::create("Chance Nyasulu");
$name = Str::of("Chance Nyasulu"); // shorthand alias

$list = new Arr([1, 2, 3]);
$list = Arr::create([1, 2, 3]);
$list = Arr::of(1, 2, 3);          // variadic shorthand



Str::of(" chance nyasulu ")->trim()->title();      // "Chance Nyasulu"
Str::of("Chance Nyasulu")->camel();                // "chanceNyasulu"
Str::of("Chance Nyasulu")->snake();                // "chance_nyasulu"
Str::of("Chance Nyasulu")->kebab();                // "chance-nyasulu"
Str::of("Hello, World! 2026")->slug();              // "hello-world-2026"
Str::of("The quick brown fox")->limit(9);           // "The quick..."
Str::of("[abc]")->between('[', ']');                // "abc"
Str::of("0987678987")->mask('*', 3);                // "098*******"
Str::of("Chance Nyasulu")->contains('nyasulu', true); // true (case-insensitive)
Str::random(12);                                     // random 12-char hex string
Str::uuid4();                                        // random UUID v4

$people = new Arr([
    ['name' => 'Chance', 'age' => 30, 'city' => 'Lilongwe'],
    ['name' => 'Grace',  'age' => 24, 'city' => 'Blantyre'],
    ['name' => 'John',   'age' => 41, 'city' => 'Lilongwe'],
]);

$people->sortByDesc('age')->pluck('name')->implode(', '); // "John, Chance, Grace"
$people->groupBy('city')['Lilongwe']->pluck('name');       // Arr(["Chance", "John"])
$people->avg('age');                                        // 31.67
$people->filter(fn ($p) => $p['age'] > 25)->values();

$data = new Arr(['address' => ['city' => 'Lilongwe']]);
$data->get('address.city');            // "Lilongwe"
$data->set('address.zip', '10101');    // new Arr with the value added
$data->has('address.city');            // true

LazyCollection::range(1, PHP_INT_MAX)      // infinite
    ->filter(fn ($n) => $n % 7 === 0)
    ->map(fn ($n) => $n * $n)
    ->take(5)
    ->toArray();                            // [49, 196, 441, 784, 1225] - only 35 numbers ever touched

$errors = LazyCollection::make(function () {
        $handle = fopen('huge.log', 'r');
        while (($line = fgets($handle)) !== false) {
            yield rtrim($line);
        }
        fclose($handle);
    })
    ->filter(fn ($line) => str_contains($line, 'ERROR'))
    ->take(10)
    ->toArray(); // stops reading the file after the 10th match

Num::of(1234.5)->currency('MK');       // "MK 1,234.50"
Num::of(1234.5)->format(2);            // "1,234.50"
Num::of(3)->ordinal();                 // "3rd"
Num::of(11)->ordinal();                // "11th"
Num::of(1994)->roman();                // "MCMXCIV"
Num::of(2026)->toWords();              // "two thousand twenty-six"
Num::of(1048576)->humanFileSize();     // "1 MB"
Num::of(2)->add(3)->multiply(4);       // Num(20) — chainable math
Num::of(150)->clamp(0, 100);           // Num(100)
Num::of(17)->isPrime();                // true
Num::average([1, 2, 3, 4]);            // 2.5 (static)
Num::random(1, 100);                   // random int (static)

$mw = new Country("MW");          // or Country::create("Malawi") / Country::create("MWI")
$mw->getName();                   // "Malawi"
$mw->getAlpha2();                 // "MW"
$mw->getAlpha3();                 // "MWI"
$mw->getDialCode();               // "265"
$mw->getDialCodeFormatted();      // "+265"
$mw->getTld();                    // ".mw"
$mw->getFlagUrl();                // flag image URL
$mw->getTimezone();               // "+02:00"

Country::all();                   // array of all 249 Country objects
Country::search('mala');          // array of Country objects matching "mala"
Country::random();                // random Country
Country::fromDialCode('+44');     // Country("GB")
Country::tryFind('Narnia');       // null instead of throwing

$phone = new Phone("0987678987", "MW");     // national number + ISO country
$phone = Phone::create("+265987678987");    // or a full international number — country auto-detected

$phone->toE164();            // "+265987678987"
$phone->toInternational();   // "+265 987 678 987"
$phone->toNational();        // "0987 678 987"
$phone->getCountry();        // Country("MW")
$phone->getDialCode();       // "265"
$phone->getNationalNumber(); // "987678987"
$phone->isValid();           // true (plausible digit count)
$phone->equals($otherPhone);

Phone::isValidNumber("0987678987", "MW"); // true, no exception
Phone::tryParse("garbage");               // null instead of throwing

Phone::create("2025551234", "US")->toNational(); // "(202) 555-1234"

Phone::registerMobilePrefixes('MW', ['88', '89', '99', '31']);
$phone->isMobile(); // true/false once a rule exists for that country, otherwise null

$email = new Email("[email protected]");

$email->isValid();       // true (RFC-ish, via filter_var)
$email->getLocalPart();  // "Chance.Nyasulu+news"
$email->getDomain();     // "Gmail.com"
$email->getTld();        // "com"
$email->isFree();        // true (Gmail, Yahoo, Outlook, iCloud, etc.)
$email->isDisposable();  // false (checked against a small built-in list)
$email->normalize();     // Email("[email protected]") — dots/+tag stripped for Gmail
$email->mask();          // "Ch*****************@Gmail.com"
$email->gravatarUrl();   // Gravatar image URL
$email->equals($other);  // compares normalized addresses by default
$email->hasMxRecord();   // live DNS check (needs network at runtime)

Email::registerFreeProvider('mycompany-webmail.com');
Email::registerDisposableProvider('some-temp-mail-service.com');
bash
php tests/smoke_test.php      # Str, Num, Country, Phone, Email - ~60 assertions
php tests/arr_lazy_test.php   # Arr, LazyCollection - ~60 assertions
php examples/demo.php         # runnable usage walkthrough

php-toolkit/
├── composer.json
├── autoload.php          # zero-dependency PSR-4 autoloader (works without Composer)
├── src/
│   ├── Str.php
│   ├── Num.php
│   ├── Arr.php
│   ├── LazyCollection.php
│   ├── Phone.php
│   ├── Email.php
│   ├── Country.php
│   ├── Support/CountryRepository.php
│   └── Exceptions/{InvalidPhoneNumberException,InvalidEmailException,UnknownCountryException}.php
├── resources/countries.json
├── examples/demo.php
└── tests/
    ├── smoke_test.php       # Str, Num, Country, Phone, Email
    └── arr_lazy_test.php    # Arr, LazyCollection