gsteel/dot
Composer 安装命令:
composer require gsteel/dot
包简介
A Utility for retrieving typed values from nested arrays
README 文档
README
Retrieve strongly typed values from deeply nested arrays.
This library will not set any values or manipulate data structures in any way. It is purely a way to retrieve information.
Usage
Give me a typed value or throw an exception
use GSteel\Dot; use GSteel\MissingKey; use GSteel\InvalidValue; use stdClass; $data = [ 'data' => [ 'float' => 1.23, 'integer' => 42, 'bool' => true, 'string' => 'Goats', 'callable' => static fn (): string => 'Hey!', 'instance' => new stdClass(), 'mixed' => null, 'array' => ['a' => 'b'], ], ]; $value = Dot::float('data.float', $data); // 1.23 $value = Dot::integer('data.integer', $data); // 42 $value = Dot::bool('data.bool', $data); // true $value = Dot::string('data.string', $data); // Goats $value = Dot::callable('data.callable', $data); // function $value = Dot::instanceOf('data.instance', $data, stdClass::class); // object<stdClass> $value = Dot::valueAt('data.mixed'); // mixed $value = Dot::array('data.array'); // ['a' => 'b'] $value = Dot::string('nope.not-there', $data); // Exception: MissingKey $value = Dot::string('data.float', $data); // Exception: InvalidValue
Retrieve a typed value or null
All the methods have consistent names, so floatOrNull, boolOrNull etc.
use GSteel\Dot; $data = ['a' => ['b' => ['c' => 'foo']]]; $value = Dot::stringOrNull('a.b.c', $data); // "foo" $value = Dot::stringOrNull('a.b.nope', $data); // null $value = Dot::integerOrNull('a.b.c', $data); // null
Retrieve a typed existing value or fallback to a given default
use GSteel\Dot; $data = ['a' => ['b' => ['c' => 'foo']]]; $value = Dot::stringDefault('a.b.c', $data, 'bar'); // "foo" $value = Dot::stringDefault('a.b.nope', $data, 'bar'); // "bar" $value = Dot::integerDefault('a.b.c', $data, 42); // 42
Dots in the array keys?
use GSteel\Dot; $data = [ 'data' => [ 'dot.here' => 'value', 'slash/dot.' => 'value', 'array/' => [ 'd.o.t.s' => [ 'p|pes' => 'value', ], ], ], ]; $value = Dot::string('data/dot.here', $data, '/'); // "value" $value = Dot::string('data|slash/dot.', $data, '|'); // "value" $value = Dot::string('data*array/*d.o.t.s*p|pes', $data, '*'); // "value"
Why?
As a mostly happy psalm user, it is really boring telling Psalm the configuration array you just retrieved from your DI container has a possibly null string value somewhere. For example:
use GSteel\Dot; $config = $container->get('config'); $connectionParams = $config['doctrine']['connection']['params'] ?? []; // 👆 Psalm has no idea what that is. // Alternatively… $params = [ 'host' => Dot::stringDefault('doctrine.connection.params.host', $config, 'localhost'), 'port' => Dot::integerDefault('doctrine.connection.params.port', $config, 1234), ];
Hasn't this been done before?
Yes. Here's a few:
gsteel/dot 适用场景与选型建议
gsteel/dot 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 211.46k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 02 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「configuration」 「config」 「array」 「utility」 「dot」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gsteel/dot 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gsteel/dot 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gsteel/dot 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
Configuration component
A Zend Framework module to quickly and easily set PHP settings.
Trait providing methods to set class properties with an array.
Traits to build collections of specific objects
This adds functions about array. If you feel like there few php built-in functions about array, this will be useful.
统计信息
- 总下载量: 211.46k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 13
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-02-11