Spamworldpro Mini Shell
Spamworldpro


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/Paq/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Soon/DataSync/Model/Data/Type/Paq/DocReader.php
<?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\Paq;

use Magento\Framework\Filter\FilterManager;
use Magento\Framework\Setup\SampleData\FixtureManager;
use Symfony\Component\DomCrawler\Crawler;

class DocReader
{
    const TABLE_FLAG = '__TABLE__';
    const TYPE_FLAG = '__TYPE__';
    /**
     * @var array
     */
    private $excludedTableCss = ['frment', 'body'];
    /**
     * @var array
     */
    private $excludedHeadersTags = ['script'];
    /**
     * @var array
     */
    private $typeToFileMapping = [
        'dm' => [
            'file' => 'odd',
            'line' => 'dm'
        ],
        'cd' => [
            'file' => 'det'
        ],
        'cm' => [
            'file' => 'cma'
        ],
        'cp' => [
            'line' => 'cp'
        ],
        'ma' => [
            'file' => 'mat'
        ],
    ];
    private $subLinesColIndex = [
        'bv' => 4,
        'cd' => 0
    ];
    /**
     * @var array
     */
    private $parsingHeaders = [];
    /**
     * @var string
     */
    private $exp;
    /**
     * @var int
     */
    private $version;
    /**
     * @var FixtureManager
     */
    private $fixtureManager;
    /**
     * @var Crawler
     */
    private $doc;
    /**
     * @var FilterManager
     */
    private $filterManager;
    /**
     * @var array
     */
    private $headers;
    /**
     * @var string
     */
    private $currentPath;
    /**
     * @var string
     */
    private $type;
    /**
     * @var Crawler
     */
    private $parsedTable;

    public function __construct(
        FixtureManager $fixtureManager,
        FilterManager $filterManager,
        $exp = '',
        $version = 0
    )
    {
        $this->exp = $exp;
        $this->version = $version;
        $this->fixtureManager = $fixtureManager;
        $this->filterManager = $filterManager;

        $this->loadFile();
    }

    /**
     * @param string $type
     * @return bool|mixed
     */
    public function subLineColIndex($type)
    {
        return (isset($this->subLinesColIndex[strtolower($type)]))
            ? $this->subLinesColIndex[strtolower($type)]
            : false;
    }

    private function loadFile()
    {
        $filepath = 'Soon_DataSync::Model/Data/Type/Paq/doc/exp' . strtolower($this->exp()) . $this->version . '.htm';
        $this->doc = new Crawler(file_get_contents($this->fixtureManager->getFixture($filepath)));
    }

    /**
     * @return string
     */
    private function exp()
    {
        return (isset($this->typeToFileMapping[strtolower($this->exp)]['file']))
            ? $this->typeToFileMapping[strtolower($this->exp)]['file']
            : $this->exp;
    }

    /**
     * @param String $type
     * @return array
     */
    public function headers(String $type)
    {
        $this->type = $this->type($type);

        if (!isset($this->headers[$this->type])) {
            $this->parseHeaders($this->doc);
            $this->headers[$this->type] = $this->findHeaders();
        }

        return $this->headers[$this->type];
    }

    /**
     * @param string $type
     * @return string
     */
    private function type($type)
    {
        return (isset($this->typeToFileMapping[strtolower($this->exp)]['line']))
            ? $this->typeToFileMapping[strtolower($this->exp)]['line']
            : $type;
    }

    /**
     * @param Crawler $el
     * @return DocReader
     */
    private function parseHeaders(Crawler $el)
    {
        if ($el->getNode(0)->tagName == 'table') {
            $this->parsingNewTable($el);
        }

        if ($el->children()->count()) {
            return $this->parseChildren($el);
        }

        $this->parseType($el);
        $this->populateHeaders($el);

        return $this;
    }

    /**
     * @param Crawler $el
     */
    private function parsingNewTable(Crawler $el)
    {
        $node = $el->getNode(0);
        $this->parsedTable = null;
        if (!in_array($node->getAttribute('class'), $this->excludedTableCss)) {
            $this->parsedTable = $el;
            $this->currentPath = '';
            $this->parsingHeaders[] = self::TABLE_FLAG;
        }
    }

    /**
     * @param Crawler $el
     * @return DocReader
     */
    private function parseChildren(Crawler $el): DocReader
    {
        $el->children()->each(function ($child) {
            /** @var Crawler $child */
            $this->currentPath = $this->currentPath . '/' . $child->getNode(0)->tagName;

            return $this->parseHeaders($child);
        });

        return $this;
    }

    /**
     * @param Crawler $el
     */
    private function parseType(Crawler $el)
    {
        $allowedPatterns = [
            sprintf('"%s"', $this->type),
            sprintf('="%s"', $this->type),
            sprintf('= "%s"', $this->type),
        ];

        if (
            in_array($el->text(), $allowedPatterns)
            && !array_search(self::TYPE_FLAG . $this->type, $this->parsingHeaders)
        ) {

            $this->parsingHeaders[] = self::TYPE_FLAG . $this->type;
        }
    }

    /**
     * @param Crawler $el
     */
    private function populateHeaders(Crawler $el)
    {
        if (
            $this->isFirstTdInTr()
            && trim($el->text()) !== ''
            && !in_array($el->getNode(0)->tagName, $this->excludedHeadersTags)
            && strpos($this->currentPath, 'td') !== false
            && $this->parsedTable
        ) {
            $this->parsingHeaders[] = trim($el->text());
        }
    }

    /**
     * @return bool
     */
    private function isFirstTdInTr()
    {
        $pathArr = explode('tr', $this->currentPath);
        $tdCount = 0;
        array_map(function ($part) use (&$tdCount) {
            $tdCount = ($part == 'td') ? $tdCount + 1 : $tdCount;
        }, explode('/', end($pathArr)));

        return ($tdCount == 1);
    }

    /**
     * @return array
     */
    private function findHeaders()
    {
        $headers = [];
        $populateNextTable = true;
        $populate = true;
        foreach ($this->parsingHeaders as $k => $header) {
            if ($header == self::TABLE_FLAG && isset($this->parsingHeaders[$k + 1])) {
                if ($populateNextTable == false) {
                    $populate = false;
                } else {
                    $headers = [];
                }
                continue;
            }
            if ($header == self::TYPE_FLAG . $this->type) {
                $populateNextTable = false;
                continue;
            }
            if ($populate) {
                $headers[] = $this->filterManager->translitUrl($header);
            }
        }

        return $headers;
    }
}

Spamworldpro Mini