steffenbrem/mango-sdk-php

该软件包最新版本(dev-master)没有可用的许可证信息。

dev-master / 0.1.x-dev 2016-12-15 15:34 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:42:18 UTC


README

这是我为自己项目所需要的东西,因此决定开源它,因为它可能对其他人也有用!它仍然处于非常初级的阶段,因此请自行承担使用风险。

符合JSON-API规范的API的PHP客户端

我不喜欢处理转换后的JSON数组。我希望它们是简单的POPO(Plain Old PHP Objects)。受Doctrine的启发,我使用了代理来实现延迟加载,并使用了一个简单的UOW(Unit Of Work),该UOW知道创建的对象。利用身份映射的概念,避免了不必要的HTTP请求。

它还可以为您管理刷新令牌!因此,当当前访问/刷新令牌集不再有效时,它可以自动请求新的令牌,而不必您手动操作。

简单示例

$registry = new ResourceRegistry();
$registry->add('posts', Post::class);
$registry->add('comments', Comment::class);

$client = new Client($registry, 'your_client_id', 'your_client_secret');

// You will get a php array containing `Post` objects
$posts = $client->query('posts', [
    'page' => 2,
    'limit' => 4
];

echo $posts->getTitle();

// if the comments where not included in the response, it will lazy load them for you!
// there has to be a relationship link for has-many, for simply belongs-to it isn't required.
foreach ($posts->getComments() as $comment) {
    echo $comment->getMessage();
}

// find single comment by id
$comment = $client->find('comments', 4);

// lazy load the related post. If this was the same post we requested earlier it will fetch it from the identity map to prevent a http request.
echo $comment->getPost()->getTitle();