From ca78366886a4a138679869cadb0cdb952ef70a19 Mon Sep 17 00:00:00 2001 From: Leonardo Kagohara Date: Mon, 10 Mar 2025 14:49:31 -0300 Subject: [PATCH] Fix sub resource --- src/utils/rest.php | 22 ++--- tests/testRestGet.php | 194 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 202 insertions(+), 14 deletions(-) diff --git a/src/utils/rest.php b/src/utils/rest.php index a2c6150..33789bc 100644 --- a/src/utils/rest.php +++ b/src/utils/rest.php @@ -87,11 +87,11 @@ public static function getId($sdkVersion, $host, $apiVersion, $user, $resource, return API::fromApiJson($resource["maker"], $entity); } - public static function getContent($sdkVersion, $host, $apiVersion, $user, $resource, $id, $subresourceName, $language, $timeout, $options = null) + public static function getContent($sdkVersion, $host, $apiVersion, $user, $resource, $id, $subResourceName, $language, $timeout, $options = null) { $id = Checks::checkId($id); $options = API::castJsonToApiFormat($options); - $path = API::endpoint($resource["name"]) . "/" . $id . "/" . $subresourceName; + $path = API::endpoint($resource["name"]) . "/" . $id . "/" . $subResourceName; return Request::fetch( $host, $sdkVersion, @@ -106,11 +106,11 @@ public static function getContent($sdkVersion, $host, $apiVersion, $user, $resou )->content; } - public static function getSubresource($sdkVersion, $host, $apiVersion, $user, $resource, $id, $subresource, $language, $timeout, $options = null) + public static function getSubResource($sdkVersion, $host, $apiVersion, $user, $resource, $id, $subResource, $language, $timeout, $options = null) { $id = Checks::checkId($id); $options = API::castJsonToApiFormat($options); - $path = API::endpoint($resource["name"]) . "/" . $id . "/" . API::endpoint($subresource["name"]); + $path = API::endpoint($resource["name"]) . "/" . $id . "/" . API::endpoint($subResource["name"]); $json = Request::fetch( $host, $sdkVersion, @@ -123,27 +123,27 @@ public static function getSubresource($sdkVersion, $host, $apiVersion, $user, $r $language, $timeout )->json(); - $entity = $json[API::lastName($subresource["name"])]; - return API::fromApiJson($subresource["maker"], $entity); + $entity = $json[API::lastName($subResource["name"])]; + return API::fromApiJson($subResource["maker"], $entity); } - public static function postSubResource($sdkVersion, $host, $apiVersion, $user, $resource, $id, $subresource, $entity, $language, $timeout) + public static function postSubResource($sdkVersion, $host, $apiVersion, $user, $resource, $id, $subResource, $entity, $language, $timeout) { - $payload = API::apiJson($entity, $subresource["name"]); + $payload = API::apiJson($entity, $subResource["name"]); $json = Request::fetch( $host, $sdkVersion, $user, "POST", - API::endpoint($resource["name"]) . "/" . $id . "/" . API::endpoint($subresource["name"]), + API::endpoint($resource["name"]) . "/" . $id . "/" . API::endpoint($subResource["name"]), $payload, null, $apiVersion, $language, $timeout )->json(); - $entityJson = $json[API::lastName($subresource["name"])]; - return API::fromApiJson($subresource["maker"], $entityJson); + $entityJson = $json[API::lastName($subResource["name"])]; + return API::fromApiJson($subResource["maker"], $entityJson); } public static function post($sdkVersion, $host, $apiVersion, $user, $resource, $entities, $language, $timeout, $query) diff --git a/tests/testRestGet.php b/tests/testRestGet.php index 75faf25..b0c63a5 100644 --- a/tests/testRestGet.php +++ b/tests/testRestGet.php @@ -6,6 +6,7 @@ use StarkCore\Utils\Rest; use StarkCore\Utils\Checks; use StarkCore\Utils\Resource; +use StarkCore\Utils\SubResource; use StarkCore\Utils\StarkHost; @@ -29,10 +30,114 @@ public static function resource() } } +class Payment extends SubResource +{ + + public $name; + public $taxId; + public $bankCode; + public $branchCode; + public $accountNumber; + public $accountType; + public $amount; + public $endToEndId; + public $method; + + function __construct(array $params) + { + $this->name = Checks::checkParam($params, "name"); + $this->taxId = Checks::checkParam($params, "taxId"); + $this->bankCode = Checks::checkParam($params, "bankCode"); + $this->branchCode = Checks::checkParam($params, "branchCode"); + $this->accountNumber = Checks::checkParam($params, "accountNumber"); + $this->accountType = Checks::checkParam($params, "accountType"); + $this->amount = Checks::checkParam($params, "amount"); + $this->endToEndId = Checks::checkParam($params, "endToEndId"); + $this->method = Checks::checkParam($params, "method"); + + Checks::checkParams($params); + } + + public static function subResource() + { + $payment = function ($array) { + return new Payment($array); + }; + return [ + "name" => "Payment", + "maker" => $payment, + ]; + } +} + +class Invoice extends Resource +{ + + public $amount; + public $due; + public $taxId; + public $name; + public $fine; + public $interest; + public $discounts; + public $tags; + public $descriptions; + public $pdf; + public $link; + public $nominalAmount; + public $fineAmount; + public $interestAmount; + public $discountAmount; + public $brcode; + public $fee; + public $status; + public $transactionIds; + public $created; + public $updated; + + function __construct(array $params) + { + parent::__construct($params); + + $this->amount = Checks::checkParam($params, "amount"); + $this->due = Checks::checkDateTime(Checks::checkParam($params, "due")); + $this->taxId = Checks::checkParam($params, "taxId"); + $this->name = Checks::checkParam($params, "name"); + $this->fine = Checks::checkParam($params, "fine"); + $this->interest = Checks::checkParam($params, "interest"); + $this->tags = Checks::checkParam($params, "tags"); + $this->descriptions = Checks::checkParam($params, "descriptions"); + $this->pdf = Checks::checkParam($params, "pdf"); + $this->link = Checks::checkParam($params, "link"); + $this->nominalAmount = Checks::checkParam($params, "nominalAmount"); + $this->fineAmount = Checks::checkParam($params, "fineAmount"); + $this->interestAmount = Checks::checkParam($params, "interestAmount"); + $this->discountAmount = Checks::checkParam($params, "discountAmount"); + $this->brcode = Checks::checkParam($params, "brcode"); + $this->fee = Checks::checkParam($params, "fee"); + $this->status = Checks::checkParam($params, "status"); + $this->transactionIds = Checks::checkParam($params, "transactionIds"); + $this->created = Checks::checkDateTime(Checks::checkParam($params, "created")); + $this->updated = Checks::checkDateTime(Checks::checkParam($params, "updated")); + + } + + public static function resource() + { + $invoice = function ($array) { + return new Invoice($array); + }; + return [ + "name" => "Invoice", + "maker" => $invoice, + ]; + } +} + class TestRestGet { - public function testSuccess() + public function testGetPageSuccess() { list($transactions, $_cursor) = Rest::getPage( "0.0.0", @@ -50,11 +155,94 @@ public function testSuccess() throw new Exception("failed"); } } + + public function testGetContentSuccess() + { + list($invoices, $_cursor) = Rest::getPage( + "0.0.0", + StarkHost::bank, + "v2", + User::exampleProject(), + Invoice::resource(), + "pt-BR", + 15, + ["before" => "2022-02-01", "limit" => 1] + ); + + $id = $invoices[0] -> id; + + $content = Rest::getContent( + "0.0.0", + StarkHost::bank, + "v2", + User::exampleProject(), + Invoice::resource(), + $id, + "pdf", + "pt-BR", + 15, + null + ); + + $fp = fopen('invoice.pdf', 'w'); + fwrite($fp, $content); + fclose($fp); + } + + public function testGetSubResourceSuccess() + { + list($invoices, $_cursor) = Rest::getPage( + "0.0.0", + StarkHost::bank, + "v2", + User::exampleProject(), + Invoice::resource(), + "pt-BR", + 15, + ["before" => "2022-02-01", "limit" => 1] + ); + + $id = $invoices[0] -> id; + + + $payment = function ($array) { + return new Payment($array); + }; + $subResource = [ + "name" => "Payment", + "maker" => $payment + ]; + + $content = Rest::getSubResource( + "0.0.0", + StarkHost::bank, + "v2", + User::exampleProject(), + Invoice::resource(), + $id, + $subResource, + "pt-BR", + 15, + null + ); + + print_r($content); + + } + } echo "\n\nRest Get:"; $tests = new TestRestGet(); -echo "\n\t- success"; -$tests->testSuccess(); +echo "\n\t- Get Page success"; +$tests->testGetPageSuccess(); +echo " - OK"; + +echo "\n\t- Get Content success"; +$tests->testGetContentSuccess(); +echo " - OK"; + +echo "\n\t- Get Sub Resource success"; +$tests->testGetSubResourceSuccess(); echo " - OK";