init
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Customer\Helper\Deepblue;
|
||||
|
||||
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) : string
|
||||
{
|
||||
// return mb_convert_encoding($str, 'UTF-8', ['gb2312', 'gbk']);
|
||||
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[3], $row[4]);
|
||||
// $row[2] = $this->chineseEncoding($row[2]);
|
||||
|
||||
return [$row[0], $row[1], $this->getCarModelId($row[2]), $region[0], $region[1]];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function getRegionId(string $province, string $city)
|
||||
{
|
||||
$city = str_replace(' ', '', $city);
|
||||
// $city = $this->chineseEncoding($city);
|
||||
// $province = $this->chineseEncoding($province);
|
||||
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/pc-code.json');
|
||||
foreach ($regions as $item) {
|
||||
if (mb_strpos($item['name'], $province) === 0 ) {
|
||||
foreach ($item['children'] as $child) {
|
||||
if (mb_strpos($child['name'], $city) === 0 ) {
|
||||
return $this->regions[$city] = [
|
||||
str_pad($item['code'], 6, '0'),
|
||||
str_pad($child['code'], 6, '0')
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $series
|
||||
* @return string | null
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function getCarSeriesCode(string $series): ?string
|
||||
{
|
||||
$series = str_replace(' ', '', $series);
|
||||
// $series = $this->chineseEncoding($series);
|
||||
// var_dump($series);
|
||||
// echo '<br/><br/><br/>';
|
||||
$data = $this->loadDataFromFile('/data/car-series.json');
|
||||
// var_dump($data[$series]);
|
||||
// echo '<br/><br/><br/>';
|
||||
return $data[$series] ?? null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user