-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathPaymentMethodTitles.php
More file actions
133 lines (112 loc) · 3.52 KB
/
Copy pathPaymentMethodTitles.php
File metadata and controls
133 lines (112 loc) · 3.52 KB
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
<?php
/**
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2026 Adyen N.V. (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <magento@adyen.com>
*/
namespace Adyen\Payment\Model\Config\Backend;
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\SerializerInterface;
class PaymentMethodTitles extends Value
{
/**
* @param Context $context
* @param Registry $registry
* @param ScopeConfigInterface $config
* @param TypeListInterface $cacheTypeList
* @param Random $mathRandom
* @param SerializerInterface $serializer
* @param AbstractResource|null $resource
* @param AbstractDb|null $resourceCollection
* @param array $data
*/
public function __construct(
Context $context,
Registry $registry,
ScopeConfigInterface $config,
TypeListInterface $cacheTypeList,
protected readonly Random $mathRandom,
private readonly SerializerInterface $serializer,
?AbstractResource $resource = null,
?AbstractDb $resourceCollection = null,
array $data = []
) {
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
}
/**
* Serialize the dynamic table rows before saving to the database.
* Duplicate payment method types are deduplicated (last row wins).
*
* @return $this
*/
public function beforeSave(): static
{
$value = $this->getValue();
if (!is_array($value)) {
return $this;
}
$result = [];
foreach ($value as $rowData) {
if (!is_array($rowData)) {
continue;
}
$type = trim((string) ($rowData['payment_method_type'] ?? ''));
$title = trim((string) ($rowData['title'] ?? ''));
if ($type === '' || $title === '') {
continue;
}
$result[$type] = $title;
}
$this->setValue($this->serializer->serialize($result));
return $this;
}
/**
* Deserialize the stored value after loading from the database so it can
* be rendered by the AbstractFieldArray dynamic table.
*
* @return $this
*/
protected function _afterLoad(): static
{
$value = $this->getValue();
if (empty($value)) {
return $this;
}
$decoded = $this->serializer->unserialize($value);
if (!is_array($decoded)) {
return $this;
}
$this->setValue($this->encodeArrayFieldValue($decoded));
return $this;
}
/**
* Convert the stored `{type: title}` map into the row-keyed format that
* AbstractFieldArray expects for pre-population.
*
* @param array $value
* @return array
*/
protected function encodeArrayFieldValue(array $value): array
{
$result = [];
foreach ($value as $type => $title) {
$id = $this->mathRandom->getUniqueHash('_');
$result[$id] = [
'payment_method_type' => $type,
'title' => $title,
];
}
return $result;
}
}