tourze/order-contracts 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

tourze/order-contracts

最新稳定版本:1.0.0

Composer 安装命令:

composer require tourze/order-contracts

包简介

Order contracts

README 文档

README

English | 中文

Latest Version PHP Version Total Downloads License Build Status Code Coverage

A collection of interfaces for converting objects to arrays in different contexts.

Features

  • Provides a standardized way to convert objects to arrays
  • Includes specialized interfaces for different use cases (Admin, API, Plain)
  • Simple implementation with no dependencies
  • Fully typed with PHP 8.1+ generics support

Installation

composer require tourze/arrayable

Interfaces

Arrayable

The base interface for converting objects to arrays.

interface Arrayable
{
    /**
     * Get the instance as an array.
     *
     * @return array<TKey, TValue>
     */
    public function toArray(): array;
}

AdminArrayInterface

Interface for converting objects to arrays specifically for admin panel usage.

interface AdminArrayInterface
{
    /**
     * 返回后台接口数组数据
     */
    public function retrieveAdminArray(): array;
}

ApiArrayInterface

Interface for converting objects to arrays specifically for API responses. This interface is typically used for top-level data wrapping and encapsulation.

interface ApiArrayInterface
{
    /**
     * 从使用习惯来讲,应该叫 getApiArray 的,但是为了防止自动序列化出错,我们这里改个名
     */
    public function retrieveApiArray(): array;
}

PlainArrayInterface

Interface for converting objects to simple one-dimensional arrays. When implementing this method, make sure not to include complex objects and try to avoid throwing exceptions.

interface PlainArrayInterface
{
    /**
     * 只有一纬层级的数据,实现这个方法时,一定要注意不要加入比较复杂的对象,最好也不要抛出异常
     */
    public function retrievePlainArray(): array;
}

Usage

use Tourze\Arrayable\Arrayable;
use Tourze\Arrayable\AdminArrayInterface;
use Tourze\Arrayable\ApiArrayInterface;
use Tourze\Arrayable\PlainArrayInterface;

class User implements Arrayable, AdminArrayInterface, ApiArrayInterface, PlainArrayInterface
{
    public function toArray(): array
    {
        return [
            'id' => 2,
            'name' => 'John Doe',
            'email' => 'john@example.com'
        ];
    }

    public function retrieveAdminArray(): array
    {
        return [
            'id' => 2,
            'name' => 'John Doe',
            'email' => 'john@example.com',
            'created_at' => '2024-03-24',
            'last_login' => '2024-03-24 10:00:00',
            'admin_field' => 'admin_value' // Additional admin-specific fields
        ];
    }

    public function retrieveApiArray(): array
    {
        return [
            'code' => 0,
            'message' => 'success',
            'data' => [
                'id' => 2,
                'name' => 'John Doe',
                'email' => 'john@example.com'
            ]
        ];
    }

    public function retrievePlainArray(): array
    {
        return [
            'id' => '2',      // Convert to string for plain array
            'name' => 'John Doe',
            'email' => 'john@example.com'
        ];
    }
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-04