Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of Client Hint detection based on the current …
…impementation in Chrome Canary
- Loading branch information
1 parent
c7b71a4
commit 46dd731
Showing
5 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace WhichBrowser\Analyser\Header; | ||
|
||
class ClientHints | ||
{ | ||
use ClientHints\Browser, ClientHints\Platform; | ||
|
||
public function __construct($headers, &$data) | ||
{ | ||
$this->headers =& $headers; | ||
$this->data =& $data; | ||
|
||
if ($header = $this->getHeader('Sec-CH-UA')) { | ||
$this->detectBrowser($header); | ||
} | ||
|
||
if ($header = $this->getHeader('Sec-CH-UA-Platform')) { | ||
$this->detectPlatform($header); | ||
} | ||
} | ||
|
||
private function getHeader($h) | ||
{ | ||
foreach ($this->headers as $k => $v) { | ||
if (strtolower($h) == strtolower($k)) { | ||
return $v; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace WhichBrowser\Analyser\Header\ClientHints; | ||
|
||
use WhichBrowser\Constants; | ||
use WhichBrowser\Data; | ||
use WhichBrowser\Model\Version; | ||
|
||
trait Browser | ||
{ | ||
private function &detectBrowser($ch) | ||
{ | ||
if (preg_match('/Google Chrome ([0-9.]*)/u', $ch, $matches)) { | ||
$this->data->browser->reset([ | ||
'name' => 'Chrome', | ||
'type' => Constants\BrowserType::BROWSER, | ||
'stock' => false, | ||
'version' => new Version([ 'value' => $matches[1] ]) | ||
]); | ||
} | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace WhichBrowser\Analyser\Header\ClientHints; | ||
|
||
use WhichBrowser\Constants; | ||
use WhichBrowser\Data; | ||
|
||
trait Platform | ||
{ | ||
private function &detectPlatform($ch) | ||
{ | ||
if (preg_match('/Mac OS X/u', $ch, $matches)) { | ||
$this->data->os->reset([ | ||
'name' => 'OS X', | ||
'alias' => 'macOS', | ||
'type' => Constants\DeviceType::DESKTOP | ||
]); | ||
} | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters