Example #1
0
 /**
  * @covers ::setFormatter
  */
 public function testCustomFormatter()
 {
     $customFormatter = new Optimized();
     $browscap = new Browscap();
     $browscap->setFormatter($customFormatter);
     $formatter = $browscap->getFormatter();
     static::assertSame($customFormatter, $formatter);
 }
Example #2
0
 /**
  * Gets the browser data formatter for the given user agent
  * (or null if no data available, no even the default browser)
  *
  * @param string $user_agent
  * @return \Crossjoin\Browscap\Formatter\AbstractFormatter|null
  */
 public function getBrowser($user_agent)
 {
     $formatter = null;
     foreach ($this->getPatterns($user_agent) as $patterns) {
         if (preg_match("/^(?:" . str_replace("\t", ")|(?:", $patterns) . ")\$/i", $user_agent)) {
             // strtok() requires less memory than explode()
             $pattern = strtok($patterns, "\t");
             while ($pattern !== false) {
                 $pattern = str_replace('[\\d]', '(\\d)', $pattern);
                 if (preg_match('/^' . $pattern . '$/i', $user_agent, $matches)) {
                     // Insert the digits back into the pattern, so that we can search the settings for it
                     if (count($matches) > 1) {
                         array_shift($matches);
                         foreach ($matches as $one_match) {
                             $num_pos = strpos($pattern, '(\\d)');
                             $pattern = substr_replace($pattern, $one_match, $num_pos, 4);
                         }
                     }
                     // Try to get settings - as digits have been replaced to speed up the pattern search (up to 90 faster),
                     // we won't always find the data in the first step - so check if settings have been found and if not,
                     // search for the next pattern.
                     $settings = $this->getSettings($pattern);
                     if (count($settings) > 0) {
                         $formatter = Browscap::getFormatter();
                         $formatter->setData($this->getSettings($pattern));
                         break 2;
                     }
                 }
                 $pattern = strtok("\t");
             }
         }
     }
     return $formatter;
 }