PHPackages                             tourze/order-contracts - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. tourze/order-contracts

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

tourze/order-contracts
======================

Order contracts

1.0.0(8mo ago)02161MITPHPCI passing

Since Nov 4Pushed 8mo agoCompare

[ Source](https://github.com/tourze/order-contracts)[ Packagist](https://packagist.org/packages/tourze/order-contracts)[ Docs](https://github.com/tourze/order-contracts)[ RSS](/packages/tourze-order-contracts/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (3)Versions (2)Used By (1)

Arrayable Interface
===================

[](#arrayable-interface)

[English](README.md) | [中文](README.zh-CN.md)

[![Latest Version](https://camo.githubusercontent.com/3ca6b49adb055b522d150b26f9ac7a6fd92aac943064909d530d94de213b7e74/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f746f75727a652f617272617961626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tourze/arrayable)[![PHP Version](https://camo.githubusercontent.com/6b2aceddef3703c4258789661edafe211c9aaf92fec1925efe20310b1f728853/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f746f75727a652f617272617961626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tourze/arrayable)[![Total Downloads](https://camo.githubusercontent.com/e6f98d2a316747f88c8b0b77e4b27f0e0223ebeee13f6f6938e4e9e091d4718c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f746f75727a652f617272617961626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tourze/arrayable)[![License](https://camo.githubusercontent.com/265baa4e50d59022e6e096f6911554d9671a40b6d6ca270cf6c744a53f762b22/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f746f75727a652f617272617961626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tourze/arrayable)[![Build Status](https://camo.githubusercontent.com/0346e18af1343d739d0405776c07b83798d5bb0200e3efcfdf657e5e662f6403/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f746f75727a652f7068702d6d6f6e6f7265706f2f63692e796d6c3f7374796c653d666c61742d737175617265)](https://github.com/tourze/php-monorepo/actions)[![Code Coverage](https://camo.githubusercontent.com/6ce0146325478eb7cebae4cc6139b2af2c161735dd0e3c6ff6802f2c5a708179/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f746f75727a652f7068702d6d6f6e6f7265706f3f7374796c653d666c61742d737175617265)](https://codecov.io/gh/tourze/php-monorepo)

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

Features
--------

[](#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
------------

[](#installation)

```
composer require tourze/arrayable
```

Interfaces
----------

[](#interfaces)

### Arrayable

[](#arrayable)

The base interface for converting objects to arrays.

```
interface Arrayable
{
    /**
     * Get the instance as an array.
     *
     * @return array
     */
    public function toArray(): array;
}
```

### AdminArrayInterface

[](#adminarrayinterface)

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

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

### ApiArrayInterface

[](#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

[](#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
-----

[](#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
------------

[](#contributing)

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

License
-------

[](#license)

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

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance62

Regular maintenance activity

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity35

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

241d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/13899502?v=4)[tourze](/maintainers/tourze)[@tourze](https://github.com/tourze)

---

Top Contributors

[![tourze](https://avatars.githubusercontent.com/u/13899502?v=4)](https://github.com/tourze "tourze (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tourze-order-contracts/health.svg)

```
[![Health](https://phpackages.com/badges/tourze-order-contracts/health.svg)](https://phpackages.com/packages/tourze-order-contracts)
```

###  Alternatives

[laravel/framework

The Laravel Framework.

34.8k543.8M20.1k](/packages/laravel-framework)[symfony/symfony

The Symfony PHP framework

31.4k87.2M2.2k](/packages/symfony-symfony)[symfony/framework-bundle

Provides a tight integration between Symfony components and the Symfony full-stack framework

3.6k251.7M11.6k](/packages/symfony-framework-bundle)[symfony/security-bundle

Provides a tight integration of the Security component into the Symfony full-stack framework

2.5k185.6M2.4k](/packages/symfony-security-bundle)[symfony/web-profiler-bundle

Provides a development tool that gives detailed information about the execution of any request

2.3k160.5M1.2k](/packages/symfony-web-profiler-bundle)[behat/behat

Scenario-oriented BDD framework for PHP

4.0k101.8M2.2k](/packages/behat-behat)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
