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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Braintree\Model\Adminhtml\System\Config;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Value;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Math\Random;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Registry;
use Magento\Framework\Serialize\Serializer\Json;
/**
* Class CountryCreditCard
*/
class CountryCreditCard extends Value
{
/**
* @var \Magento\Framework\Math\Random
*/
protected $mathRandom;
/**
* @var \Magento\Framework\Serialize\Serializer\Json
*/
private $serializer;
/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
* @param \Magento\Framework\Math\Random $mathRandom
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
* @param \Magento\Framework\Serialize\Serializer\Json $serializer
*/
public function __construct(
Context $context,
Registry $registry,
ScopeConfigInterface $config,
TypeListInterface $cacheTypeList,
Random $mathRandom,
AbstractResource $resource = null,
AbstractDb $resourceCollection = null,
array $data = [],
Json $serializer = null
) {
$this->mathRandom = $mathRandom;
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(Json::class);
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
}
/**
* Prepare data before save
*
* @return $this
*/
public function beforeSave()
{
$value = $this->getValue();
if (!is_array($value)) {
try {
$value = $this->serializer->unserialize($value);
} catch (\InvalidArgumentException $e) {
$value = [];
}
}
$result = [];
foreach ($value as $data) {
if (empty($data['country_id']) || empty($data['cc_types'])) {
continue;
}
$country = $data['country_id'];
if (array_key_exists($country, $result)) {
$result[$country] = $this->appendUniqueCountries($result[$country], $data['cc_types']);
} else {
$result[$country] = $data['cc_types'];
}
}
$this->setValue($this->serializer->serialize($result));
return $this;
}
/**
* Process data after load
*
* @return $this
*/
public function afterLoad()
{
if ($this->getValue()) {
$value = $this->serializer->unserialize($this->getValue());
if (is_array($value)) {
$this->setValue($this->encodeArrayFieldValue($value));
}
}
return $this;
}
/**
* Encode value to be used in \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
*
* @param array $value
* @return array
*/
protected function encodeArrayFieldValue(array $value)
{
$result = [];
foreach ($value as $country => $creditCardType) {
$id = $this->mathRandom->getUniqueHash('_');
$result[$id] = ['country_id' => $country, 'cc_types' => $creditCardType];
}
return $result;
}
/**
* Append unique countries to list of exists and reindex keys
*
* @param array $countriesList
* @param array $inputCountriesList
* @return array
*/
private function appendUniqueCountries(array $countriesList, array $inputCountriesList)
{
$result = array_merge($countriesList, $inputCountriesList);
return array_values(array_unique($result));
}
}