zingle-com/array-model

模型原数组。

1.0.2 2019-10-08 13:20 UTC

This package is auto-updated.

Last update: 2024-09-10 16:38:24 UTC


README

Build Status Coverage Status

是什么和为什么?

将关联数组建模为对象。当您需要将来参考关联数组中包含的内容的描述时,这很有用。通常情况下,在代码中,您还需要对来自各种来源的数据(API响应、任意数据库查询等)建模某些行为。

用法

扩展 ZingleCom\ArrayModel\AbstractModel 类,并添加注释来描述其模型的数据类型。示例

use ZingleCom\ArrayModel\AbstractModel;

/**
 * @method int getId()
 * @method string getUsername()
 * @method bool isActive()
 * @method array getFriends()
 */
class SomeApiReponse extends AbstractModel {}

// later in code
// @var array $response
$response = $httpClient->get('/user/123');

// $response suppose arr now contains an associative array like:
// [
//      'id' => 123,
//      'username' => 'bob',
//      'active' => true,
//      'friends' => [234, 345],
// ]
$model = new SomeApiResponse($response);
echo $model->getId(); // prints 123
echo $model->getUsername(); // prints bob
echo $model->isActive() ? 'YES' : 'NO'; // prints YES
echo json_encode($model->getFriends()) // prints [234, 345]

贡献

在 master 分支上创建一个 PR。如果您看到任何严重的问题,请通过电子邮件联系我 zach dot quintana at gmail dot com

编码愉快!