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 
<?php

/* --------------------------------------------------------------
   CustomerGroupSerializerTest.php 2018-03-12
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2018 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

/**
 * Class CustomerGroupSerializer
 *
 * @category   System
 * @package    CustomerGroup
 * @subpackage Serializers
 */
class CustomerGroupSerializer implements CustomerGroupSerializerInterface
{
    /**
     * @var \CustomerGroupSettingsSerializerInterface
     */
    protected $settingsSerializer;
    
    /**
     * @var \CustomerGroupConfigurationsSerializerInterface
     */
    protected $configurationsSerializer;
    
    /**
     * @var \CustomerGroupFactory
     */
    protected $factory;
    
    
    /**
     * CustomerGroupSerializer constructor.
     *
     * @param \CustomerGroupSettingsSerializerInterface       $settingsSerializer
     * @param \CustomerGroupConfigurationsSerializerInterface $configurationsSerializer
     * @param \CustomerGroupFactory                           $customerGroupFactory
     */
    public function __construct(CustomerGroupSettingsSerializerInterface $settingsSerializer,
                                CustomerGroupConfigurationsSerializerInterface $configurationsSerializer,
                                CustomerGroupFactory $customerGroupFactory)
    {
        $this->settingsSerializer       = $settingsSerializer;
        $this->configurationsSerializer = $configurationsSerializer;
        $this->factory                  = $customerGroupFactory;
    }
    
    
    /**
     * Serialize a value to a JSON string.
     *
     * @param \CustomerGroupInterface $customerGroup Content to be serialized.
     *
     * @return array
     */
    public function serialize(CustomerGroupInterface $customerGroup)
    {
        return [
            'id'             => $customerGroup->getId(),
            'names'          => $this->_serializeCustomerGroupNames($customerGroup->getNames()),
            'settings'       => $this->settingsSerializer->serialize($customerGroup->getSettings()),
            'configurations' => $this->configurationsSerializer->serialize($customerGroup->getConfigurations())
        ];
    }
    
    
    /**
     * JSON Encode Wrapper.
     *
     * @param \CustomerGroupInterface $customerGroup Content to be encoded.
     *
     * @return string Returns the encoded JSON string that represents the data.
     */
    public function encode(CustomerGroupInterface $customerGroup)
    {
        return json_encode($this->serialize($customerGroup), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
    }
    
    
    /**
     * Deserialize a JSON string.
     *
     * @param \StringType $customerGroup Content to be deserialize.
     *
     * @return \CustomerGroupInterface
     */
    public function deserialize(StringType $customerGroup)
    {
        $customerGroupData   = json_decode($customerGroup->asString(), true);
        $customerGroupObject = $this->factory->createEntity()->setId(new IntType($customerGroupData['id']));
        
        foreach($customerGroupData['names'] as $languageCode => $name)
        {
            $customerGroupObject->setName(new StringType($name), new LanguageCode(new StringType($languageCode)));
        }
        
        $customerGroupObject->setSettings($this->settingsSerializer->deserialize(new StringType(json_encode($customerGroupData['settings']))))
                            ->setConfigurations($this->configurationsSerializer->deserialize(new StringType(json_encode($customerGroupData['configurations']))));
        
        return $customerGroupObject;
    }
    
    
    /**
     * Serializes customer group names array.
     *
     * @param array $customerGroupNames Customer group names array to be serialized.
     *
     * @return array Serialized customer group names array.
     */
    protected function _serializeCustomerGroupNames(array $customerGroupNames)
    {
        $data = [];
        foreach($customerGroupNames as $languageCode => $name)
        {
            $data[$languageCode] = $name;
        }
        
        return $data;
    }
}