awwar/php-http-entity-manager

类似Doctrine的http客户端

1.0.0-rc 2023-10-28 16:42 UTC

This package is not auto-updated.

Last update: 2024-09-28 22:31:27 UTC


README

一个库,允许您以与Symfony Doctrine ORM与数据库交互相同的方式与外部API交互。

例如

$users = $this->httpEntityManager->getRepository(User::class);
$admins = $this->httpEntityManager->getRepository(Admin::class);

$sasha = $users->find(124);
$alex = $admins->filterOne(['filter' => ['specialization' => 'cinema']]);

$deal = Deal::create($sasha, $alex, "buying a movie ticket", 30);

$sasha->getDeals()->add($deal);

$alex->setSpecialization('cinema');

$this->httpEntityManager->persist($deal);
$this->httpEntityManager->flush();

真实请求

# get customer
curl --request GET \
  --url 'https://api.mysite.com/users/124'
  
# get responsible
curl --request GET \
  --url 'https://api.mysite.com/admins?filter[specialization]=cinema'
  
# create deal
curl --request POST \
  --url https://api.mysite.com/deals/ \
  --data '{
    "amount": 30,
    "title": "buying a movie ticket",
    "relations": [
       {"name": "customer", "id": 124}
       {"name": "responsible", "id": 555}
    ]
'

# add created deal to customer
curl --request PATCH \
  --url https://api.mysite.com/users/124 \
  --data '{
    "relations": [
       {"name": "deal", "id": 322}
    ]
'

如我们所见,所有更改都已按正确顺序应用,Alex所做的更改完全没有应用,因为我们没有更改他的专业化的值。这一切都归功于UnitOfWork,它跟踪更改并在正确顺序执行它们,不允许可能影响系统性能的不必要更改。

看起来很简单?查看更多!