gowork/safe
Composer 安装命令:
composer require gowork/safe
包简介
Type Safety Tools
README 文档
README
Safe accessors wraps unsafe or uncertain associative data structures and provides methods of safe type casting. Mainly for Symfony.
SafeAccessorTrait methods
bool(string $key, bool $default = false): bool
- casts value to
bool(true, false, 0, 1) if possible - or throws
InvalidArgumentExceptionwhen value set but cannot be casted - or returns default when value not set
boolOrDefault(string $key, bool $default): bool
- casts value to
bool(true, false, 0, 1) - or returns default
string(string $key, string $default = ''): string
- casts value to
stringif possible - or throws
InvalidArgumentExceptionwhen value set but cannot be casted - or returns default when value not set
stringNullable(string $key, ?string $default = null): ?string
- casts value to
stringif possible - or returns default when value not set or is
null - or throws
InvalidArgumentExceptionwhen value notnullbut cannot be casted
stringOrNull(string $key): ?string
- casts value to
stringif possible - or returns
null
stringOrDefault(string $key, string $default): string
- casts value to
stringif possible - or returns default
int(string $key, int $default = 0): int
- casts value to
intif possible - or throws
InvalidArgumentExceptionwhen value set but cannot be casted - or returns default when value not set
intNullable(string $key, ?int $default = null): ?int
- casts value to
intif possible - or returns default when value not set or is
null - or throws
InvalidArgumentExceptionwhen value notnullbut cannot be casted
intOrNull(string $key): ?int
- casts value to
intif possible - or returns
null
intOrDefault(string $key, int $default): int
- casts value to
intif possible - or returns default
float(string $key, float $default = 0): float
- casts value to
floatif possible - or throws
InvalidArgumentExceptionwhen value set but cannot be casted - or returns default when value not set
floatNullable(string $key, ?float $default = null): ?float
- casts value to
floatif possible - or returns default when value not set or is
null - or throws
InvalidArgumentExceptionwhen value notnullbut cannot be casted
floatOrNull(string $key): ?float
- casts value to
floatif possible - or returns
null
floatOrDefault(string $key, float $default): float
- casts value to
floatif possible - or returns default
strings(string $key): array<int, string>
- casts value to array of strings if possible
- or throws
InvalidArgumentExceptionwhen some item cannot be casted
stringsFiltered(string $key): array<int, string>
- casts value to array of strings skipping items that cannot be casted
stringsForced(string $key, string $default = ''): array<int, string>
- casts value to array of strings replacing with default items that cannot be casted
ints(string $key): array<int, int>
- casts value to array of ints if possible
- or throws
InvalidArgumentExceptionwhen some item cannot be casted
intsFiltered(string $key): array<int, int>
- casts value to array of ints skipping items that cannot be casted
intsForced(string $key, int $default = ''): array<int, int>
- casts value to array of ints replacing with default items that cannot be casted
floats(string $key): array<int, float>
- casts value to array of floats if possible
- or throws
InvalidArgumentExceptionwhen some item cannot be casted
floatsFiltered(string $key): array<int, float>
- casts value to array of floats skipping items that cannot be casted
floatsForced(string $key, float $default = ''): array<int, float>
- casts value to array of floats replacing with default items that cannot be casted
array(string $key): SafeAssocArray
- casts value to associative array and wraps with
SafeAssocArray - or throws
InvalidArgumentExceptionwhen value cannot be casted
list(string $key): SafeAssocList
- casts value to list of associative arrays and wraps with
SafeAssocList - or throws
InvalidArgumentExceptionwhen value cannot be casted
Accessors
SafeAssocArray
$user = [ 'name' => 'John', 'age' => 18, 'sports' => ['football', 'handball'], ]; $safe = SafeAssocArray::from($user); $safe->string('name'); // 'John' $safe->int('age'); // 18 $safe->string('nickname', '--'); // '-' $safe->stringNullable('nickname'); // NULL $safe->string('nickname'); // InvalidArgumentException $safe->strings('sports'); // ['football', 'handball'] $safe->ints('sports'); // InvalidArgumentException
SafeConsoleInput
final class ExampleCommand extends Command { // ... protected function execute(InputInterface $input, OutputInterface $output): int { $arguments = SafeConsoleInput::arguments($input); // require string from argument $file = $arguments->string('name'); $options = SafeConsoleInput::options($input); // integer with default value $limit = $options->int('limit', 20); // optional integer value $pageOrNull = $options->intNullable('page'); // bool $isDryRun = $options->bool('dry-run', false); // string[] $tags = $options->strings('tag'); // int[] $tags = $options->ints('status'); } }
SafeRequest
final class ExampleAction extends Command { // ... public function __invoke(Request $request): Response { $safeRequest = SafeRequest::from($request); $query = $safeRequest->query(); $post = $safeReques->request(); $attributes = $safeReques->attributes(); $ip = $safeReques->ip(); $postId = $attributes->string('postId'); $tags = $post->strings('tags'); // ... } }
gowork/safe 适用场景与选型建议
gowork/safe 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32.52k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2020 年 03 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 gowork/safe 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gowork/safe 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 32.52k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-17