setUserAgent($userAgent); } } /** * Sets the useragent to be parsed * * @param string $userAgent */ public function setUserAgent($userAgent) { if ($this->userAgent != $userAgent) { $this->reset(); } $this->userAgent = $userAgent; } protected function reset() { $this->bot = null; $this->parsed = false; } /** * Sets whether to discard additional bot information * If information is discarded it's only possible check whether UA was detected as bot or not. * (Discarding information speeds up the detection a bit) * * @param bool $discard */ public function discardBotInformation($discard = true) { $this->discardBotInformation = $discard; } /** * Sets whether to skip bot detection. * It is needed if we want bots to be processed as a simple clients. So we can detect if it is mobile client, * or desktop, or enything else. By default all this information is not retrieved for the bots. * * @param bool $skip */ public function skipBotDetection($skip = true) { $this->skipBotDetection = $skip; } /** * Returns if the parsed UA was identified as a Bot * * @see bots.yml for a list of detected bots * * @return bool */ public function isBot() { return !empty($this->bot); } /** * Returns the user agent that is set to be parsed * * @return string */ public function getUserAgent() { return $this->userAgent; } /** * Returns the bot extracted from the parsed UA * * @return array */ public function getBot() { return $this->bot; } /** * Returns true, if userAgent was already parsed with parse() * * @return bool */ public function isParsed() { return $this->parsed; } /** * Triggers the parsing of the current user agent * @throws \Exception */ public function parse() { if ($this->isParsed()) { return; } $this->parsed = true; // skip parsing for empty useragents or those not containing any letter if (empty($this->userAgent) || !preg_match('/([a-z])/i', $this->userAgent)) { return; } $this->parseBot(); if ($this->isBot()) { return; } } /** * Parses the UA for bot information using the Bot parser * @throws \Exception * @return void */ protected function parseBot() { if ($this->skipBotDetection) { $this->bot = false; return; } $botParser = new Bot(); $botParser->setUserAgent($this->getUserAgent()); $botParser->setYamlParser($this->getYamlParser()); if ($this->discardBotInformation) { $botParser->discardDetails(); } $this->bot = $botParser->parse(); } protected function matchUserAgent($regex) { $regex = '/(?:^|[^A-Z_-])(?:' . str_replace('/', '\/', $regex) . ')/i'; if (preg_match($regex, $this->userAgent, $matches)) { return $matches; } return false; } /** * Sets the Yaml Parser class * * @param YamlParser * @throws \Exception */ public function setYamlParser($yamlParser) { if ($yamlParser instanceof YamlParser) { $this->yamlParser = $yamlParser; return; } throw new \Exception('Yaml Parser not supported'); } /** * Returns Yaml Parser object * * @return YamlParser */ public function getYamlParser() { if (!empty($this->yamlParser)) { return $this->yamlParser; } return new Spyc(); } }