moduleDir = $moduleDir; } /** * @param string $path a relative path to $module, must begin with / * @param string $module * @return array * @throws LocalizedException */ protected function loadDataFromFile(string $path, string $module='Stemcell_Customer') : array { if (isset($this->data[$path]) && !empty($this->data[$path])) { return $this->data[$path]; } $file = $this->moduleDir->getDir($module).$path; if (!file_exists($file)) { throw new LocalizedException(__("$file not exist")); } $data = json_decode(file_get_contents($file), true); if (!$data) { throw new LocalizedException(__("$file content is not json")); } return $this->data[$path] = $data; } protected function chineseEncoding(string $str) { // return mb_convert_encoding($str, 'UTF-8', 'gb2312'); return $str; } /** * @param array $row original row data * @return array data order by ['name', 'telephone', 'favorite_vehicle', 'province_id', 'city_id'] * @throws LocalizedException */ public function transform(array $row): array { return [$this->chineseEncoding($row[0]), $row[1], $this->getCarModelId($row[2]), null, null]; } /** * @throws LocalizedException */ public function getCarModelId(string $carModel) { $carModel = str_replace(' ', '', $carModel); $carModel = $this->chineseEncoding($carModel); $carModels = $this->loadDataFromFile('/data/car-models.json'); return $carModels[$carModel] ?? null; } }