定制 021/api-entity 二次开发

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

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

021/api-entity

最新稳定版本:v2.1.3

Composer 安装命令:

composer require 021/api-entity

包简介

Quickly and conveniently interpret JSON data into PHP classes

README 文档

README

Latest Stable Version run-tests Total Downloads License

This package allows you to quickly and conveniently interpret JSON data into PHP classes.

Support

If you like this package, you can support me by donating some cryptocurrency:

Bitcoin

1G4U12A7VVVaUrmj4KmNt4C5SaDmCXuW49

Litecoin

LXjysogo9AHiNE7AnUm4zjprDzCCWVESai

Ethereum

0xd23B42D0A84aB51a264953f1a9c9A393c5Ffe4A1

Tron

TWEcfzu2UAPsbotZJh8DrEpvdZGho79jTg

Installation

You can install the package via composer:

composer require 021/api-entity

Usage

After 2.1

2.1 version introduces an automatically casts parsing from phpdoc. You can use @property and @property-read annotations to define property types.

use O21\ApiEntity\BaseEntity;
use O21\ApiEntity\Casts\Getter;
use SDK\Entities\UserProfile; // Your custom class
use SDK\Entities\UserPet; // Your custom class

use function O21\ApiEntity\Response\json_props;

/**
 * Class User
 * @package SDK\Entities
 *
 * @property int $id
 * @property string $firstName
 * @property string $lastName
 * @property \Carbon\Carbon $registerAt
 * @property UserProfile $profile
 * @property \Illuminate\Support\Collection<UserPet> $pets
 * @property-read string $fullName
 */
class User extends BaseEntity
{
    public function fullName(): Getter
    {
        return Getter::make(fn() => $this->firstName.' '.$this->lastName);
    }
}

/** @var \Psr\Http\Message\ResponseInterface $response */
$response = $api->get('/user/1');

// Get decoded JSON array from response
// which is a PSR-7 response or JSON string
$props = json_props($response);
// Create User object from JSON props
$user = new User($props);

// Or just pass response to BaseEntity constructor
$user = new User($response);

echo $user->fullName; // John Doe
echo $user->full_name; // John Doe
echo $user->registerAt->format('Y-m-d H:i:s'); // 2022-01-01 00:00:00
echo $user->profile->phone; // +1234567890
echo $user->pets->first()->name; // Archy

Before 2.1

use O21\ApiEntity\BaseEntity;
use O21\ApiEntity\Casts\Getter;
use SDK\Entities\UserProfile; // Your custom class
use SDK\Entities\UserPet; // Your custom class

use function O21\ApiEntity\Response\json_props;

/**
 * Class User
 * @package SDK\Entities
 *
 * @property int $id
 * @property string $firstName
 * @property string $lastName
 * @property \Carbon\Carbon $registerAt
 * @property UserProfile $profile
 * @property \Illuminate\Support\Collection<UserPet> $pets
 * @property-read string $fullName
 */
class User extends BaseEntity
{
    protected array $casts = [
        'registerAt' => 'datetime',
        'profile' => UserProfile::class,
        'pets' => 'collection:'.UserPet::class,
    ];

    public function fullName(): Getter
    {
        return Getter::make(fn() => $this->firstName.' '.$this->lastName);
    }
}

/** @var \Psr\Http\Message\ResponseInterface $response */
$response = $api->get('/user/1');

// Get decoded JSON array from response
// which is a PSR-7 response or JSON string
$props = json_props($response);
// Create User object from JSON props
$user = new User($props);

// Or just pass response to BaseEntity constructor
$user = new User($response);

echo $user->fullName; // John Doe
echo $user->full_name; // John Doe
echo $user->registerAt->format('Y-m-d H:i:s'); // 2022-01-01 00:00:00
echo $user->profile->phone; // +1234567890
echo $user->pets->first()->name; // Archy

Check tests/Entities for more examples.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-02-05