GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - php-toolkit/stdlib: Standard and basic PHP tool library. Helper utils for string, array, object, env, os and more.

BranchesTags
Open more actions menu

Latest commit

 

History

208 Commits
208 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
src
src
 
 
test
test
 
 
.gitignore
.gitignore
 
 
.nojekyll
.nojekyll
 
 
.php-cs-fixer.php
.php-cs-fixer.php
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
README.zh-CN.md
README.zh-CN.md
 
 
composer.json
composer.json
 
 
index.html
index.html
 
 
phpunit.xml
phpunit.xml
 
 

Repository files navigation

StdLib

License Php Version Support Latest Stable Version Unit Tests Docs on pages

🧰 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 load
  • AutoLoader Simple autoloader
  • ObjectBox simple object container
  • Optional like java java.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'

License

MIT

About

Standard and basic PHP tool library. Helper utils for string, array, object, env, os and more.

php-toolkit.github.io/stdlib/

Topics

dotenvenvironmentoptionalphpphp-arrayphp-libraryphp-stringsstdlib

Resources

Readme
MIT license
Activity
Custom properties

Stars

9 stars

Watchers

1 watching

Forks

1 fork
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.