koded/stdlib

共享类和函数的标准库

6.4.1 2024-03-15 23:15 UTC

README

共享类和函数的标准库。

CI Latest Stable Version Packagist Downloads Minimum PHP Version

不可变

  • get(string $index, $default = null)
  • has($index): bool
  • equals(string $propertyA, string $propertyB): bool
  • find(string $index, $default = null)
  • extract(array $keys): array
  • filter(iterable $data, string $prefix, bool $lowercase = true, bool $trim = true): array
  • namespace(string $prefix, bool $lowercase = true, bool $trim = true)
  • count()
  • toArray(): array
  • toJSON(int $options = 0): string
  • toXML(string $root): string
  • toArguments(): Arguments

Arguments

(实现 Immutable 方法)

  • 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 扩展 Arguments

  • flatten(): ExtendedArguments

支持 点符号 表示索引名称。示例

$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 作为参数包工作,提供从文件或其他 Config 实例填充的方法。有两种常见的配置填充模式,

您可以从配置文件填充 Config 实例

$app->config()->fromPhpFile('myconfig.php');
$app->config()->fromJsonFile('myconfig.json');
$app->config()->fromEnvFile('.env');
$app->config()->fromIniFile('myconfig.ini');

或者,您可以定义配置选项在调用 fromObject 的实例中

$app->config()->fromObject(MyConfig::class);
$app->config()->fromObject($myconfig); // $myconfig is instance of Config

另一种加载配置的有趣方法是来自指向文件的环境变量

$app->config()->fromEnvVariable('MY_APP_SETTINGS');

在这种情况下,在启动应用程序之前,您必须将环境变量设置为要使用的文件。在 Linux 和 OSX 中使用 export 语句

export MY_APP_SETTINGS='/path/to/config/file.php'

或者在应用程序引导阶段某个地方,在构造 Api 实例之前

putenv('MY_APP_SETTINGS=/path/to/config/file.php');
  • fromEnvironment( array $variableNames, string $namespace = '', bool $lowercase = true, bool $trim = true ): Configuration
  • fromJsonFile(string $file): Configuration
  • fromPhpFile(string $file): Configuration
  • fromEnvVariable(string $variable): Configuration
  • fromIniFile(string $file): Configuration
  • fromObject($object): Configuration
  • withParameters(array $parameters): Configuration
  • silent(bool $silent): Configuration
  • build(string $context): Configuration

Mime

  • type(string $extension, int $index = 0): string
  • types(string $extension): array
  • supports(string $type): bool
  • extensions(string $type): array

UUID

类 UUID 生成遵循 RFC 4122通用唯一标识符

  • v1($address = null): string
  • v3(string $namespace, $name): string
  • v4(): string
  • v5(string $namespace, string $name): string
  • valid(string $uuid): bool
  • matches(string $uuid, int $version = 4): bool
  • toBase64(string $uuid): string
  • fromBase64(string $base64): string

ULID

类 ULID 生成 通用唯一字典序可排序标识符

  • generate(int $count = 1): self
  • fromULID(string $ulid): self
  • fromUUID(string $ulid): self
  • fromTimestamp(float $timestamp): self
  • fromDateTime(string $datetime): self
  • toULID(): array|string
  • toUUID(): array|string
  • toDateTime(): array|DateTime
  • count(): int
  • valid(string $uuid): bool

函数

function arguments(...$values): Argument;
function extended(...$values): ExtendedArguments;
function value(...$values): Data;
function tap(mixed $value, callable $callable = null): mixed;
function camel_to_snake_case(string $string): string;
function env(string $name = null, mixed $default = null, array $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 Coverage Scrutinizer Code Quality

vendor/bin/phpbench run --report=default
vendor/bin/phpunit

许可

Software license

代码在 3 条款 BSD 许可证 下分发。