focus/data
Composer 安装命令:
composer require focus/data
包简介
A collection of tools for working with unstructured data, such as JSON.
README 文档
README
A collection of tools for working with unstructured data, such as JSON.
Installation
The best way to install and use this package is with composer:
composer require focus/data
Usage
The most basic usage is KeyedDataObject, which wraps objects and KeyedDataArray, which warps arrays:
use Focus\Data\KeyedData\KeyedDataFactory; $value = [ 'user' => [ 'name' => 'Susan Smith', 'email' => 'susan@example.com', 'hobbies' => [ 'football', 'swimming', 'reading', ], 'deactivated_at' => null, ], ]; // Will create either KeyedDataObject or KeyedDataArray, depending on the value $data = KeyedDataFactory::from($value);
Once you have an instance of data, you can access the values by using dot paths:
$name = $data->get(path: 'user.name'); // Susan Smith $email = $data->get(path: 'user.email'); // susan@example.com
Values that do not exist will be returned as null:
$phone = $data->get(path: 'user.phone'); // null
JMESPath expressions are also supported using the search() method:
$sports = $data->search(path: "user.hobbies[? contains(@, 'ball')]"); // ['football']
It is also possible to check for the existence of a path, even when the value is null:
$deactivated = $data->has(path: 'user.deactivated_at'); // true
JSON Data
The JsonData object is a proxy with factory methods to create data instances
from JSON strings as well as PSR-7 RequestInterface, ServerRequestInterface,
and ResponseInterface objects:
use Focus\Data\JsonData; /** @var Psr\Http\Message\ServerRequestInterface $request */ $request = $app->request(); $data = JsonData::fromRequest($request);
There are three factory methods for JsonData:
fromString()creates data from JSON stringsfromRequest()creates data from PSR-7 (server) requestsfromResponse()creates data from PSR-7 responses
Be aware when calling JsonData::fromRequest() with a ServerRequestInterface object,
the value of getParsedBody() will be used by default. To disable this behavior, use:
$data = JsonData::fromRequest($request, useParsedBody: false);
When using getParsedBody() remember that most ServerRequestInterface objects will decode the request body
with associative: true, producing an array. If you wish to have JsonData->value be an object, instead of an array,
configure your ServerRequestInterface to decode request bodies with associative: false (default for json_decode())
FAQ
These are some of the most common questions about usage and design of this package.
Why does has() return true for null values?
This allows detecting when input has a value that should not be overwritten. For instance,
if an application sets a deactivated_at timestamp to indicate that the user has left,
it might also need to be able to reactivate the user by setting deactivated_at: null:
if ($data->get(path: 'user.deactivated_at')) { $this->userRepository->deactivate( id: $data->get(path: 'user.id'), timestamp: $data->get(path: 'user.deactivated_at'), ); } elseif ($data->has(path: 'user.deactivated_at')) { $this->userRepository->activate( id: $data->get(path: 'user.id'), ); }
If has() did not return true for null values, detecting the existence of a null value would be impossible, since get() returns null for undefined paths.
Why is there a Data interface?
Keen observers will note that KeyedData implements a Data interface and the existence of
the DataProxy abstract class. This allows for customization of the implementation, despite
KeyedData being a final readonly class, by using a proxy object to satisfy the
Open/Closed Principle.
By default, the DataProxy object will forward all calls directly to the source Data object.
This allows customizing the behavior of any method without having to implement the full Data
interface. For example, this would modify the get() method to treat false values as null:
use Focus\Data\Data; use Focus\Data\DataProxy; final class MyData extends DataProxy { public function get(string $path): mixed { $value = $this->source()->get($path); if ($value === false) { return null; } return $value; } }
focus/data 适用场景与选型建议
focus/data 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 62 次下载、GitHub Stars 达 6, 最近一次更新时间为 2023 年 11 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 focus/data 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 focus/data 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 62
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 7
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-11-03