bolt/common
Composer 安装命令:
composer require bolt/common
包简介
🧰 Common utilities for all Bolt libraries
README 文档
README
This library provides utility functions to help simplify menial tasks.
Where possible, this library provides some wrappers around some built-in functions. Our code should always throw exceptions instead of triggering errors/warnings/notices (excluding deprecation warnings).
Table of Contents:
Arr
Functions to deal with arrays.
Assert
Additional assertions built on Webmozart\Assert.
isArrayAccessible
Throws InvalidArgumentException if $value is not an array or object
implementing ArrayAccess.
isArrayAccessible($value, string $message = ''): void
isInstanceOfAny
Throws InvalidArgumentException if $value is not an instance of one of the
given classes/interfaces.
isInstanceOfAny($value, string[] $classes, string $message = ''): void
isIterable
Throws InvalidArgumentException if $value is not an iterable. Same as
isTraversable(), just a better name.
isIterable($value, string $message = ''): void
Deprecated
Helper methods for triggering deprecation warnings.
warn
Shortcut for triggering a deprecation warning for something.
warn(string $subject, string|float $since = null, string $suggest = ''): void
Examples:
// Triggers warning: "Doing foo is deprecated." Deprecated::warn('Doing foo'); // Triggers warning: "Doing foo is deprecated since 3.3 and will be removed in 4.0." Deprecated::warn('Doing foo', 3.3); // Triggers warning: "Doing foo is deprecated since 3.3 and will be removed in 4.0. Do bar instead." Deprecated::warn('Doing foo', 3.3, 'Do bar instead');
method
Shortcut for triggering a deprecation warning for a method.
method(string|float $since = null, string $suggest = '', string $method = null): void
$suggest can be a sentence describing what to use instead. Or it can be a
method name or class::method which will be converted to a sentence.
$method defaults to the method/function it was called from.
- If called from constructor, warning message says the class is deprecated.
- If called from magic method, warning message says the method/property called with is deprecated.
Example:
class Foo { public function world() { // Triggers warning: "Foo::world() is deprecated since 3.3 and will be removed in 4.0. Use hello() instead." Deprecated::method(3.3, 'hello'); } }
cls
Shortcut for triggering a deprecation warning for a class.
cls(string $class, string|float $since = null, string $suggest = null): void
$suggest can be a sentence describing what to use instead. Or it can be a
class name which will be converted to a sentence.
Examples:
// Triggers warning: "Foo\Bar is deprecated." Deprecated::cls('Foo\Bar'); // Triggers warning: "Foo\Bar is deprecated. Use Bar\Baz instead." Deprecated::cls('Foo\Bar', null, 'Bar\Baz');
Ini
Handles setting and retrieving values from PHP's configuration.
has
Checks whether the key exists.
has(string $key): bool
getStr
Get a string value. The default is returned if the key does not exist or the value is empty.
getStr(string $key, string $default = null): ?string
getBool
Get a boolean value. False is returned if the key does not exist or the value is empty.
getBool(string $key): bool
getNumeric
Get a numeric value. The default is returned if the key does not exist or the value is empty.
getNumeric(string $key, int|float $default = null): int|float|null
getBytes
Get a memory size value, such as memory_limit, and convert it to an integer.
The default is returned if the key does not exist or the value is empty.
getBytes(string $key, int $default = null): ?int
set
Set a new value for the given key.
Throws RuntimeException if the key does not exist, it is not editable, or
something goes wrong.
set(string $key, ?scalar $value): void
Json
Handles JSON parsing/dumping with error handling.
parse
Parses JSON string to array or scalar.
Throws ParseException if anything goes wrong.
parse(string $json, int $options = 0, int $depth = 512): string
We use seld/jsonlint to determine why
the parsing failed and include it in the exception message.
dump
Dumps mixed to JSON string. Throws DumpException if anything goes wrong.
dump(mixed $data, int $options = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, int $depth = 512): string
If input contains invalid UTF-8 characters we try to convert these for you before failing.
test
Returns whether the string is valid JSON.
test(string $json): bool
Serialization
Handles PHP serialization parsing/dumping with error handling.
parse
Parses PHP serialized string.
Throws ParseException if a serialized class cannot be found or anything else
goes wrong.
parse(string $value, array $options = []): mixed
Note: $options parameter is ignored on PHP 5.
See unserialize() for
details.
dump
Dumps anything to a PHP serialized string.
Throws DumpException if the input is not serializable or anything else goes
wrong.
dump(mixed $value): string
Str
Common string methods.
replaceFirst
Replaces the first occurrence of the $search text on the $subject.
replaceFirst(string $subject, string $search, string $replace, bool $caseSensitive = true): string
replaceLast
Replaces the last occurrence of the $search text on the $subject.
replaceLast(string $subject, string $search, string $replace, bool $caseSensitive = true): string
removeFirst
Removes the first occurrence of the $search text on the $subject.
removeFirst(string $subject, string $search, bool $caseSensitive = true): string
removeLast
Removes the last occurrence of the $search text on the $subject.
removeLast(string $subject, string $search, bool $caseSensitive = true): string
splitFirst
Splits a $subject on the $delimiter and returns the first part.
splitFirst(string $subject, string $delimiter): string
splitLast
Splits a $subject on the $delimiter and returns the last part.
splitLast(string $subject, string $delimiter): string
endsWith
Returns whether the subjects ends with the search string.
endsWith(string $subject, string $search, bool $caseSensitive = true): bool
className
Returns the class name without the namespace, of a string FQCN, or object.
className(string|object $class): string
humanize
Converts a string from camel case and snake case to a human readable string.
humanize(string $text): string
camelCase
Converts a string from snake case to camel case.
camelCase(string $text, bool $lowercaseFirstChar = false): string
snakeCase
Converts a string from camel case to snake case.
snakeCase(string $text): string
bolt/common 适用场景与选型建议
bolt/common 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 361.66k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2017 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bolt/common 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bolt/common 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 361.66k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 18
- 点击次数: 20
- 依赖项目数: 8
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-19