cache = new BrowscapCache($cache); $this->formatter = new Formatter\PhpGetBrowser(); } /** * Set theformatter instance to use for the getBrowser() result * * @param \BrowscapPHP\Formatter\FormatterInterface $formatter */ public function setFormatter(Formatter\FormatterInterface $formatter) : void { $this->formatter = $formatter; } /** * Sets the parser instance to use * * @param \BrowscapPHP\Parser\ParserInterface $parser */ public function setParser(ParserInterface $parser) : void { $this->parser = $parser; } /** * returns an instance of the used parser class * * @return \BrowscapPHP\Parser\ParserInterface */ public function getParser() : ParserInterface { if (null === $this->parser) { $patternHelper = new Parser\Helper\GetPattern($this->cache); $dataHelper = new Parser\Helper\GetData($this->cache, new Quoter()); $this->parser = new Parser\Ini($patternHelper, $dataHelper, $this->formatter); } return $this->parser; } /** * parses the given user agent to get the information about the browser * * if no user agent is given, it uses {@see \BrowscapPHP\Helper\Support} to get it * * @param string $userAgent the user agent string * * @throws \BrowscapPHP\Exception * * @return \stdClass the object containing the browsers details. */ public function getBrowser(string $userAgent = null) : \stdClass { if (null === $this->cache->getVersion()) { // there is no active/warm cache available throw new Exception('there is no active cache available'); } // Automatically detect the useragent if (! is_string($userAgent)) { $support = new Helper\Support($_SERVER); $userAgent = $support->getUserAgent(); } try { // try to get browser data $formatter = $this->getParser()->getBrowser($userAgent); } catch (\UnexpectedValueException $e) { $formatter = null; } // if return is still NULL, updates are disabled... in this // case we return an empty formatter instance if (null === $formatter) { $formatter = $this->formatter; } return $formatter->getData(); } }