toolkit / stdlib
some stdlib tool library of the php
Installs: 72 088
Dependents: 22
Suggesters: 0
Security: 0
Stars: 8
Watchers: 4
Forks: 1
Open Issues: 0
Requires
- php: >=8.0
- ext-mbstring: *
Requires (Dev)
- psr/container: ^2.0
Suggests
- inhere/php-validate: Very lightweight data validate tool
- dev-master
- v2.0.12
- v2.0.11
- v2.0.10
- v2.0.9
- v2.0.8
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- 1.x-dev
- v1.2.0
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.24
- v1.0.23
- v1.0.22
- v1.0.21
- v1.0.20
- v1.0.19
- v1.0.18
- v1.0.17
- v1.0.16
- v1.0.15
- v1.0.14
- v1.0.13
- v1.0.12
- v1.0.11
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.0.1
This package is auto-updated.
Last update: 2024-10-25 03:07:19 UTC
README
🧰 Stdlib - Useful basic tools library for PHP development.
Contains:
- array, string, number, object helper
- common php, OS env information
More Utils
PhpDotEnv
Dotenv(.env
) file loadAutoLoader
Simple autoloaderObjectBox
simple object containerOptional
like javajava.util.Optional
- and more ...
Install
composer require toolkit/stdlib
String helper
StrBuffer
use Toolkit\Stdlib\Str\StrBuffer; $buf = StrBuffer::new("c"); $buf->prepend('ab') $buf->append('de') $str = (string)$buf; // "abcde" $str = $buf->toString(); // "abcde" // get and clean. $str = $buf->fetch(); // "abcde" $str = $buf->join(','); // "ab,c,de"
Object helper
Object box
ObjectBox
- Simple object container.
use Toolkit\Stdlib\Obj\ObjectBox; $box = ObjectBox::global(); // set $box->set('router', function () { return new MyRouter(); }); $box->set('renderer', [ 'class' => MyRenderer::class, 'tplDir' => 'path/to/dir', ]); // with options for create $box->set('somObj', [ 'class' => MyObject::class, '__opt' => [ // will always create new object. 'objType' => ObjectBox::TYPE_PROTOTYPE, ], ]); // get /** @var MyRouter $router */ $router = $box->get('router'); /** @var MyRenderer $renderer */ $renderer = $box->get('renderer');
Util classes
AutoLoader
AutoLoader
- an simple psr4 loader, can use for tests.
AutoLoader::addFiles([ // alone files ]); $loader = AutoLoader::getLoader(); $loader->addPsr4Map([ 'namespace' => 'path' ]); $loader->addClassMap([ 'name' => 'class file' ]);
Optional
It aims to eliminate excessive if judgments.
Not use Optional:
use Toolkit\Stdlib\Util\Optional; $userModel = UserModel::findOne(23); if ($userModel) { $username = $userModel->name; } else { $username = 'unknown'; }
Use Optional:
use Toolkit\Stdlib\Util\Optional; $username = Optional::ofNullable($userModel) ->map(function ($userModel) { return $userModel->name; })->orElse('unknown');
Use arrow syntax:
use Toolkit\Stdlib\Util\Optional; $username = Optional::ofNullable($userModel) ->map(fn($userModel) => $userModel->name) ->orElse('unknown');
PhpDotEnv
PhpDotEnv
- a simple dont env file loader.
The env config file .env
(must is 'ini' format):
APP_ENV=dev DEBUG=true ; ... ...
Usage:
PhpDotEnv::load(__DIR__, '.env'); env('DEBUG', false); env('APP_ENV', 'prod');
Stream
use Toolkit\Stdlib\Util\Stream\DataStream; use Toolkit\Stdlib\Util\Stream\ListStream; $userList = ListStream::of($userModels) ->filter(fn($userModel) => $userModel->age > 20) // only need age > 20 ->map(function ($userModel) { // only need field: age, name return [ 'age' => $userModel->age, 'name' => $userModel->name, ]; }) ->toArray(); vdump($userList);
PipeFilters
$pf = PipeFilters::newWithDefaultFilters(); $val = $pf->applyString('inhere', 'upper'); // 'INHERE' $val = $pf->applyString('inhere', 'upper|substr:0,3'); // 'INH'