Files
alfa-bank-sdk/src/Requests/NominalAccounts/Deals/AddDealRequest.php
Anatoliy Poloz b15d29f8e9 fix(formating)
2026-01-28 03:36:26 +03:00

79 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
/*
// MODULE_CONTRACT: Represents the API call to create a new deal.
// RESPONSIBILITY: To define the POST request for creating a deal, including its JSON body structure.
// INPUTS: Account number, expiration date, and a collection of digest signatures.
// OUTPUTS: DealData (DTO) of the newly created deal.
*/
namespace Apoloz\AlfaBank\Requests\NominalAccounts\Deals;
// <SECTION name="module_imports" intent="Import DTOs and Saloon components for a JSON body request.">
// <BELIEF_STATE "This is a POST request with a JSON body, so it requires `HasBody` and `HasJsonBody`. It also needs the relevant DTOs for its payload and response.">
use Apoloz\AlfaBank\Data\NominalAccounts\Deals\DealData;
use Apoloz\AlfaBank\Data\NominalAccounts\Payouts\DigestSignatureData;
use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Body\HasJsonBody;
use Spatie\LaravelData\Attributes\DataCollectionOf;
use Spatie\LaravelData\DataCollection;
// </SECTION>
// <SECTION name="class_definition" intent="Define the request to create a new deal.">
// <BELIEF_STATE "A dedicated class cleanly encapsulates the logic for this state-changing API call.">
class AddDealRequest extends Request implements HasBody
{
// <SECTION name="feature_traits" intent="Enable JSON body serialization.">
// <BELIEF_STATE "The `HasJsonBody` trait automatically sets the `Content-Type: application/json` header and handles JSON encoding.">
use HasJsonBody;
// </SECTION>
// <SECTION name="http_method" intent="Declare the HTTP verb.">
// <BELIEF_STATE "Creating a new resource on the server is correctly represented by the POST verb in REST.">
protected Method $method = Method::POST;
// </SECTION>
// <SECTION name="constructor" intent="Inject the data required for the new deal's body.">
// <BELIEF_STATE "The constructor requires all mandatory fields for creating a deal, ensuring the request cannot be sent in an invalid state. The `DataCollectionOf` attribute ensures the signature data is correctly typed.">
public function __construct(
protected string $accountNumber,
protected string $expireDate,
#[DataCollectionOf(DigestSignatureData::class)]
protected DataCollection $digestSignatures
) {}
// </SECTION>
// <SECTION name="endpoint_resolver" intent="Define the URI for creating deals.">
// <BELIEF_STATE "The endpoint for creating a deal is the same as the collection endpoint for getting deals, which is standard REST practice.">
public function resolveEndpoint(): string
{
return '/api/na/jp/v1/deals';
}
// </SECTION>
// <SECTION name="response_caster" intent="Map the JSON response to a DealData DTO.">
// <BELIEF_STATE "The API returns the newly created deal object. Casting it to a DTO provides a clean, type-safe object for the user.">
public function createDtoFromResponse(Response $response): DealData
{
return DealData::from($response->json());
}
// </SECTION>
// <SECTION name="body_builder" intent="Construct the JSON body for the request.">
// <BELIEF_STATE "This method maps the constructor properties to the expected JSON payload structure. The `toArray()` call on the data collection handles its serialization.">
protected function defaultBody(): array
{
return [
'accountNumber' => $this->accountNumber,
'expireDate' => $this->expireDate,
'digestSignatures' => $this->digestSignatures->toArray(),
];
}
// </SECTION>
}