1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 
<?php

/* --------------------------------------------------------------
   OrderListItemJsonSerializer.inc.php 2016-01-14
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2016 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

MainFactory::load_class('AbstractJsonSerializer');

/**
 * Class OrderListItemJsonSerializer
 *
 * This class will serialize and deserialize an OrderListItem entity. It can be used into many
 * places where PHP interacts with external requests such as AJAX or API communication.
 *
 * @category   System
 * @package    Extensions
 * @subpackage Serializers
 */
class OrderListItemJsonSerializer extends AbstractJsonSerializer
{
    /**
     * Serialize an OrderListItem object to a JSON string.
     *
     * @param OrderListItem $object Object instance to be serialized.
     * @param bool          $encode (optional) Whether to json_encode the result of the method (default true). Sometimes
     *                              it might be required to encode an array of multiple customer records together and
     *                              not one by one.
     *
     * @return string|array Returns the json encoded order list item (string) or an array that can be easily encoded
     *                      into a JSON string.
     * @throws InvalidArgumentException If the provided object type is invalid.
     */
    public function serialize($object, $encode = true)
    {
        if(!is_a($object, 'OrderListItem'))
        {
            throw new InvalidArgumentException('Invalid argument provided, OrderListItem object required: '
                                               . get_class($object));
        }
        
        $orderListItem = array(
            'id'                 => $object->getOrderId(),
            'statusId'           => $object->getOrderStatusId(),
            'statusName'         => $object->getOrderStatusName(),
            'totalSum'           => $object->getTotalSum(),
            'purchaseDate'       => $object->getPurchaseDateTime()->format('Y-m-d H:i:s'),
            'comment'            => $object->getComment(),
            'withdrawalIds'      => $this->_serializeWithDrawalIds($object->getWithdrawalIds()),
            'mailStatus'         => $object->getMailStatus(),
            'customerId'         => $object->getCustomerId(),
            'customerName'       => $object->getCustomerName(),
            'customerEmail'      => $object->getCustomerEmail(),
            'customerStatusId'   => $object->getCustomerStatusId(),
            'customerStatusName' => $object->getCustomerStatusName(),
            'customerMemos'      => $this->_serializeCustomerMemos($object->getCustomerMemos()),
            'deliveryAddress'    => $this->_serializeOrderAddressBlock($object->getDeliveryAddress()),
            'billingAddress'     => $this->_serializeOrderAddressBlock($object->getBillingAddress()),
            'paymentType'        => array(
                'title'  => $object->getPaymentType()->getTitle(),
                'module' => $object->getPaymentType()->getModule()
            ),
            'shippingType'       => array(
                'title'  => $object->getShippingType()->getTitle(),
                'module' => $object->getShippingType()->getModule()
            ),
            'trackingLinks'      => $object->getTrackingLinks()->getStringArray(),
        );
        
        return $encode ? $this->jsonEncode($orderListItem) : $orderListItem;
    }
    
    
    /**
     * Deserialize method is not used by the api.
     *
     * @param string   $string     JSON string that contains the data of the address.
     * @param stdClass $baseObject (optional) This parameter is not supported for this serializer because the
     *                             OrderListItem does not have any setter methods.
     *
     * @throws RuntimeException If the argument is not a string or is empty.
     */
    public function deserialize($string, $baseObject = null)
    {
        throw new RuntimeException('Method is not implemented');
    }
    
    
    /**
     * Serialize Customer Memo Collection
     *
     * @param CustomerMemoCollection $customerMemoCollection
     *
     * @return array
     */
    protected function _serializeCustomerMemos(CustomerMemoCollection $customerMemoCollection)
    {
        $customerMemoCollectionArray = [];
        
        /** @var CustomerMemo $customerMemo */
        foreach($customerMemoCollection->getArray() as $customerMemo)
        {
            $customerMemoCollectionArray[] = [
                'title'    => $customerMemo->getTitle(),
                'text'     => $customerMemo->getText(),
                'date'     => $customerMemo->getCreationDate()->format('Y-m-d H:i:s'),
                'posterId' => $customerMemo->getPosterId()
            ];
        }
        
        return $customerMemoCollectionArray;
    }
    
    
    /**
     * Serialize Order AddressBlock
     *
     * @param OrderAddressBlock $orderAddressBlock
     *
     * @return array
     */
    protected function _serializeOrderAddressBlock(OrderAddressBlock $orderAddressBlock)
    {
        $orderAddressBlockArray = [
            'firstName'             => $orderAddressBlock->getFirstName(),
            'lastName'              => $orderAddressBlock->getLastName(),
            'company'               => $orderAddressBlock->getCompany(),
            'street'                => $orderAddressBlock->getStreet(),
            'houseNumber'           => $orderAddressBlock->getHouseNumber(),
            'additionalAddressInfo' => $orderAddressBlock->getAdditionalAddressInfo(),
            'postcode'              => $orderAddressBlock->getPostcode(),
            'city'                  => $orderAddressBlock->getCity(),
            'state'                 => $orderAddressBlock->getState(),
            'country'               => $orderAddressBlock->getCountry(),
            'countryIsoCode'        => $orderAddressBlock->getCountryIsoCode()
        ];
        
        return $orderAddressBlockArray;
    }
    
    
    /**
     * Serialize WithdrawalIds Collection
     *
     * @param IdCollection $withdrawalIds
     *
     * @return array
     */
    protected function _serializeWithdrawalIds(IdCollection $withdrawalIds)
    {
        $withdrawalIdsArray = [];
        
        foreach($withdrawalIds->getArray() as $withdrawalId)
        {
            $withdrawalIdsArray[] = $withdrawalId->asInt();
        }
        
        return $withdrawalIdsArray;
    }
}