107 lines
3.1 KiB
PHP
107 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Stemcell\Customer\Helper\Avatr;
|
|
|
|
use Magento\Framework\Exception\LocalizedException;
|
|
use Magento\Framework\Module\Dir as ModuleDir;
|
|
use Stemcell\Customer\Helper\TransformersInterface;
|
|
|
|
/**
|
|
* transform original province and city name to Avatr require id
|
|
*/
|
|
class Transformers implements TransformersInterface
|
|
{
|
|
private ModuleDir $moduleDir;
|
|
|
|
protected array $data = [];
|
|
|
|
protected array $regions = [];
|
|
public function __construct(
|
|
ModuleDir $moduleDir
|
|
) {
|
|
$this->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
|
|
{
|
|
$region = $this->getRegionId($row[4]);
|
|
return [$this->chineseEncoding($row[0]), $row[1], $this->getCarModelId($row[2]), $region[0], $region[1]];
|
|
}
|
|
|
|
/**
|
|
* @throws LocalizedException
|
|
*/
|
|
public function getRegionId(string $city)
|
|
{
|
|
$city = str_replace(' ', '', $city);
|
|
$city = $this->chineseEncoding($city);
|
|
if (isset($this->regions[$city])) {
|
|
return $this->regions[$city];
|
|
}
|
|
|
|
|
|
if (mb_strlen($city) < 2) {
|
|
throw new LocalizedException(__("invalid city name: ".$city));
|
|
}
|
|
|
|
$regions = $this->loadDataFromFile('/data/regions.json');
|
|
if (isset($regions[$city]) && is_array($regions[$city])) {
|
|
return $this->regions[$city] = [$regions[$city]['province_id'], $regions[$city]['city_id']];
|
|
}
|
|
|
|
foreach ($regions as $cityName => $item) {
|
|
if (mb_strpos($cityName, $city) === 0 ) {
|
|
return $this->regions[$city] = [$item['province_id'], $item['city_id']];
|
|
}
|
|
}
|
|
|
|
return [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;
|
|
}
|
|
}
|