DeliveryTerm.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Model\Config;
  7. use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
  8. use Magento\Framework\Math\Random;
  9. /**
  10. * Class encode/decode Delivery Term configuration
  11. */
  12. class DeliveryTerm
  13. {
  14. /** @var Random */
  15. private $mathRandom;
  16. /**
  17. * @param Random $mathRandom
  18. */
  19. public function __construct(Random $mathRandom)
  20. {
  21. $this->mathRandom = $mathRandom;
  22. }
  23. /**
  24. * Make value readable by @see AbstractFieldArray
  25. *
  26. * @param string|array $value
  27. * @return array
  28. */
  29. public function makeArrayFieldValue($value)
  30. {
  31. $value = $this->unserializeValue($value);
  32. if (!$this->isEncodedArrayFieldValue($value)) {
  33. return $this->encodeArrayFieldValue($value);
  34. }
  35. return $this->unserializeValue($value);
  36. }
  37. /**
  38. * Make value ready for store
  39. *
  40. * @param string|array $value
  41. * @return string
  42. */
  43. public function makeStorableArrayFieldValue($value)
  44. {
  45. if ($this->isEncodedArrayFieldValue($value)) {
  46. $value = $this->decodeArrayFieldValue($value);
  47. }
  48. return $this->serializeValue($value);
  49. }
  50. /**
  51. * Decode value from used in @see AbstractFieldArray
  52. *
  53. * @param array $value
  54. * @return array
  55. */
  56. private function decodeArrayFieldValue(array $value)
  57. {
  58. $result = [];
  59. unset($value['__empty']);
  60. foreach ($value as $row) {
  61. if (!is_array($row)
  62. || !array_key_exists('country_id', $row)
  63. || !array_key_exists('delivery_term', $row)
  64. ) {
  65. continue;
  66. }
  67. $countryId = $row['country_id'];
  68. $deliveryTerm = $row['delivery_term'];
  69. $result[$countryId] = $deliveryTerm;
  70. }
  71. return $result;
  72. }
  73. /**
  74. * Encode value to be used in @see AbstractFieldArray
  75. *
  76. * @param array $value
  77. * @return array
  78. */
  79. private function encodeArrayFieldValue(array $value)
  80. {
  81. $result = [];
  82. foreach ($value as $countryId => $deliveryTerm) {
  83. $resultId = $this->mathRandom->getUniqueHash('_');
  84. $result[$resultId] = ['country_id' => $countryId, 'delivery_term' => $deliveryTerm];
  85. }
  86. return $result;
  87. }
  88. /**
  89. * Check whether value is in form retrieved by @see encodeArrayFieldValue
  90. *
  91. * @param string|array $value
  92. * @return bool
  93. */
  94. private function isEncodedArrayFieldValue($value)
  95. {
  96. if (!is_array($value)) {
  97. return false;
  98. }
  99. unset($value['__empty']);
  100. foreach ($value as $row) {
  101. if (!is_array($row)
  102. || !array_key_exists('country_id', $row)
  103. || !array_key_exists('delivery_term', $row)
  104. ) {
  105. return false;
  106. }
  107. }
  108. return true;
  109. }
  110. /**
  111. * Generate a storable representation of a value
  112. *
  113. * @param array $value
  114. * @return string
  115. */
  116. private function serializeValue($value)
  117. {
  118. if (is_array($value)) {
  119. $data = [];
  120. foreach ($value as $countryId => $deliveryTerm) {
  121. if (!array_key_exists($countryId, $data)) {
  122. $data[$countryId] = $deliveryTerm;
  123. }
  124. }
  125. return json_encode($data, true);
  126. }
  127. return '';
  128. }
  129. /**
  130. * Create a value from a storable representation
  131. *
  132. * @param string|null $value
  133. * @return array
  134. */
  135. public function unserializeValue($value)
  136. {
  137. if (is_string($value) && !empty($value)) {
  138. return json_decode($value, true);
  139. }
  140. return [];
  141. }
  142. }