From 76b27e7191958c7e18023293d4324e02d5552c22 Mon Sep 17 00:00:00 2001 From: Alexey Kopytko Date: Thu, 29 Oct 2020 12:49:54 +0900 Subject: [PATCH] Add UpdateOrderRequest --- ...esRequest.php => 030_DeliveryServices.php} | 0 ...stalCodeRequest.php => 040_PostalCode.php} | 0 ...0_LocationRequest.php => 050_Location.php} | 0 ...sRequest.php => 060_WithdrawIntervals.php} | 0 ...alsRequest.php => 070_ImportIntervals.php} | 0 ...onsRequest.php => 080_DeliveryOptions.php} | 0 ...teOrderRequest.php => 090_CreateOrder.php} | 0 ...itOrderRequest.php => 100_SubmitOrder.php} | 0 examples/110_CreateAndSubmitOrder.php | 37 +-- ...SearchRequest.php => 120_OrdersSearch.php} | 0 examples/130_UpdateOrder.php | 220 ++++++++++++++++++ .../OrderRequestBuilder.php} | 27 ++- src/Requests/CreateOrderRequest.php | 18 +- src/Requests/Shortcuts.php | 1 + src/Requests/UpdateOrderRequest.php | 94 ++++++++ src/Responses/CreateOrderResponse.php | 68 ------ src/Responses/DeliveryOptionsResponse.php | 16 ++ src/Responses/OrderResponse.php | 24 +- src/Responses/Types/DeliveryOption.php | 11 + .../DeliveryOptionsRequestTest.php | 2 +- .../DeliveryServicesRequestTest.php | 2 +- .../ImportIntervalsRequestTest.php | 2 +- tests/Integration/LocationRequestTest.php | 2 +- tests/Integration/PostalCodeRequestTest.php | 2 +- tests/Integration/SubmitOrderRequestTest.php | 4 +- .../WithdrawIntervalsRequestTest.php | 2 +- 26 files changed, 427 insertions(+), 105 deletions(-) rename examples/{030_DeliveryServicesRequest.php => 030_DeliveryServices.php} (100%) rename examples/{040_PostalCodeRequest.php => 040_PostalCode.php} (100%) rename examples/{050_LocationRequest.php => 050_Location.php} (100%) rename examples/{060_WithdrawIntervalsRequest.php => 060_WithdrawIntervals.php} (100%) rename examples/{070_ImportIntervalsRequest.php => 070_ImportIntervals.php} (100%) rename examples/{080_DeliveryOptionsRequest.php => 080_DeliveryOptions.php} (100%) rename examples/{090_CreateOrderRequest.php => 090_CreateOrder.php} (100%) rename examples/{100_SubmitOrderRequest.php => 100_SubmitOrder.php} (100%) rename examples/{120_OrdersSearchRequest.php => 120_OrdersSearch.php} (100%) create mode 100644 examples/130_UpdateOrder.php rename src/Requests/{CreateOrderRequest/Builder.php => Builders/OrderRequestBuilder.php} (90%) create mode 100644 src/Requests/UpdateOrderRequest.php delete mode 100644 src/Responses/CreateOrderResponse.php diff --git a/examples/030_DeliveryServicesRequest.php b/examples/030_DeliveryServices.php similarity index 100% rename from examples/030_DeliveryServicesRequest.php rename to examples/030_DeliveryServices.php diff --git a/examples/040_PostalCodeRequest.php b/examples/040_PostalCode.php similarity index 100% rename from examples/040_PostalCodeRequest.php rename to examples/040_PostalCode.php diff --git a/examples/050_LocationRequest.php b/examples/050_Location.php similarity index 100% rename from examples/050_LocationRequest.php rename to examples/050_Location.php diff --git a/examples/060_WithdrawIntervalsRequest.php b/examples/060_WithdrawIntervals.php similarity index 100% rename from examples/060_WithdrawIntervalsRequest.php rename to examples/060_WithdrawIntervals.php diff --git a/examples/070_ImportIntervalsRequest.php b/examples/070_ImportIntervals.php similarity index 100% rename from examples/070_ImportIntervalsRequest.php rename to examples/070_ImportIntervals.php diff --git a/examples/080_DeliveryOptionsRequest.php b/examples/080_DeliveryOptions.php similarity index 100% rename from examples/080_DeliveryOptionsRequest.php rename to examples/080_DeliveryOptions.php diff --git a/examples/090_CreateOrderRequest.php b/examples/090_CreateOrder.php similarity index 100% rename from examples/090_CreateOrderRequest.php rename to examples/090_CreateOrder.php diff --git a/examples/100_SubmitOrderRequest.php b/examples/100_SubmitOrder.php similarity index 100% rename from examples/100_SubmitOrderRequest.php rename to examples/100_SubmitOrder.php diff --git a/examples/110_CreateAndSubmitOrder.php b/examples/110_CreateAndSubmitOrder.php index f08cb0d..f815dc4 100644 --- a/examples/110_CreateAndSubmitOrder.php +++ b/examples/110_CreateAndSubmitOrder.php @@ -80,7 +80,7 @@ $request->from->geoId = $_SERVER['YANDEX_SENDER_GEOID']; $request->to->location = 'Новосибирск, ул. Державина, 5'; -$request->dimensions->length = $length = 10; +$request->dimensions->length = 10; $request->dimensions->width = 20; $request->dimensions->height = 30; $request->dimensions->weight = 1.25; @@ -100,23 +100,12 @@ $logger->addFile('delivery-options-request.json'); $logger->addFile('delivery-options-response.json'); -$response = $client->sendDeliveryOptionsRequest($request); +$deliveryMethods = $client->sendDeliveryOptionsRequest($request); -if (!\count($response)) { +if (!\count($deliveryMethods)) { return; } -/** - * @var \Traversable<\YDeliverySDK\Responses\Types\DeliveryOption> $response - */ -$deliveryMethods = \iterator_to_array($response, false); - -/* -\usort($deliveryMethods, function (\YDeliverySDK\Responses\Types\DeliveryOption $a, \YDeliverySDK\Responses\Types\DeliveryOption $b) { - return $a->cost->deliveryForSender <=> $b->cost->deliveryForSender; -}); -*/ - /** @var \YDeliverySDK\Responses\Types\DeliveryOption $deliveryMethod */ foreach ($deliveryMethods as $deliveryMethod) { echo \join("\t", [ @@ -127,13 +116,29 @@ $deliveryMethod->delivery->calculatedDeliveryDateMin->format('Y-m-d'), $deliveryMethod->delivery->calculatedDeliveryDateMax->format('Y-m-d'), ]), "\n"; - break; } +$deliveryMethod = $deliveryMethods->getFirstTagged($deliveryMethods::TAG_CHEAPEST); + +if (!$deliveryMethod) { + return; +} + +echo "\nCheapest delivery method:\n"; + +echo \join("\t", [ + $deliveryMethod->delivery->type, + $deliveryMethod->tariffId, + $deliveryMethod->tariffName ?? 'Без названия', + $deliveryMethod->cost->deliveryForSender, + $deliveryMethod->delivery->calculatedDeliveryDateMin->format('Y-m-d'), + $deliveryMethod->delivery->calculatedDeliveryDateMax->format('Y-m-d'), +]), "\n"; + /** * Отправим заказ. */ -$requestBuilder = new CreateOrderRequest\Builder($deliveryMethod, $location); +$requestBuilder = CreateOrderRequest::builder($deliveryMethod, $location); $requestBuilder->setPostalCode($postalCode); $request = $requestBuilder->build(); diff --git a/examples/120_OrdersSearchRequest.php b/examples/120_OrdersSearch.php similarity index 100% rename from examples/120_OrdersSearchRequest.php rename to examples/120_OrdersSearch.php diff --git a/examples/130_UpdateOrder.php b/examples/130_UpdateOrder.php new file mode 100644 index 0000000..5f1670a --- /dev/null +++ b/examples/130_UpdateOrder.php @@ -0,0 +1,220 @@ + and contributors + * Copyright (c) 2018 Appwilio (http://appwilio.com), greabock (https://github.com/greabock), JhaoDa (https://github.com/jhaoda) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +declare(strict_types=1); + +use Tests\YDeliverySDK\Integration\DebuggingLogger; +use YDeliverySDK\Requests\CreateOrderRequest; +use YDeliverySDK\Requests\DeliveryOptionsRequest; +use YDeliverySDK\Requests\UpdateOrderRequest; + +include_once 'vendor/autoload.php'; + +$logger = new DebuggingLogger(); + +$builder = new \YDeliverySDK\ClientBuilder(); +$builder->setToken($_SERVER['YANDEX_DELIVERY_TOKEN'] ?? ''); +$builder->setLogger($logger); +/** @var \YDeliverySDK\Client $client */ +$client = $builder->build(); + +/** + * Получим данные по адресу получателя. + */ +$response = $client->makeLocationRequest('Новосибирская область, Новосибирск'); + +\var_dump(\count($response)); + +foreach ($response as $location) { + echo "{$location->geoId}\t{$location->address}\n"; + + foreach ($location->addressComponents as $component) { + echo "- {$component->kind}: {$component->name}\n"; + } + break; +} + +/** + * Получим почтовый индекс. + */ +$response = $client->makePostalCodeRequest('Новосибирская область, Новосибирск, ул. Державина, 5'); + +\var_dump(\count($response)); + +foreach ($response as $postalCode) { + echo "$postalCode\n"; + + break; +} + +/** + * Получим тарифы. + */ +$request = new DeliveryOptionsRequest(); + +$request->senderId = $_SERVER['YANDEX_SHOP_ID']; + +$request->from->geoId = $_SERVER['YANDEX_SENDER_GEOID']; +$request->to->location = 'Новосибирск, ул. Державина, 5'; + +$request->dimensions->length = 10; +$request->dimensions->width = 20; +$request->dimensions->height = 30; +$request->dimensions->weight = 1.25; +$dimensions = $request->dimensions; + +$request->deliveryType = $request::DELIVERY_TYPE_COURIER; + +// $request->shipment->date = new DateTime('next Monday'); +$request->shipment->type = $request->shipment::TYPE_WITHDRAW; + +$request->cost->assessedValue = 1000; +$request->cost->itemsSum = 1000; +$request->cost->fullyPrepaid = true; + +// $request->tariffId = 333333333; + +$logger->addFile('delivery-options-request.json'); +$logger->addFile('delivery-options-response.json'); + +$deliveryMethods = $client->sendDeliveryOptionsRequest($request); + +if (!\count($deliveryMethods)) { + return; +} + +$deliveryMethod = $deliveryMethods->getFirstTagged($deliveryMethods::TAG_CHEAPEST); + +if (!$deliveryMethod) { + return; +} + +/** + * Отправим заказ. + */ +$requestBuilder = CreateOrderRequest::builder($deliveryMethod, $location); +$requestBuilder->setPostalCode($postalCode); +$request = $requestBuilder->build(); + +$request->shipment->warehouseFrom = (int) $_SERVER['YANDEX_WAREHOUSE_ID']; +$request->senderId = (int) $_SERVER['YANDEX_SHOP_ID']; +$request->comment = 'Доставки не будет - тестовый заказ'; +// $request->externalId = '426'; + +$request->recipient->firstName = 'Василий'; +$request->recipient->lastName = 'Юрочкин'; +$request->recipient->phone = '+79266056128'; + +$request->recipient->address->apartment = '43'; +$request->recipient->address->house = '5'; +$request->recipient->address->housing = ''; +$request->recipient->address->street = 'ул. Державина'; +//$request->recipient->pickupPointId = 10000018299; + +$place = $request->addPlace($dimensions); +// $place->externalId = '427'; +$item = $place->addItem(); +$item->externalId = '428'; +$item->name = 'HELP'; +$item->count = 2; +$item->price = 500; +$item->assessedValue = 500; +// $item->tax = $item::TAX_NO_VAT; + +$request->cost->assessedValue = 1000; +$request->cost->fullyPrepaid = true; +$request->cost->paymentMethod = $request->cost::PAYMENT_METHOD_PREPAID; + +$contact = $request->addContact(); + +$contact->phone = '+79266056128'; +$contact->firstName = 'Василий'; +$contact->lastName = 'Юрочкин'; + +$logger->addFile('create-order-request.json'); +$logger->addFile('create-order-response.json'); + +$response = $client->sendCreateOrderRequest($request); + +if ($response->hasErrors()) { + // Обрабатываем ошибки + foreach ($response->getMessages() as $message) { + if ($message->getErrorCode() !== '') { + // Это ошибка + echo "{$message->getErrorCode()}: {$message->getMessage()}\n"; + } + } + + return; +} + +$requestBuilder = UpdateOrderRequest::builder($response->id, $deliveryMethod, $location); +$requestBuilder->setPostalCode($postalCode); +$request = $requestBuilder->build(); + +$request->shipment->warehouseFrom = (int) $_SERVER['YANDEX_WAREHOUSE_ID']; +$request->senderId = (int) $_SERVER['YANDEX_SHOP_ID']; +$request->comment = 'Доставки не будет - тестовый заказ'; +// $request->externalId = '426'; + +$request->recipient->firstName = 'Василий'; +$request->recipient->lastName = 'Юрочкин'; +$request->recipient->phone = '+79266056128'; + +$request->recipient->address->apartment = '43'; +$request->recipient->address->house = '5'; +$request->recipient->address->housing = ''; +$request->recipient->address->street = 'ул. Державина'; +//$request->recipient->pickupPointId = 10000018299; + +$place = $request->addPlace($dimensions); +// $place->externalId = '427'; +$item = $place->addItem(); +$item->externalId = '428'; +$item->name = 'HELP'; +$item->count = 2; +$item->price = 500; +$item->assessedValue = 500; +// $item->tax = $item::TAX_NO_VAT; + +$request->cost->assessedValue = 1000; +$request->cost->fullyPrepaid = true; +$request->cost->paymentMethod = $request->cost::PAYMENT_METHOD_PREPAID; + +$contact = $request->addContact(); + +$contact->phone = '+79266056128'; +$contact->firstName = 'Василий'; +$contact->lastName = 'Юрочкин'; + +$request->comment = 'Доставки не будет - тестовый заказ (обновлено)'; + +$logger->addFile('update-order-request.json'); +$logger->addFile('update-order-response.json'); + +$response = $client->sendUpdateOrderRequest($request); + +echo "\n\nUpdated: {$response->id}\n\n"; diff --git a/src/Requests/CreateOrderRequest/Builder.php b/src/Requests/Builders/OrderRequestBuilder.php similarity index 90% rename from src/Requests/CreateOrderRequest/Builder.php rename to src/Requests/Builders/OrderRequestBuilder.php index 142bcd7..dd24ada 100644 --- a/src/Requests/CreateOrderRequest/Builder.php +++ b/src/Requests/Builders/OrderRequestBuilder.php @@ -26,7 +26,7 @@ declare(strict_types=1); -namespace YDeliverySDK\Requests\CreateOrderRequest; +namespace YDeliverySDK\Requests\Builders; use YDeliverySDK\Requests\CreateOrderRequest; use YDeliverySDK\Requests\Templates\OrderRequest; @@ -36,13 +36,18 @@ use YDeliverySDK\Responses\Types\PostalCode; /** - * CreateOrderRequest builder. + * OrderRequestRequest builder. + * + * @psalm-template T of OrderRequest * * @see https://yandex.ru/dev/delivery-3/doc/dg/reference/post-orders.html */ -final class Builder +final class OrderRequestBuilder { - /** @var CreateOrderRequest */ + /** + * @var OrderRequest + * @psalm-var T + */ private $request; /** @var DeliveryOption */ @@ -60,9 +65,12 @@ final class Builder /** @var PostalCode|null */ private $postalCode; - public function __construct(DeliveryOption $deliveryOption, Location $location, CreateOrderRequest $request = null) + /** + * @psalm-param T $request + */ + public function __construct(DeliveryOption $deliveryOption, Location $location, OrderRequest $request) { - $this->request = $request ?? new CreateOrderRequest(new OrderRequest\DeliveryOption($deliveryOption->services)); + $this->request = $request; $this->deliveryOption = $deliveryOption; $this->location = $location; } @@ -74,7 +82,12 @@ public function setPostalCode(PostalCode $postalCode): self return $this; } - public function build(): CreateOrderRequest + /** + * @psalm-return T + * + * @return OrderRequest + */ + public function build() { $this->request->deliveryType = $this->deliveryOption->delivery->type; $this->request->shipment->date = $this->deliveryOption->shipments[$this->shipment]->date; diff --git a/src/Requests/CreateOrderRequest.php b/src/Requests/CreateOrderRequest.php index debd3a6..5dc11bb 100644 --- a/src/Requests/CreateOrderRequest.php +++ b/src/Requests/CreateOrderRequest.php @@ -29,8 +29,10 @@ namespace YDeliverySDK\Requests; use CommonSDK\Concerns\RequestCore; +use YDeliverySDK\Requests\Builders\OrderRequestBuilder; use YDeliverySDK\Requests\Templates\OrderRequest; -use YDeliverySDK\Responses\CreateOrderResponse; +use YDeliverySDK\Responses\OrderResponse; +use YDeliverySDK\Responses\Types as ResponsesTypes; /** * CreateOrderRequest. @@ -44,5 +46,17 @@ final class CreateOrderRequest extends OrderRequest private const METHOD = 'POST'; private const ADDRESS = '/orders'; - protected const RESPONSE = CreateOrderResponse::class; + protected const RESPONSE = OrderResponse::class; + + /** + * @return OrderRequestBuilder + */ + public static function builder( + ResponsesTypes\DeliveryOption $deliveryOption, + ResponsesTypes\Location $location + ) { + return new OrderRequestBuilder($deliveryOption, $location, new static( + new OrderRequest\DeliveryOption($deliveryOption->services) + )); + } } diff --git a/src/Requests/Shortcuts.php b/src/Requests/Shortcuts.php index a7fb8d6..e9ff630 100644 --- a/src/Requests/Shortcuts.php +++ b/src/Requests/Shortcuts.php @@ -41,6 +41,7 @@ * @method Responses\IntervalsResponse|Responses\Types\Interval[] sendImportIntervalsRequest(ImportIntervalsRequest $request) * @method Responses\DeliveryOptionsResponse|Responses\Types\DeliveryOption[] sendDeliveryOptionsRequest(DeliveryOptionsRequest $request) * @method Responses\OrderResponse sendCreateOrderRequest(CreateOrderRequest $request) + * @method Responses\OrderResponse sendUpdateOrderRequest(UpdateOrderRequest $request) * @method Responses\SubmitOrderResponse|Responses\Types\SubmittedOrder[] sendSubmitOrderRequest(SubmitOrderRequest $request) * @method Responses\OrdersSearchResponse|Responses\Types\Order[] sendOrdersSearchRequest(OrdersSearchRequest $request) * @method Contracts\Response sendDeleteOrderRequest(DeleteOrderRequest $request) diff --git a/src/Requests/UpdateOrderRequest.php b/src/Requests/UpdateOrderRequest.php new file mode 100644 index 0000000..bec6f5a --- /dev/null +++ b/src/Requests/UpdateOrderRequest.php @@ -0,0 +1,94 @@ + and contributors + * Copyright (c) 2018 Appwilio (http://appwilio.com), greabock (https://github.com/greabock), JhaoDa (https://github.com/jhaoda) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +declare(strict_types=1); + +namespace YDeliverySDK\Requests; + +use CommonSDK\Concerns\RequestCore; +use JMS\Serializer\Annotation as JMS; +use YDeliverySDK\Requests\Builders\OrderRequestBuilder; +use YDeliverySDK\Requests\Templates\OrderRequest; +use YDeliverySDK\Responses\OrderResponse; +use YDeliverySDK\Responses\Types as ResponsesTypes; + +/** + * UpdateOrderRequest. + * + * @see https://yandex.ru/dev/delivery-3/doc/dg/reference/put-orders-id.html + */ +final class UpdateOrderRequest extends OrderRequest +{ + use RequestCore; + + private const METHOD = 'PUT'; + private const ADDRESS = '/orders/%s'; + + protected const RESPONSE = OrderResponse::class; + + /** + * @JMS\Exclude + * + * @var int + */ + private $id; + + /** + * @phan-suppress PhanAccessReadOnlyMagicProperty + */ + public function __construct( + int $id, + ?OrderRequest\DeliveryOption $deliveryOption = null, + ?OrderRequest\Recipient $recipient = null, + ?OrderRequest\Cost $cost = null, + array $contacts = [], + ?Types\Shipment $shipment = null, + array $places = [] + ) { + parent::__construct($deliveryOption, $recipient, $cost, $contacts, $shipment, $places); + + $this->id = $id; + } + + /** + * @return OrderRequestBuilder + */ + public static function builder( + int $id, + ResponsesTypes\DeliveryOption $deliveryOption, + ResponsesTypes\Location $location + ) { + return new OrderRequestBuilder($deliveryOption, $location, new static( + $id, + new OrderRequest\DeliveryOption($deliveryOption->services) + )); + } + + public function getAddress(): string + { + return \sprintf(static::ADDRESS, $this->id); + } +} diff --git a/src/Responses/CreateOrderResponse.php b/src/Responses/CreateOrderResponse.php deleted file mode 100644 index 733cb1e..0000000 --- a/src/Responses/CreateOrderResponse.php +++ /dev/null @@ -1,68 +0,0 @@ - and contributors - * Copyright (c) 2018 Appwilio (http://appwilio.com), greabock (https://github.com/greabock), JhaoDa (https://github.com/jhaoda) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -declare(strict_types=1); - -namespace YDeliverySDK\Responses; - -use CommonSDK\Concerns\PropertyRead; -use CommonSDK\Concerns\SuccessfulResponse; -use CommonSDK\Contracts\Response; -use JSONSerializer\Contracts\ScalarValue; - -/** - * CreateOrderResponse. - * - * @property-read int $id - */ -final class CreateOrderResponse implements Response, ScalarValue -{ - use PropertyRead; - use SuccessfulResponse; - - /** - * @var int - */ - private $id; - - /** - * @phan-suppress PhanAccessReadOnlyMagicProperty - * - * @param mixed $value - */ - public static function withValue($value) - { - $response = new self(); - $response->id = $value; - - return $response; - } - - public static function getType(): string - { - return 'int'; - } -} diff --git a/src/Responses/DeliveryOptionsResponse.php b/src/Responses/DeliveryOptionsResponse.php index 867de75..0d58f33 100644 --- a/src/Responses/DeliveryOptionsResponse.php +++ b/src/Responses/DeliveryOptionsResponse.php @@ -41,4 +41,20 @@ final class DeliveryOptionsResponse implements Response, ItemList, \IteratorAggr use ListContainer; private const LIST_TYPE = DeliveryOption::class; + + public const TAG_FASTEST = 'FASTEST'; + + public const TAG_CHEAPEST = 'CHEAPEST'; + + public function getFirstTagged(string $tag): ?DeliveryOption + { + foreach ($this as $option) { + /** @var DeliveryOption $option */ + if ($option->isTagged($tag)) { + return $option; + } + } + + return null; + } } diff --git a/src/Responses/OrderResponse.php b/src/Responses/OrderResponse.php index ca487c0..7fc0838 100644 --- a/src/Responses/OrderResponse.php +++ b/src/Responses/OrderResponse.php @@ -31,22 +31,38 @@ use CommonSDK\Concerns\PropertyRead; use CommonSDK\Concerns\SuccessfulResponse; use CommonSDK\Contracts\Response; -use JMS\Serializer\Annotation as JMS; +use JSONSerializer\Contracts\ScalarValue; /** * OrderResponse. * * @property-read int $id */ -final class OrderResponse implements Response +final class OrderResponse implements Response, ScalarValue { use PropertyRead; use SuccessfulResponse; /** - * @JMS\Type("int") - * * @var int */ private $id; + + /** + * @phan-suppress PhanAccessReadOnlyMagicProperty + * + * @param mixed $value + */ + public static function withValue($value) + { + $response = new self(); + $response->id = $value; + + return $response; + } + + public static function getType(): string + { + return 'int'; + } } diff --git a/src/Responses/Types/DeliveryOption.php b/src/Responses/Types/DeliveryOption.php index 07a9142..6974bfd 100644 --- a/src/Responses/Types/DeliveryOption.php +++ b/src/Responses/Types/DeliveryOption.php @@ -113,4 +113,15 @@ final class DeliveryOption * @var string[] */ private $tags = []; + + public function isTagged(string $withTag): bool + { + foreach ($this->tags as $tag) { + if ($tag === $withTag) { + return true; + } + } + + return false; + } } diff --git a/tests/Integration/DeliveryOptionsRequestTest.php b/tests/Integration/DeliveryOptionsRequestTest.php index 4c335bc..6094bb4 100644 --- a/tests/Integration/DeliveryOptionsRequestTest.php +++ b/tests/Integration/DeliveryOptionsRequestTest.php @@ -35,7 +35,7 @@ /** @psalm-suppress TypeDoesNotContainType */ if (false) { - include 'examples/080_DeliveryOptionsRequest.php'; + include 'examples/080_DeliveryOptions.php'; } /** diff --git a/tests/Integration/DeliveryServicesRequestTest.php b/tests/Integration/DeliveryServicesRequestTest.php index 2e7be07..5c4d93d 100644 --- a/tests/Integration/DeliveryServicesRequestTest.php +++ b/tests/Integration/DeliveryServicesRequestTest.php @@ -32,7 +32,7 @@ /** @psalm-suppress TypeDoesNotContainType */ if (false) { - include 'examples/030_DeliveryServicesRequest.php'; + include 'examples/030_DeliveryServices.php'; } /** diff --git a/tests/Integration/ImportIntervalsRequestTest.php b/tests/Integration/ImportIntervalsRequestTest.php index 5a924c3..e15a548 100644 --- a/tests/Integration/ImportIntervalsRequestTest.php +++ b/tests/Integration/ImportIntervalsRequestTest.php @@ -33,7 +33,7 @@ /** @psalm-suppress TypeDoesNotContainType */ if (false) { - include 'examples/070_ImportIntervalsRequest.php'; + include 'examples/070_ImportIntervals.php'; } /** diff --git a/tests/Integration/LocationRequestTest.php b/tests/Integration/LocationRequestTest.php index 054727f..d30fca8 100644 --- a/tests/Integration/LocationRequestTest.php +++ b/tests/Integration/LocationRequestTest.php @@ -32,7 +32,7 @@ /** @psalm-suppress TypeDoesNotContainType */ if (false) { - include 'examples/050_LocationRequest.php'; + include 'examples/050_Location.php'; } /** diff --git a/tests/Integration/PostalCodeRequestTest.php b/tests/Integration/PostalCodeRequestTest.php index 3e05444..bba744d 100644 --- a/tests/Integration/PostalCodeRequestTest.php +++ b/tests/Integration/PostalCodeRequestTest.php @@ -32,7 +32,7 @@ /** @psalm-suppress TypeDoesNotContainType */ if (false) { - include 'examples/040_PostalCodeRequest.php'; + include 'examples/040_PostalCode.php'; } /** diff --git a/tests/Integration/SubmitOrderRequestTest.php b/tests/Integration/SubmitOrderRequestTest.php index 86bd78b..317e44d 100644 --- a/tests/Integration/SubmitOrderRequestTest.php +++ b/tests/Integration/SubmitOrderRequestTest.php @@ -33,8 +33,8 @@ /** @psalm-suppress TypeDoesNotContainType */ if (false) { - include 'examples/090_CreateOrderRequest.php'; - include 'examples/100_SubmitOrderRequest.php'; + include 'examples/090_CreateOrder.php'; + include 'examples/100_SubmitOrder.php'; } /** diff --git a/tests/Integration/WithdrawIntervalsRequestTest.php b/tests/Integration/WithdrawIntervalsRequestTest.php index 4db42c4..3b256e3 100644 --- a/tests/Integration/WithdrawIntervalsRequestTest.php +++ b/tests/Integration/WithdrawIntervalsRequestTest.php @@ -33,7 +33,7 @@ /** @psalm-suppress TypeDoesNotContainType */ if (false) { - include 'examples/060_WithdrawIntervalsRequest.php'; + include 'examples/060_WithdrawIntervals.php'; } /**