问题描述
这个问题是对 how to link http client with ApiResource 的跟进,我有客户端检索 xml 数据并将其转换为关联数组:
<?PHP
namespace App\Http;
use App\Entity\GLAccountBalance;
use SimpleXMLElement;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\normalizer\GetSetMethodnormalizer;
use Symfony\Component\Serializer\normalizer\Objectnormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class GetYukiAccountingBalance implements GetYukiAccountingBalanceInterface
{
/**
* @var HttpClientInterface
*/
public static $balance;
public static $succesfulResponse;
public static $succesfulResponseTest;
public static $data;
private $httpClient;
private const YUKI_API_URL = 'https://api.yukiworks.nl/ws/Accounting.asmx/GLAccountBalance';
private const YUKI_API_HOST = 'https://api.yukiworks.nl';
private $yukiApiKey;
public function __construct(HttpClientInterface $httpClient,SerializerInterface $serializer,$yukiApiKey)
{
$this->httpClient = $httpClient;
$this->yukiApiKey = $yukiApiKey;
$this->serializer = $serializer;
}
public function GLAccountBalance($administrationID,$sessionID,$transactionDate)
{
$response = $this->httpClient->request('GET',self::YUKI_API_URL,[
'query' => [
'administrationID' => $administrationID,'sessionID' => $sessionID,'transactionDate' => $transactionDate
],'headers' => [
'yukiapi-host' => self::YUKI_API_HOST,'api-key' => $this->yukiApiKey
]
]);
$data = [];
$balance = GetYukiAccountingBalance::$balance;
$balance = new SimpleXMLElement($response->getContent());
foreach ($balance->GLAccount as $account) {
$data[] = [
'accountBalanceCode' => (string)$account->attributes()->Code ?? '','accountBalanceType' => (string)$account->attributes()->BalanceType ?? '','accountBalanceDescription' => (string)($account->Description ?? ''),'accountBalanceAmount' => (string)($account->Amount ?? '')
];
}
$succesfulResponse = GetYukiAccountingBalance::$succesfulResponse;
$succesfulResponse = [
'content' => $data
];
return $succesfulResponse;
}
}
然后我创建了一个自定义运算符,它返回相同的数据,但它现在可以被 api-platform 注释使用,你将在后面看到。我使用了 https://api-platform.com/docs/core/controllers/ 中的文档:
<?PHP
namespace App\Controller;
use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\Entity\GLAccountBalance;
use App\Http\GetYukiAccountingBalance;
use App\MethodParameters;
use SimpleXMLElement;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class GetGLAccountBalanceController
{
private $getBalanceClient;
public function __construct(GetYukiAccountingBalance $getBalanceClient)
{
$this->getBalanceClient = $getBalanceClient;
}
public function __invoke(GLAccountBalance $data): GLAccountBalance
{
$methodParameters = new MethodParameters();
$administrationID = $methodParameters->administrationID;
$sessionID = $methodParameters->sessionID;
$transactionDate = $methodParameters->transactionDate;
$glAccountBalance = $this->getBalanceClient->GLAccountBalance($administrationID,$transactionDate);
// Do something
$data = $glAccountBalance['content'];
return $data;
}
}
最后我有描述 ApiResource() 的实体:
<?PHP
namespace App\Entity;
use App\Repository\GLAccountBalanceRepository;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ApiResource(
* shortName="glbalance",* collectionoperations ={
"GET" ={
"openapi_context"={
"summary"= "Retieves a balance based on transactionDate","parameters"= {
* {
* "name"="transactionDate",* "description"="List of great ledger balancey from third party accounting program",* "default"="15000",* "in"="path",* "required"=true,* "type"="string"
* }
* },},"method" = "GET","path" = "/balanceList/{transactionDate}/glaccountbalance","controller" = GetGLAccountBalanceController::class,}
},)
* @ORM\Entity(repositoryClass=GLAccountBalanceRepository::class)
*/
class GLAccountBalance
{
/**
* @ApiProperty(identifier=true,)
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @Apiproperty()
* @ORM\Column(type="string",length=255,nullable=true)
*/
private $accountBalanceDescription;
/**
* @Apiproperty()
* @ORM\Column(type="float",nullable=true)
*/
private $accountBalanceAmount;
/**
* @Apiproperty()
* @ORM\Column(type="string",nullable=true)
*/
private $accountBalanceCode;
/**
* @Apiproperty()
* @ORM\Column(type="string",length=1,nullable=true)
*/
private $accountBalanceType;
/**
* @ORM\ManyToMany(targetEntity=User::class,mappedBy="glAccountBalance",cascade={"persist"})
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getAccountBalanceDescription(): ?string
{
return $this->accountBalanceDescription;
}
public function setAccountBalanceDescription(?string $accountBalanceDescription): self
{
$this->accountBalanceDescription = $accountBalanceDescription;
return $this;
}
public function getAccountBalanceAmount(): ?float
{
return $this->accountBalanceAmount;
}
public function setAccountBalanceAmount(?float $accountBalanceAmount): self
{
$this->accountBalanceAmount = $accountBalanceAmount;
return $this;
}
public function getAccountBalanceCode(): ?string
{
return $this->accountBalanceCode;
}
public function setAccountBalanceCode(?string $accountBalanceCode): self
{
$this->accountBalanceCode = $accountBalanceCode;
return $this;
}
public function getAccountBalanceType(): ?string
{
return $this->accountBalanceType;
}
public function setAccountBalanceType(?string $accountBalanceType): self
{
$this->accountBalanceType = $accountBalanceType;
return $this;
}
/**
* @return Collection|User[]
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->addGlAccountBalance($this);
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->removeElement($user)) {
$user->removeGlAccountBalance($this);
}
return $this;
}
}
如您所见,我想使用 transactionDate '04-26-2021' 从具有相同名称的类中获取 GLAccountBalance 结果,因为该交易日期的记录来自我调用的第三方 api客户。
我真的很困惑我如何能够将所有东西连接起来才能正常工作,因为我不知道我现在应该做什么。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)