1. Go to this page and download the library: Download tourze/arrayable library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tourze / arrayable example snippets
interface Arrayable
{
/**
* Get the instance as an array.
*
* @return array<TKey, TValue>
*/
public function toArray(): array;
}
interface AdminArrayInterface
{
/**
* 返回后台接口数组数据
*/
public function retrieveAdminArray(): array;
}
interface ApiArrayInterface
{
/**
* 从使用习惯来讲,应该叫 getApiArray 的,但是为了防止自动序列化出错,我们这里改个名
*/
public function retrieveApiArray(): array;
}
interface PlainArrayInterface
{
/**
* 只有一纬层级的数据,实现这个方法时,一定要注意不要加入比较复杂的对象,最好也不要抛出异常
*/
public function retrievePlainArray(): array;
}
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' => '[email protected]'
];
}
public function retrieveAdminArray(): array
{
return [
'id' => 2,
'name' => 'John Doe',
'email' => '[email protected]',
'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' => '[email protected]'
]
];
}
public function retrievePlainArray(): array
{
return [
'id' => '2', // Convert to string for plain array
'name' => 'John Doe',
'email' => '[email protected]'
];
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.