refactor(core): enhance SDK structure and documentation
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
@@ -1,7 +1,14 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
// 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;
|
||||
@@ -9,26 +16,44 @@ 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="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 [
|
||||
@@ -37,9 +62,13 @@ class AddDealRequest extends Request implements HasBody
|
||||
'digestSignatures' => $this->digestSignatures->toArray(),
|
||||
];
|
||||
}
|
||||
// </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>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user