Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more semantic constructors #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
25 changes: 18 additions & 7 deletions tests/unit/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

Expand All @@ -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'));
}

Expand All @@ -37,19 +33,34 @@ public function testCreatingParserWithOptions()
]
]);

$this->assertTrue($parser instanceof \WhichBrowser\Parser);

$this->assertTrue($parser->isBrowser('Internet Explorer', '=', '6.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->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'));
}
}