BrowscapPHP\Parser\Helper\GetPattern::getPatterns PHP Метод

getPatterns() публичный Метод

Gets some possible patterns that have to be matched against the user agent. With the given user agent string, we can optimize the search for potential patterns: - We check the first characters of the user agent (or better: a hash, generated from it) - We compare the length of the pattern with the length of the user agent (the pattern cannot be longer than the user agent!)
public getPatterns ( string $userAgent ) : Generator
$userAgent string
Результат Generator
    public function getPatterns($userAgent)
    {
        $starts = Pattern::getHashForPattern($userAgent, true);
        $length = strlen($userAgent);
        // add special key to fall back to the default browser
        $starts[] = str_repeat('z', 32);
        // get patterns, first for the given browser and if that is not found,
        // for the default browser (with a special key)
        foreach ($starts as $tmpStart) {
            $tmpSubkey = SubKey::getPatternCacheSubkey($tmpStart);
            if (!$this->cache->hasItem('browscap.patterns.' . $tmpSubkey, true)) {
                $this->logger->debug('cache key "browscap.patterns.' . $tmpSubkey . '" not found');
                continue;
            }
            $success = null;
            $file = $this->cache->getItem('browscap.patterns.' . $tmpSubkey, true, $success);
            if (!$success) {
                $this->logger->debug('cache key "browscap.patterns.' . $tmpSubkey . '" not found');
                continue;
            }
            if (!is_array($file) || !count($file)) {
                $this->logger->debug('cache key "browscap.patterns.' . $tmpSubkey . '" was empty');
                continue;
            }
            $found = false;
            foreach ($file as $buffer) {
                list($tmpBuffer, $len, $patterns) = explode("\t", $buffer, 3);
                if ($tmpBuffer === $tmpStart) {
                    if ($len <= $length) {
                        (yield trim($patterns));
                    }
                    $found = true;
                } elseif ($found === true) {
                    break;
                }
            }
        }
        (yield '');
    }

Usage Example

Пример #1
0
 /**
  *
  */
 public function testGetPatterns()
 {
     $result = $this->object->getPatterns('Mozilla/5.0 (compatible; Ask Jeeves/Teoma*)');
     self::assertInstanceOf('Generator', $result);
 }