diff --git a/src/Parser.php b/src/Parser.php index b54c41562..234584422 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -24,6 +24,28 @@ public function __construct($headers = null, $options = []) } } + /** + * Create a new Parser instance from a user agent string + * + * @param string $agent Required, a string containing the raw User-Agent + * @param array $options Optional, an array with configuration options + */ + public static function fromUserAgent($agent, array $options = []) + { + return new static($agent, $options); + } + + /** + * Create a new Parser instance from a user agent string + * + * @param array $headers Required, an array with all of the headers + * @param array $options Optional, an array with configuration options + */ + public static function fromHeaders(array $headers, array $options = []) + { + return new static($headers, $options); + } + /** * Analyse the provided headers or User-Agent string * diff --git a/tests/unit/ParserTest.php b/tests/unit/ParserTest.php index fcd7767ac..3df451f1b 100644 --- a/tests/unit/ParserTest.php +++ b/tests/unit/ParserTest.php @@ -13,8 +13,6 @@ public function testCreatingParserWithString() { $parser = new Parser("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')); } @@ -24,8 +22,6 @@ public function testCreatingParserWithHeaders() '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')); } @@ -37,8 +33,6 @@ public function testCreatingParserWithOptions() ] ]); - $this->assertTrue($parser instanceof \WhichBrowser\Parser); - $this->assertTrue($parser->isBrowser('Internet Explorer', '=', '6.0')); } @@ -46,10 +40,27 @@ public function testCreatingParserWithoutArgumentsAndCallAnalyse() { $parser = new Parser(); $parser->analyse("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)"); - + + $this->assertTrue($parser->isBrowser('Internet Explorer', '=', '6.0')); + } + + public function testCreatingParserFromUserAgent() + { + $parser = Parser::fromUserAgent('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')); } + public function testCreatingParserFromHeaderArray() + { + $parser = Parser::fromHeaders([ + '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')); + } }