Skip to content
This repository has been archived by the owner on May 21, 2021. It is now read-only.

Commit

Permalink
Skip object fields when empty
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Oct 26, 2020
1 parent 2048c57 commit e876b89
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 2 deletions.
1 change: 0 additions & 1 deletion examples/110_CreateAndSubmitOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@
$place = $request->addPlace($dimensions);
// $place->externalId = '427';
$item = $place->addItem();
$item->dimensions->length = $length / 2;
$item->externalId = '428';
$item->name = 'HELP';
$item->count = 2;
Expand Down
1 change: 1 addition & 0 deletions src/Common/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ abstract class Item

/**
* @JMS\Type("YDeliverySDK\Common\Dimensions")
* @JMS\SkipWhenEmpty
*
* @var Dimensions
*/
Expand Down
1 change: 1 addition & 0 deletions src/Common/Place.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ abstract class Place

/**
* @JMS\Type("YDeliverySDK\Common\Dimensions")
* @JMS\SkipWhenEmpty
*
* @var Dimensions
*/
Expand Down
1 change: 1 addition & 0 deletions src/Common/Recipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ abstract class Recipient

/**
* @JMS\Type("YDeliverySDK\Common\Address")
* @JMS\SkipWhenEmpty
*
* @var Address
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Requests/Templates/OrderRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,15 @@ abstract class OrderRequest implements JsonRequest

/**
* @JMS\Type("YDeliverySDK\Requests\Templates\OrderRequest\Recipient")
* @JMS\SkipWhenEmpty
*
* @var OrderRequest\Recipient
*/
protected $recipient;

/**
* @JMS\Type("YDeliverySDK\Requests\Templates\OrderRequest\Cost")
* @JMS\SkipWhenEmpty
*
* @var OrderRequest\Cost
*/
Expand All @@ -123,13 +125,15 @@ abstract class OrderRequest implements JsonRequest

/**
* @JMS\Type("YDeliverySDK\Common\DeliveryOption")
* @JMS\SkipWhenEmpty
*
* @var Common\DeliveryOption
*/
protected $deliveryOption;

/**
* @JMS\Type("YDeliverySDK\Requests\Types\Shipment")
* @JMS\SkipWhenEmpty
*
* @var Shipment
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Requests/Templates/OrderRequest/Place.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __construct(?Dimensions $dimensions = null, array $items = [])

public function addItem(?Dimensions $dimensions = null): Item
{
$item = new Item($dimensions ?? $this->dimensions);
$item = new Item($dimensions);

$this->items[] = $item;

Expand Down
54 changes: 54 additions & 0 deletions tests/Serialization/ItemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* This code is licensed under the MIT License.
*
* Copyright (c) 2018-2020 Alexey Kopytko <alexey@kopytko.com> 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 Tests\YDeliverySDK\Serialization;

use YDeliverySDK\Requests\Templates\OrderRequest;

/**
* @covers \YDeliverySDK\Requests\Templates\OrderRequest\Item
*/
final class ItemTest extends TestCase
{
public function test_serialize_item_without_dimensions()
{
$item = new OrderRequest\Item();
$item->externalId = '123';

$this->assertSameAsJSON('{"externalId":"123"}', $item);
}

public function test_serialize_item_with_dimensions()
{
$item = new OrderRequest\Item();
$item->externalId = '123';
$item->dimensions->height = 100;

$this->assertSameAsJSON('{"externalId":"123","dimensions":{"height":100.0}}', $item);
}
}

0 comments on commit e876b89

Please sign in to comment.