1. Go to this page and download the library: Download efureev/support 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/ */
efureev / support example snippets
use Php\Support\Helpers\Arr;
use Php\Support\Helpers\Str;
use Php\Support\Structures\Collections\ArrayCollection;
Arr::get($config, 'database.connections.pgsql.host', '127.0.0.1');
Str::slugify('Привет, мир!'); // 'privet-mir'
(new ArrayCollection($orders))
->filter(static fn(array $o) => $o['paid'])
->groupBy('customer_id')
->map(static fn(ArrayCollection $c) => $c->sum('total'));
use Php\Support\Helpers\Str;
Str::toSnake('getHTTPResponse'); // 'get_http_response'
Str::toCamel('user_first_name'); // 'UserFirstName'
Str::toLowerCamel('user_id'); // 'userId'
Str::toKebab('BackgroundColor'); // 'background-color'
Str::toSnake('ПриветМир'); // 'привет_мир' — not only Latin
Str::toCamel('über_straße'); // 'ÜberStraße'
Str::slugify('Привет, мир!'); // 'privet-mir'
Str::slugify('Il était une fois'); // 'il-etait-une-fois'
Str::slugify('Hello World', '_'); // 'hello_world'
Str::truncate('The quick brown fox', 9); // 'The...' — at a word boundary
Str::limit('The quick brown fox', 9); // 'The quick...' — at an exact length
Str::mask('4111111111111111', '*', 4, 8); // '4111********1111'
Str::padLeft('7', 3, '0'); // '007'
Str::squish(" too much \n space "); // 'too much space'
Str::uuid(); // '9f8c1f1e-...' — RFC 4122 v4
Str::ulid(); // '01J...' — sortable by creation time
Str::random(); // 16 random alphanumerics from the CSPRNG
use Php\Support\Structures\Collections\HashCollection;
$handlers = new HashCollection();
$handlers[] = new SendEmail(); // keyed by SendEmail::class
$handlers->get(SendEmail::class);
use Php\Support\Traits\{Maker, UseErrorsBox, Metable};
final class Report
{
use Maker; // Report::make(...$args)
use UseErrorsBox; // addError(), errors(), firstError(), hasErrors()
use Metable; // withMeta(), metaAttribute()
}
Report::make()
->withMeta(['source' => 'cron'])
->addError('row 12 is malformed');
use Php\Support\Traits\HasPrePostActions;
final class Importer
{
use HasPrePostActions;
public function import(array $rows): bool
{
if (!$this->runActions('before', $rows)) {
return false; // a listener said no
}
// ... do the work
$this->runActions('after', count($rows));
return true;
}
}
$importer = (new Importer())
->addCallbackAction('before', static fn(array $rows) => $rows !== [])
->addCallbackAction('after', static fn(int $count) => $logger->info("imported {$count}"));
$importer->import([]); // false — the guard vetoed it, nothing ran
$importer->import([['id' => 1]]); // true
use Php\Support\ConditionalHandler;
$auditLink = ConditionalHandler::make(
static fn(User $user) => ['label' => 'Audit log', 'href' => '/audit'],
)->handleIf(static fn(User $user) => $user->isAdmin());
$auditLink($admin); // ['label' => 'Audit log', 'href' => '/audit']
$auditLink($guest); // null — the handler was never called
use Php\Support\Helpers\Arr;
use Php\Support\Types\GeoPoint;
Arr::toPostgresArray(['a,b', 'plain', null]); // '{"a,b",plain,NULL}'
Arr::fromPostgresArray('{"a,b",plain}'); // ['a,b', 'plain']
$point = GeoPoint::castFromDatabase('(37.6,55.7)');
$point->toJson(); // '{"longitude":37.6,"latitude":55.7}'
use Php\Support\Testing\{AdditionalAssertionsTrait, TestingHelper};
final class ReportTest extends TestCase
{
use AdditionalAssertionsTrait;
use TestingHelper;
public function testItIsMakeable(): void
{
static::assertClassUsesTraits(Report::class, [Maker::class]);
self::assertSame('hidden', static::runProtectedMethod(new Report(), 'internal'));
self::assertSame(1, static::getProperty(new Report(), 'privateCounter'));
}
}