Team Ai
Apppublic

errorcode418/php

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
AbstractParser.php141 linesDownload Raw Back to src
1<?php2/**3 * ua-parser4 *5 * Copyright (c) 2011-2013 Dave Olsen, http://dmolsen.com6 * Copyright (c) 2013-2014 Lars Strojny, http://usrportage.de7 *8 * Released under the MIT license9 */10namespace UAParser;11 12use UAParser\Exception\FileNotFoundException;13 14abstract class AbstractParser15{16    /** @var string */17    public static $defaultFile;18 19    /** @var array */20    protected $regexes = array();21 22    public function __construct(array $regexes)23    {24        $this->regexes = $regexes;25    }26 27    /**28     * Create parser instance29     *30     * Either pass a custom regexes.php file or leave the argument empty and use the default file.31     *32     * @param string $file33     * @throws FileNotFoundException34     * @return static35     */36    public static function create($file = null)37    {38        return $file ? static::createCustom($file) : static::createDefault();39    }40 41    /**42     * @return static43     * @throws FileNotFoundException44     */45    protected static function createDefault()46    {47        return static::createInstance(48            static::getDefaultFile(),49            array('UAParser\Exception\FileNotFoundException', 'defaultFileNotFound')50        );51    }52 53    /**54     * @return static55     * @throws FileNotFoundException56     */57    protected static function createCustom($file)58    {59        return static::createInstance(60            $file,61            array('UAParser\Exception\FileNotFoundException', 'customRegexFileNotFound')62        );63    }64 65    private static function createInstance($file, $exceptionFactory)66    {67        if (!file_exists($file)) {68            throw call_user_func($exceptionFactory, $file);69        }70 71        return new static(include $file);72    }73 74    /**75     * @param array $regexes76     * @param string $userAgent77     * @return array78     */79    protected static function tryMatch(array $regexes, $userAgent)80    {81        foreach ($regexes as $regex) {82            $flag = isset($regex['regex_flag']) ? $regex['regex_flag'] : '';83            if (preg_match('@' . $regex['regex'] . '@' . $flag, $userAgent, $matches)) {84 85                $defaults = array(86                    1 => 'Other',87                    2 => null,88                    3 => null,89                    4 => null,90                    5 => null,91                );92 93                return array($regex, $matches + $defaults);94            }95        }96 97        return array(null, null);98    }99 100    /**101     * @param array $regex102     * @param string $key103     * @param string $default104     * @param array $matches105     * @return string|null106     */107    protected static function multiReplace(array $regex, $key, $default, array $matches)108    {109        if (!isset($regex[$key])) {110            return self::emptyStringToNull($default);111        }112 113        $replacement = preg_replace_callback(114            '|\$(?P<key>\d)|',115            function ($m) use ($matches) {116                return isset($matches[$m['key']]) ? $matches[$m['key']] : '';117            },118            $regex[$key]119        );120 121        return self::emptyStringToNull($replacement);122    }123 124    private static function emptyStringToNull($string)125    {126        $string = trim($string);127 128        return $string === '' ? null : $string;129    }130 131    /**132     * @return string133     */134    protected static function getDefaultFile()135    {136        return static::$defaultFile137            ? static::$defaultFile138            : realpath(__DIR__ . '/../resources') . DIRECTORY_SEPARATOR . 'regexes.php';139    }140}141