File: //var/www/elite/coordParser/JsonHelper.php
<?php
namespace coordParser;
class JsonHelper
{
private string $jsMapFilename = __DIR__ . '/../public/commimMap.json';
private array $fields;
private array $table;
public function setFields(array $fields): self
{
$this->fields = $fields;
return $this;
}
public function setTable(array $table): self
{
$this->table = $table;
return $this;
}
/** Создания кеш файла JSON и файла для карты */
public function createJson(): self
{
$json = [];
foreach ($this->table as $rowNum => $row) {
foreach ($row as $colNum => $cell) {
$fieldName = $this->fields[$colNum];
switch ($fieldName) {
case 'objArea':
$cell = empty($cell) ? null : floatval(str_replace(',', '.', $cell));
break;
case 'choice':
$cell = 'да' === mb_strtolower($cell);
break;
case 'longitude':
case 'latitude':
$cell = empty($cell) ? null : floatval($cell);
break;
case 'images':
$images = array_filter(explode(PHP_EOL, $cell));
$cell = [];
foreach ($images as $image) {
$cell[] = ['src' => $image];
}
break;
default:
break;
}
$json[$rowNum][$fieldName] = $cell;
}
}
$json = json_encode(array_values($json), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
file_put_contents($this->jsMapFilename, $json);
return $this;
}
}