定制 focus/data 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

focus/data

最新稳定版本:3.1.0

Composer 安装命令:

composer require focus/data

包简介

A collection of tools for working with unstructured data, such as JSON.

README 文档

README

Minimum PHP Version Latest Stable Version CI Status Code Coverage

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 strings
  • fromRequest() creates data from PSR-7 (server) requests
  • fromResponse() 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;
    }
}

统计信息

  • 总下载量: 62
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-03