5
5
*/
6
6
namespace Magento \CustomerSampleData \Model ;
7
7
8
+ use Magento \Customer \Api \CustomerMetadataInterface ;
8
9
use Magento \Customer \Api \Data \RegionInterface ;
9
10
use Magento \Framework \Serialize \Serializer \Json ;
10
11
use Magento \Framework \Setup \SampleData \Context as SampleDataContext ;
@@ -79,6 +80,11 @@ class Customer
79
80
*/
80
81
private $ serializer ;
81
82
83
+ /**
84
+ * @var CustomerMetadataInterface
85
+ */
86
+ private $ customerMetadata ;
87
+
82
88
/**
83
89
* @param SampleDataContext $sampleDataContext
84
90
* @param \Magento\Directory\Model\CountryFactory $countryFactory
@@ -89,7 +95,8 @@ class Customer
89
95
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
90
96
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
91
97
* @param \Magento\Framework\App\State $appState
92
- * @param Json|null $serializer
98
+ * @param Json $serializer
99
+ * @param CustomerMetadataInterface $customerMetadata
93
100
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
94
101
*/
95
102
public function __construct (
@@ -102,7 +109,8 @@ public function __construct(
102
109
\Magento \Store \Model \StoreManagerInterface $ storeManager ,
103
110
\Magento \Framework \Api \DataObjectHelper $ dataObjectHelper ,
104
111
\Magento \Framework \App \State $ appState ,
105
- Json $ serializer = null
112
+ Json $ serializer ,
113
+ CustomerMetadataInterface $ customerMetadata
106
114
) {
107
115
$ this ->fixtureManager = $ sampleDataContext ->getFixtureManager ();
108
116
$ this ->csvReader = $ sampleDataContext ->getCsvReader ();
@@ -114,7 +122,8 @@ public function __construct(
114
122
$ this ->storeManager = $ storeManager ;
115
123
$ this ->dataObjectHelper = $ dataObjectHelper ;
116
124
$ this ->appState = $ appState ;
117
- $ this ->serializer = $ serializer ?: \Magento \Framework \App \ObjectManager::getInstance ()->get (Json::class);
125
+ $ this ->serializer = $ serializer ;
126
+ $ this ->customerMetadata = $ customerMetadata ;
118
127
}
119
128
120
129
/**
@@ -246,14 +255,25 @@ protected function getDefaultCustomerAddress()
246
255
*/
247
256
protected function convertRowData ($ row , $ data )
248
257
{
258
+ $ genders = [];
259
+ $ genderAttr = $ this ->customerMetadata ->getAttributeMetadata ('gender ' );
260
+ foreach ($ genderAttr ->getOptions () as $ genderOption ) {
261
+ $ genders [$ genderOption ->getValue ()] = $ genderOption ->getLabel ();
262
+ }
263
+
249
264
foreach ($ row as $ field => $ value ) {
250
265
if (isset ($ data [$ field ])) {
251
- if ($ field == 'street ' ) {
252
- $ data [$ field ] = $ this ->serializer ->unserialize ($ value );
266
+ if ($ field === 'password ' ) {
253
267
continue ;
254
268
}
255
- if ($ field == 'password ' ) {
256
- continue ;
269
+
270
+ switch ($ field ) {
271
+ case 'street ' :
272
+ $ value = $ this ->serializer ->unserialize ($ value );
273
+ break ;
274
+ case 'gender ' :
275
+ $ value = array_search ($ value , $ genders );
276
+ break ;
257
277
}
258
278
$ data [$ field ] = $ value ;
259
279
}
0 commit comments