DeliveryTerm.php 3.86 KB
Newer Older
Ketan's avatar
Ketan committed
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
<?php
/**
 * @copyright  Vertex. All rights reserved.  https://www.vertexinc.com/
 * @author     Mediotype                     https://www.mediotype.com/
 */

namespace Vertex\Tax\Model\Config;

use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
use Magento\Framework\Math\Random;

/**
 * Class encode/decode Delivery Term configuration
 */
class DeliveryTerm
{
    /** @var Random */
    private $mathRandom;

    /**
     * @param Random $mathRandom
     */
    public function __construct(Random $mathRandom)
    {
        $this->mathRandom = $mathRandom;
    }

    /**
     * Make value readable by @see AbstractFieldArray
     *
     * @param string|array $value
     * @return array
     */
    public function makeArrayFieldValue($value)
    {
        $value = $this->unserializeValue($value);
        if (!$this->isEncodedArrayFieldValue($value)) {
            return $this->encodeArrayFieldValue($value);
        }

        return $this->unserializeValue($value);
    }

    /**
     * Make value ready for store
     *
     * @param string|array $value
     * @return string
     */
    public function makeStorableArrayFieldValue($value)
    {
        if ($this->isEncodedArrayFieldValue($value)) {
            $value = $this->decodeArrayFieldValue($value);
        }

        return $this->serializeValue($value);
    }

    /**
     * Decode value from used in @see AbstractFieldArray
     *
     * @param array $value
     * @return array
     */
    private function decodeArrayFieldValue(array $value)
    {
        $result = [];
        unset($value['__empty']);
        foreach ($value as $row) {
            if (!is_array($row)
                || !array_key_exists('country_id', $row)
                || !array_key_exists('delivery_term', $row)
            ) {
                continue;
            }
            $countryId = $row['country_id'];
            $deliveryTerm = $row['delivery_term'];
            $result[$countryId] = $deliveryTerm;
        }

        return $result;
    }

    /**
     * Encode value to be used in @see AbstractFieldArray
     *
     * @param array $value
     * @return array
     */
    private function encodeArrayFieldValue(array $value)
    {
        $result = [];
        foreach ($value as $countryId => $deliveryTerm) {
            $resultId = $this->mathRandom->getUniqueHash('_');
            $result[$resultId] = ['country_id' => $countryId, 'delivery_term' => $deliveryTerm];
        }

        return $result;
    }

    /**
     * Check whether value is in form retrieved by @see encodeArrayFieldValue
     *
     * @param string|array $value
     * @return bool
     */
    private function isEncodedArrayFieldValue($value)
    {
        if (!is_array($value)) {
            return false;
        }
        unset($value['__empty']);
        foreach ($value as $row) {
            if (!is_array($row)
                || !array_key_exists('country_id', $row)
                || !array_key_exists('delivery_term', $row)
            ) {
                return false;
            }
        }

        return true;
    }

    /**
     * Generate a storable representation of a value
     *
     * @param array $value
     * @return string
     */
    private function serializeValue($value)
    {
        if (is_array($value)) {
            $data = [];
            foreach ($value as $countryId => $deliveryTerm) {
                if (!array_key_exists($countryId, $data)) {
                    $data[$countryId] = $deliveryTerm;
                }
            }

            return json_encode($data, true);
        }

        return '';
    }

    /**
     * Create a value from a storable representation
     *
     * @param string|null $value
     * @return array
     */
    public function unserializeValue($value)
    {
        if (is_string($value) && !empty($value)) {
            return json_decode($value, true);
        }

        return [];
    }
}