<commit_before><?php
namespace UserAgentParser\Provider;

use UserAgentParser\Exception;
use UserAgentParser\Model;
use WhichBrowser\Parser as WhichBrowserParser;

class WhichBrowser extends AbstractProvider
{
    /**
     * Used for unitTests mocking
     * @var WhichBrowserParser
     */
    private $parser;

    public function getName()
    {
        return 'WhichBrowser';
    }

    public function getComposerPackageName()
    {
        return 'whichbrowser/parser';
    }

    /**
     * @param  array              $headers
     * @return WhichBrowserParser
     */
    public function getParser(array $headers)
    {
        if ($this->parser !== null) {
            return $this->parser;
        }

        return new WhichBrowserParser($headers);
    }

    public function parse($userAgent, array $headers = [])
    {
        $headers['User-Agent'] = $userAgent;

        $parser = $this->getParser($headers);

        /*
         * No result found?
         */
        if ($parser->isDetected() !== true) {
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
        }

        /*
         * Hydrate the model
         */
        $result = new Model\UserAgent();
        $result->setProviderResultRaw($parser->toArray());

        /*
         * Bot detection
         */
        if ($parser->isType('bot') === true) {
            $bot = $result->getBot();
            $bot->setIsBot(true);

            $name = $parser->browser->getName();
            if ($name !== '') {
                $bot->setName($name);
            }

            return $result;
        }

        /*
         * Browser
         */
        $browser = $result->getBrowser();

        $name = $parser->browser->getName();
        if ($name !== '') {
            $browser->setName($name);
        }

        $version = $parser->browser->getVersion();
        if ($version !== '') {
            $browser->getVersion()->setComplete($version);
        }

        /*
         * renderingEngine
         */
        $renderingEngine = $result->getRenderingEngine();

        $name = $parser->engine->getName();
        if ($name !== '') {
            $renderingEngine->setName($name);
        }

        $version = $parser->engine->getVersion();
        if ($version !== '') {
            $renderingEngine->getVersion()->setComplete($version);
        }

        /*
         * operatingSystem
         */
        $operatingSystem = $result->getOperatingSystem();

        $name = $parser->os->getName();
        if ($name !== '') {
            $operatingSystem->setName($name);
        }

        $version = $parser->os->getVersion();
        if ($version !== '') {
            $operatingSystem->getVersion()->setComplete($version);
        }

        /*
         * device
         */
        $device = $result->getDevice();

        $model = $parser->device->getModel();
        if ($model !== '') {
            $device->setModel($model);
        }

        $brand = $parser->device->getManufacturer();
        if ($brand !== '') {
            $device->setBrand($brand);
        }

        $device->setType($parser->device->type);

        if ($parser->isType('mobile', 'tablet', 'ereader', 'media', 'watch', 'camera') === true) {
            $device->setIsMobile(true);
        }
<commit_msg>Use device subtype for determining if a gaming device is portable<commit_after><?php
namespace UserAgentParser\Provider;

use UserAgentParser\Exception;
use UserAgentParser\Model;
use WhichBrowser\Parser as WhichBrowserParser;

class WhichBrowser extends AbstractProvider
{
    /**
     * Used for unitTests mocking
     * @var WhichBrowserParser
     */
    private $parser;

    public function getName()
    {
        return 'WhichBrowser';
    }

    public function getComposerPackageName()
    {
        return 'whichbrowser/parser';
    }

    /**
     * @param  array              $headers
     * @return WhichBrowserParser
     */
    public function getParser(array $headers)
    {
        if ($this->parser !== null) {
            return $this->parser;
        }

        return new WhichBrowserParser($headers);
    }

    public function parse($userAgent, array $headers = [])
    {
        $headers['User-Agent'] = $userAgent;

        $parser = $this->getParser($headers);

        /*
         * No result found?
         */
        if ($parser->isDetected() !== true) {
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
        }

        /*
         * Hydrate the model
         */
        $result = new Model\UserAgent();
        $result->setProviderResultRaw($parser->toArray());

        /*
         * Bot detection
         */
        if ($parser->isType('bot') === true) {
            $bot = $result->getBot();
            $bot->setIsBot(true);

            $name = $parser->browser->getName();
            if ($name !== '') {
                $bot->setName($name);
            }

            return $result;
        }

        /*
         * Browser
         */
        $browser = $result->getBrowser();

        $name = $parser->browser->getName();
        if ($name !== '') {
            $browser->setName($name);
        }

        $version = $parser->browser->getVersion();
        if ($version !== '') {
            $browser->getVersion()->setComplete($version);
        }

        /*
         * renderingEngine
         */
        $renderingEngine = $result->getRenderingEngine();

        $name = $parser->engine->getName();
        if ($name !== '') {
            $renderingEngine->setName($name);
        }

        $version = $parser->engine->getVersion();
        if ($version !== '') {
            $renderingEngine->getVersion()->setComplete($version);
        }

        /*
         * operatingSystem
         */
        $operatingSystem = $result->getOperatingSystem();

        $name = $parser->os->getName();
        if ($name !== '') {
            $operatingSystem->setName($name);
        }

        $version = $parser->os->getVersion();
        if ($version !== '') {
            $operatingSystem->getVersion()->setComplete($version);
        }

        /*
         * device
         */
        $device = $result->getDevice();

        $model = $parser->device->getModel();
        if ($model !== '') {
            $device->setModel($model);
        }

        $brand = $parser->device->getManufacturer();
        if ($brand !== '') {
            $device->setBrand($brand);
        }

        $device->setType($parser->device->type);

        if ($parser->isType('mobile', 'tablet', 'ereader', 'media', 'watch', 'camera', 'gaming:portable') === true) {
            $device->setIsMobile(true);
        }

{'fraction_non_alphanumeric': 0.16, 'fraction_numerical': 0.0, 'mean_word_length': 1.7430668841761827, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 99, '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': '1855816', 'n_tokens_mistral': 1823, 'n_tokens_neox': 1765, 'n_words': 327}