CountryCreditCard.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Model\Adminhtml\System\Config;
  7. use Magento\Framework\App\Cache\TypeListInterface;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Framework\App\Config\Value;
  10. use Magento\Framework\Data\Collection\AbstractDb;
  11. use Magento\Framework\Math\Random;
  12. use Magento\Framework\Model\Context;
  13. use Magento\Framework\Model\ResourceModel\AbstractResource;
  14. use Magento\Framework\Registry;
  15. use Magento\Framework\Serialize\Serializer\Json;
  16. /**
  17. * Class CountryCreditCard
  18. */
  19. class CountryCreditCard extends Value
  20. {
  21. /**
  22. * @var \Magento\Framework\Math\Random
  23. */
  24. protected $mathRandom;
  25. /**
  26. * @var \Magento\Framework\Serialize\Serializer\Json
  27. */
  28. private $serializer;
  29. /**
  30. * @param \Magento\Framework\Model\Context $context
  31. * @param \Magento\Framework\Registry $registry
  32. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  33. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  34. * @param \Magento\Framework\Math\Random $mathRandom
  35. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  36. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  37. * @param array $data
  38. * @param \Magento\Framework\Serialize\Serializer\Json $serializer
  39. */
  40. public function __construct(
  41. Context $context,
  42. Registry $registry,
  43. ScopeConfigInterface $config,
  44. TypeListInterface $cacheTypeList,
  45. Random $mathRandom,
  46. AbstractResource $resource = null,
  47. AbstractDb $resourceCollection = null,
  48. array $data = [],
  49. Json $serializer = null
  50. ) {
  51. $this->mathRandom = $mathRandom;
  52. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
  53. ->get(Json::class);
  54. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  55. }
  56. /**
  57. * Prepare data before save
  58. *
  59. * @return $this
  60. */
  61. public function beforeSave()
  62. {
  63. $value = $this->getValue();
  64. if (!is_array($value)) {
  65. try {
  66. $value = $this->serializer->unserialize($value);
  67. } catch (\InvalidArgumentException $e) {
  68. $value = [];
  69. }
  70. }
  71. $result = [];
  72. foreach ($value as $data) {
  73. if (empty($data['country_id']) || empty($data['cc_types'])) {
  74. continue;
  75. }
  76. $country = $data['country_id'];
  77. if (array_key_exists($country, $result)) {
  78. $result[$country] = $this->appendUniqueCountries($result[$country], $data['cc_types']);
  79. } else {
  80. $result[$country] = $data['cc_types'];
  81. }
  82. }
  83. $this->setValue($this->serializer->serialize($result));
  84. return $this;
  85. }
  86. /**
  87. * Process data after load
  88. *
  89. * @return $this
  90. */
  91. public function afterLoad()
  92. {
  93. if ($this->getValue()) {
  94. $value = $this->serializer->unserialize($this->getValue());
  95. if (is_array($value)) {
  96. $this->setValue($this->encodeArrayFieldValue($value));
  97. }
  98. }
  99. return $this;
  100. }
  101. /**
  102. * Encode value to be used in \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
  103. *
  104. * @param array $value
  105. * @return array
  106. */
  107. protected function encodeArrayFieldValue(array $value)
  108. {
  109. $result = [];
  110. foreach ($value as $country => $creditCardType) {
  111. $id = $this->mathRandom->getUniqueHash('_');
  112. $result[$id] = ['country_id' => $country, 'cc_types' => $creditCardType];
  113. }
  114. return $result;
  115. }
  116. /**
  117. * Append unique countries to list of exists and reindex keys
  118. *
  119. * @param array $countriesList
  120. * @param array $inputCountriesList
  121. * @return array
  122. */
  123. private function appendUniqueCountries(array $countriesList, array $inputCountriesList)
  124. {
  125. $result = array_merge($countriesList, $inputCountriesList);
  126. return array_values(array_unique($result));
  127. }
  128. }