示例#1
0
 public function testCreatingParserWithoutArgumentsAndCallAnalyse()
 {
     $parser = new Parser();
     $parser->analyse("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)");
     $this->assertTrue($parser instanceof \WhichBrowser\Parser);
     $this->assertTrue($parser->isBrowser('Internet Explorer', '=', '6.0'));
 }
示例#2
0
 /**
  * @requires PHP 5.5
  */
 public function testCreatingParserWithoutArgumentsAndCallAnalyse()
 {
     function countCachedItems($pool)
     {
         $items = 0;
         $reflector = new \ReflectionClass($pool);
         if ($reflector->hasProperty('cache')) {
             $property = $reflector->getProperty('cache');
             $property->setAccessible(true);
             $items = count($property->getValue($pool));
         }
         return $items;
     }
     $pool = new ArrayCachePool();
     $this->assertEquals(0, countCachedItems($pool));
     $parser = new Parser();
     $parser->setCache($pool);
     $parser->analyse("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)");
     $result = $parser->toArray();
     $this->assertEquals(1, countCachedItems($pool));
     $this->assertEquals(false, $parser->cached);
     $parser = new Parser();
     $parser->setCache($pool);
     $parser->analyse("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)");
     $this->assertEquals($result, $parser->toArray());
     $this->assertEquals(true, $parser->cached);
     $parser = new Parser("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)", ['cache' => $pool]);
     $this->assertEquals($result, $parser->toArray());
     $this->assertEquals(true, $parser->cached);
     $parser = new Parser("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)", ['cache' => $pool, 'cacheExpires' => 0]);
     $this->assertEquals($result, $parser->toArray());
     $this->assertEquals(true, $parser->cached);
 }
 public function __construct()
 {
     parent::__construct(getallheaders());
 }
示例#4
0
 public function testCreatingParserWithOptions()
 {
     $parser = new Parser(['headers' => ['User-Agent' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)']]);
     $this->assertTrue($parser instanceof \WhichBrowser\Parser);
     $this->assertTrue($parser->isBrowser('Internet Explorer', '=', '6.0'));
 }
示例#5
0
文件: query.php 项目: vicular/Parser
include_once __DIR__ . '/bootstrap.php';
use WhichBrowser\Parser;
$command = 'exec';
$options = [];
$payload = [];
array_shift($argv);
if (count($argv)) {
    foreach ($argv as $argument) {
        if (in_array($argument, ['exec'])) {
            $command = $argument;
        } elseif (substr($argument, 0, 2) == '--') {
            $options[] = substr($argument, 2);
        } else {
            $payload[] = $argument;
        }
    }
}
$payload = implode(' ', $payload);
if ($command == 'exec') {
    if ($payload == '') {
        $payload = file_get_contents('php://stdin');
    }
    if ($payload != '') {
        echo "\nInput:\n" . trim($payload) . "\n";
        $result = new Parser(trim($payload));
        echo "\nHuman readable:\n" . $result->toString() . "\n";
        echo "\nData:\n";
        echo json_encode($result, JSON_PRETTY_PRINT);
        echo "\n\n";
    }
}
 /**
  *
  * @param Model\Device         $device
  * @param \WhichBrowser\Device $deviceRaw
  * @param WhichBrowserParser   $parser
  */
 private function hydrateDevice(Model\Device $device, \WhichBrowser\Device $deviceRaw, WhichBrowserParser $parser)
 {
     if ($this->isRealResult($deviceRaw->getModel()) === true) {
         $device->setModel($deviceRaw->getModel());
     }
     if ($this->isRealResult($deviceRaw->getManufacturer()) === true) {
         $device->setBrand($deviceRaw->getManufacturer());
     }
     $device->setType($parser->getType());
     if ($parser->isType('mobile', 'tablet', 'ereader', 'media', 'watch', 'camera', 'gaming:portable') === true) {
         $device->setIsMobile(true);
     }
 }
示例#7
0
 private static function rebaseFile($file, $sort)
 {
     $result = [];
     if (file_exists($file)) {
         $rules = Yaml::parse(file_get_contents($file));
         if (is_array($rules)) {
             echo "Rebasing {$file}\n";
             foreach ($rules as $k => $v) {
                 if (is_string($rules[$k]['headers'])) {
                     $rules[$k]['headers'] = http_parse_headers($rules[$k]['headers']);
                 }
             }
             if ($sort) {
                 $rules = self::sortRules($rules);
             }
             foreach ($rules as $rule) {
                 if (count($rule['headers']) > 1) {
                     $headers = $rule['headers'];
                 } else {
                     $key = array_keys($rule['headers'])[0];
                     $headers = $key . ': ' . $rule['headers'][$key];
                 }
                 $options = $rule;
                 if (isset($options['result'])) {
                     unset($options['result']);
                 }
                 $detected = new Parser($options);
                 $rule['headers'] = $headers;
                 $rule['readable'] = $detected->toString();
                 $rule['result'] = $detected->toArray();
                 $result[] = $rule;
             }
             if (count($result)) {
                 if (count($result) == count($rules)) {
                     if ($string = Yaml::dump($result)) {
                         file_put_contents($file . '.tmp', $string);
                         rename($file, $file . '.old');
                         rename($file . '.tmp', $file);
                         unlink($file . '.old');
                     }
                 } else {
                     echo "Rebasing {$file}\t\t => output does not match input\n";
                 }
             } else {
                 echo "Rebasing {$file}\t\t => no results found\n";
             }
         } else {
             echo "Rebasing {$file}\t\t => error reading file\n";
         }
     } else {
         echo "Rebasing {$file}\t\t => file not found\n";
     }
 }
 /**
  *
  * @param Model\Device               $device
  * @param \WhichBrowser\Model\Device $deviceRaw
  * @param WhichBrowserParser         $parser
  */
 private function hydrateDevice(Model\Device $device, \WhichBrowser\Model\Device $deviceRaw, WhichBrowserParser $parser)
 {
     $device->setModel($this->getRealResult($deviceRaw->getModel()));
     $device->setBrand($this->getRealResult($deviceRaw->getManufacturer()));
     $device->setType($this->getRealResult($parser->getType()));
     if ($parser->isMobile() === true) {
         $device->setIsMobile(true);
     }
 }