![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Soon/DataSync/Model/Data/Type/ |
<?php /** * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * @author Hervé Guétin <[email protected]> <@herveguetin> * @copyright Copyright (c) 2017 Agence Soon (http://www.agence-soon.fr) */ namespace Soon\DataSync\Model\Data\Type; use Magento\Framework\ObjectManagerInterface; use Soon\DataSync\Model\Data\Type\Paq\DocReader; use Soon\DataSync\Model\Data\Type\Paq\DocReaderFactory; class Paq extends DataTypeAbstract { const PAQ_DELIMITER = '__PAQ__'; /** * @var CsvFactory */ private $csvFactory; /** * @var Csv */ private $csv; /** * @var ObjectManagerInterface */ private $objectManager; /** * @var DocReaderFactory */ private $docReaderFactory; public function __construct( CsvFactory $csvFactory, ObjectManagerInterface $objectManager, DocReaderFactory $docReaderFactory ) { $this->csvFactory = $csvFactory; $this->objectManager = $objectManager; $this->docReaderFactory = $docReaderFactory; $this->initCsv(); } private function initCsv() { $this->csv = $this->csvFactory->create(); $this->csv->withHeader = false; $this->csv->padHeaders = false; $this->csv->textDelimiter = ''; $this->csv->fieldDelimiter = "\t"; } /** * The passed string is parsed to an array * * @param string $string * @return array */ public function parse(string $string): array { $srcData = $this->csv->parse($string); /** @var DocReader $docReader */ $header = array_shift($srcData); $docReader = $this->docReaderFactory->create(['exp' => $this->paqFile(), 'version' => $this->version($header)]); $data = array_map(function ($row) use ($docReader) { $subLineColIndex = ($docReader->subLineColIndex(reset($row))); $type = ($subLineColIndex) ? $row[$subLineColIndex] : reset($row); $rowHeaders = $docReader->headers($type); return array_combine($rowHeaders, $this->rowForCombine($row, $rowHeaders)); }, $srcData); return $data; } /** * @return string */ private function paqFile() { $file = $this->job->which('transferType')->filepath(); $fileArr = explode('/', $file); return substr(end($fileArr), 0, 2); } /** * @param array $header * @return string */ private function version($header) { $header = reset($header); preg_match('/\d*\./', $header, $header); $version = reset($header); $version = rtrim($version, '.'); $version = ltrim($version, '0'); return ($version == '') ? 0 : $version; } /** * @param array $row * @param array $rowHeaders * @return array */ private function rowForCombine($row, $rowHeaders) { $diff = count($row) - count($rowHeaders); if ($diff > 0) { array_splice($row, count($row) - 1, $diff); } if ($diff < 0) { for ($i = 1; $i <= $diff * -1; $i++) { $row[] = ''; } } return $row; } /** * The passed array is converted to a string * * @param array $array * @return string */ public function convert(array $array): string { return $this->csv->convert($array); } }