<commit_before><?php

namespace Browscap\Generator;

use Psr\Log\LoggerInterface;

/**
 * Class CollectionParser
 *
 * @package Browscap\Generator
 */
class CollectionParser
{
    /**
     * @var \Browscap\Generator\DataCollection
     */
    private $collection;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    private $logger = null;

    /**
     * Set the data collection
     *
     * @param \Browscap\Generator\DataCollection $collection
     * @return \Browscap\Generator\CollectionParser
     */
    public function setDataCollection(DataCollection $collection)
    {
        $this->collection = $collection;
        return $this;
    }

    /**
     * Get the data collection
     *
     * @throws \LogicException
     * @return \Browscap\Generator\DataCollection
     */
    public function getDataCollection()
    {
        if (!isset($this->collection)) {
            throw new \LogicException('Data collection has not been set yet - call setDataCollection');
        }

        return $this->collection;
    }

    /**
     * @param \Psr\Log\LoggerInterface $logger
     *
     * @return \Browscap\Generator\CollectionParser
     */
    public function setLogger(LoggerInterface $logger)
    {
        $this->logger = $logger;

        return $this;
    }

    /**
     * @return \Psr\Log\LoggerInterface $logger
     */
    public function getLogger()
    {
        return $this->logger;
    }

    /**
     * Generate and return the formatted browscap data
     *
     * @return array
     * @throws \LogicException
     */
    public function parse()
    {
        $allDivisions = array();

        foreach ($this->getDataCollection()->getDivisions() as $division) {
            $this->getLogger()->debug('parse a data collection into an array for division "' . $division['userAgents'][0]['userAgent'] . '"');

            if ($division['division'] == 'Browscap Version') {
                continue;
            }

            if (isset($division['lite'])) {
                $lite = $division['lite'];
            } else {
                $lite = false;
            }

            $sortIndex = $division['sortIndex'];

            if (isset($division['versions']) && is_array($division['versions'])) {
                foreach ($division['versions'] as $version) {
                    $dots = explode('.', $version, 2);

                    $majorVer = $dots[0];
                    $minorVer = (isset($dots[1]) ? $dots[1] : 0);

                    $userAgents = json_encode($division['userAgents']);
                    $userAgents = str_replace(
                        array('#MAJORVER#', '#MINORVER#'),
                        array($majorVer, $minorVer),
                        $userAgents
                    );

                    $divisionName = str_replace(
                        array('#MAJORVER#', '#MINORVER#'),
                        array($majorVer, $minorVer),
                        $division['division']
                    );

                    $userAgents = json_decode($userAgents, true);

                    $allDivisions += $this->parseDivision(
                        $userAgents,
                        $majorVer,
                        $minorVer,
                        $lite,
                        $sortIndex,
                        $divisionName
                    );
                }
            } else {
                $allDivisions += $this->parseDivision(
                    $division['userAgents'],
                    0,
                    0,
                    $lite,
                    $sortIndex,
                    $division['division']
                );
            }
        }

        // full expand of all data
        $allDivisions = $this->expandProperties($allDivisions);

        return $allDivisions;
    }

    /**
     * Render a single division
     *
     * @param array   $userAgents
     * @param string  $majorVer
     * @param string  $minorVer
     * @param boolean $lite
     * @param integer $sortIndex
     * @param string  $divisionName
     *
     * @return array
     */
    private function parseDivision(array $userAgents, $majorVer, $minorVer, $lite, $sortIndex, $divisionName)
    {
        $output = array();

        foreach ($userAgents as $uaData) {
            $output += $this->parseUserAgent($uaData, $majorVer, $minorVer, $lite, $sortIndex, $divisionName);
        }

        return $output;
    }

    /**
     * Render a single User Agent block
     *
     * @param array    $uaData
     * @param string   $majorVer
     * @param string   $minorVer
     * @param boolean  $lite
     * @param integer  $sortIndex
     * @param string   $divisionName
     *
     * @return array
     */
    private function parseUserAgent(array $uaData, $majorVer, $minorVer, $lite, $sortIndex, $divisionName)
    {
        if (!isset($uaData['properties']) || !is_array($uaData['properties'])) {
            throw new \LogicException('properties are missing or not an array for key "' . $uaData['userAgent'] . '"');
        }
        
        $output = array(<commit_msg>fixed notice<commit_after><?php

namespace Browscap\Generator;

use Psr\Log\LoggerInterface;

/**
 * Class CollectionParser
 *
 * @package Browscap\Generator
 */
class CollectionParser
{
    /**
     * @var \Browscap\Generator\DataCollection
     */
    private $collection;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    private $logger = null;

    /**
     * Set the data collection
     *
     * @param \Browscap\Generator\DataCollection $collection
     * @return \Browscap\Generator\CollectionParser
     */
    public function setDataCollection(DataCollection $collection)
    {
        $this->collection = $collection;
        return $this;
    }

    /**
     * Get the data collection
     *
     * @throws \LogicException
     * @return \Browscap\Generator\DataCollection
     */
    public function getDataCollection()
    {
        if (!isset($this->collection)) {
            throw new \LogicException('Data collection has not been set yet - call setDataCollection');
        }

        return $this->collection;
    }

    /**
     * @param \Psr\Log\LoggerInterface $logger
     *
     * @return \Browscap\Generator\CollectionParser
     */
    public function setLogger(LoggerInterface $logger)
    {
        $this->logger = $logger;

        return $this;
    }

    /**
     * @return \Psr\Log\LoggerInterface $logger
     */
    public function getLogger()
    {
        return $this->logger;
    }

    /**
     * Generate and return the formatted browscap data
     *
     * @return array
     * @throws \LogicException
     */
    public function parse()
    {
        $allDivisions = array();

        foreach ($this->getDataCollection()->getDivisions() as $division) {
            if (isset($division['userAgents'][0]['userAgent'])) {
                $this->getLogger()->debug(
                    'parse a data collection into an array for division "' . $division['userAgents'][0]['userAgent'] . '"'
                );
            } else {
                $this->getLogger()->debug('parse a data collection into an array');
            }

            if ($division['division'] == 'Browscap Version') {
                continue;
            }

            if (isset($division['lite'])) {
                $lite = $division['lite'];
            } else {
                $lite = false;
            }

            $sortIndex = $division['sortIndex'];

            if (isset($division['versions']) && is_array($division['versions'])) {
                foreach ($division['versions'] as $version) {
                    $dots = explode('.', $version, 2);

                    $majorVer = $dots[0];
                    $minorVer = (isset($dots[1]) ? $dots[1] : 0);

                    $userAgents = json_encode($division['userAgents']);
                    $userAgents = str_replace(
                        array('#MAJORVER#', '#MINORVER#'),
                        array($majorVer, $minorVer),
                        $userAgents
                    );

                    $divisionName = str_replace(
                        array('#MAJORVER#', '#MINORVER#'),
                        array($majorVer, $minorVer),
                        $division['division']
                    );

                    $userAgents = json_decode($userAgents, true);

                    $allDivisions += $this->parseDivision(
                        $userAgents,
                        $majorVer,
                        $minorVer,
                        $lite,
                        $sortIndex,
                        $divisionName
                    );
                }
            } else {
                $allDivisions += $this->parseDivision(
                    $division['userAgents'],
                    0,
                    0,
                    $lite,
                    $sortIndex,
                    $division['division']
                );
            }
        }

        // full expand of all data
        $allDivisions = $this->expandProperties($allDivisions);

        return $allDivisions;
    }

    /**
     * Render a single division
     *
     * @param array   $userAgents
     * @param string  $majorVer
     * @param string  $minorVer
     * @param boolean $lite
     * @param integer $sortIndex
     * @param string  $divisionName
     *
     * @return array
     */
    private function parseDivision(array $userAgents, $majorVer, $minorVer, $lite, $sortIndex, $divisionName)
    {
        $output = array();

        foreach ($userAgents as $uaData) {
            $output += $this->parseUserAgent($uaData, $majorVer, $minorVer, $lite, $sortIndex, $divisionName);
        }

        return $output;
    }

    /**
     * Render a single User Agent block
     *
     * @param array    $uaData
     * @param string   $majorVer
     * @param string   $minorVer
     * @param boolean  $lite
     * @param integer  $sortIndex
     * @param string   $divisionName
     *
     * @return array
     */
    private function parseUserAgent(array $uaData, $majorVer, $minorVer, $lite, $sortIndex, $divisionName)
    {
        if (!isset($uaData['properties']) || !is_array($uaData['properties'])) {
            throw new \LogicException('properties are missing or not an array for key "' . $uaData['userAgent'] . '"');
        }

        $output = array(
{'fraction_non_alphanumeric': 0.1240123337830025, 'fraction_numerical': 0.0016380805550202352, 'mean_word_length': 1.5601874691662556, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '923030', 'n_tokens_mistral': 2904, 'n_tokens_neox': 2695, 'n_words': 591}