koded / stdlib
A standard library for shareable classes and functions
Installs: 9 775
Dependents: 5
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 0
Open Issues: 0
pkg:composer/koded/stdlib
Requires
- php: ^8.1
- ext-ctype: *
- ext-dom: *
- ext-json: *
- ext-libxml: *
- ext-mbstring: *
- ext-simplexml: *
Requires (Dev)
- phpbench/phpbench: ^1
- phpunit/phpunit: ^9
Suggests
- igbinary: igbinary module for de/serialization
- msgpack: msgpack module for de/serialization
README
A standard library for shareable classes and functions.
Classes
Immutable
get(string $index, $default = null)has($index): boolequals(string $propertyA, string $propertyB): boolfind(string $index, $default = null)extract(array $keys): arrayfilter(iterable $data, string $prefix, bool $lowercase = true, bool $trim = true): arraynamespace(string $prefix, bool $lowercase = true, bool $trim = true)count()toArray(): arraytoJSON(int $options = 0): stringtoXML(string $root): stringtoArguments(): Arguments
Arguments
(implements Immutable methods)
set(string $index, $value)import(array $values)upsert(string $index, $value)bind(string $index, &$variable)pull(string $index, $default = null)delete(string $index)clear()toImmutable(): Immutable
ExtendedArguments
ExtendedArguments extends Arguments
flatten(): ExtendedArguments
Supports dot-notation for index names. Example:
$args = new \Koded\Stdlib\ExtendedArguments([ 'foo' => [ 'bar' => [ 'baz' => 42 ] ] ]); $args->get('foo.bar'); // returns ['baz' => 42] $args->get('foo.bar.baz'); // returns 42 $args->flatten(); // returns ['foo.bar.baz' => 42] $args->set('foo.bar', 'baz'); // ['foo' => ['bar' => 'baz']]
Config
Class Config works as a parameter bag that provides ways to fill it
from files or other Config instances. There are 2 common patterns
to populate the config,
either you can fill the Config instance from config files:
$app->config()->fromPhpFile('myconfig.php'); $app->config()->fromJsonFile('myconfig.json'); $app->config()->fromEnvFile('.env'); $app->config()->fromIniFile('myconfig.ini');
or alternatively you can define the configuration options in the instance
that calls fromObject,
$app->config()->fromObject(MyConfig::class); $app->config()->fromObject($myconfig); // $myconfig is instance of Config
Other interesting way to load configuration is from an environment variable that points to a file
$app->config()->fromEnvVariable('MY_APP_SETTINGS');
In this case, before launching the application you have to set the env variable to the file you want to use. On Linux and OSX use the export statement
export MY_APP_SETTINGS='/path/to/config/file.php'
or somewhere in your app bootstrap phase before constructing the Api instance
putenv('MY_APP_SETTINGS=/path/to/config/file.php');
fromEnvironment( array $variableNames, string $namespace = '', bool $lowercase = true, bool $trim = true ): ConfigurationfromJsonFile(string $file): ConfigurationfromPhpFile(string $file): ConfigurationfromEnvVariable(string $variable): ConfigurationfromIniFile(string $file): ConfigurationfromObject($object): ConfigurationwithParameters(array $parameters): Configurationsilent(bool $silent): Configurationbuild(string $context): Configuration
Mime
type(string $extension, int $index = 0): stringtypes(string $extension): arraysupports(string $type): boolextensions(string $type): array
UUID
Class UUID generates Universally Unique Identifiers following the RFC 4122.
v1(string|null $address = null): stringv3(string $namespace, $name): stringv4(): stringv5(string $namespace, string $name): stringvalid(string $uuid): boolmatches(string $uuid, int $version = 4): booltoBase64(string $uuid): stringfromBase64(string $base64): string
ULID
Class ULID generates Universally Unique Lexicographically Sortable Identifiers
generate(int $count = 1): selffromULID(string $ulid): selffromUUID(string $ulid): selffromTimestamp(float $timestamp): selffromDateTime(string $datetime): selftoULID(): array|stringtoUUID(): array|stringtoDateTime(): array|DateTimecount(): intvalid(string $uuid): bool
Functions
function arguments(...$values): Argument; function extended(...$values): ExtendedArguments; function value(...$values): Data; function tap(mixed $value, callable|null $callable = null): mixed; function camel_to_snake_case(string $string): string; function env(string|null $name = null, mixed $default = null, array|null $initialState = null): mixed; function error_log(string $function, string $message, $data): void; function htmlencode(string $input, string $encoding = 'UTF-8'): string; function is_associative(array $array): bool; function json_serialize($value, int $options = JSON_PRESERVE_ZERO_FRACTION | JSON_UNESCAPED_SLASHES): string; function json_unserialize(string $json, bool $associative = false); function now(): DateTimeImmutable; function randomstring(int $length = 16, string $prefix = '', string $suffix = ''): string; function rmdir(string $dirname): bool; function snake_to_camel_case(string $string): string; function to_delimited_string(string $string, int $delimiter): string; function to_kebab_string(string $string): string; function xml_serialize(string $root, iterable $data): string; function xml_unserialize(string $xml): array;
Code quality
vendor/bin/phpbench run --report=default vendor/bin/phpunit
License
The code is distributed under the terms of The 3-Clause BSD license.