UserAgent
Raxos\Http\UserAgent
Parses a User-Agent header string into a browser, platform and version. It is immutable and implements JsonSerializable and Stringable.
readonly class UserAgent implements JsonSerializable, StringableConstructor
public function __construct(protected string $userAgent)Parses the given user agent string. The parsed pieces are available as the public readonly properties browser, platform and version, each of which may be null when it could not be determined.
HttpRequest::userAgent() builds this object for you from the request header, so you rarely construct it by hand.
Methods
public function isChrome(): boolReturns true when the browser is Google Chrome.
public function isFirefox(): boolReturns true when the browser is Mozilla Firefox.
public function isSafari(): boolReturns true when the browser is Apple Safari.
public function isInternetExplorer(): boolReturns true when the browser is Microsoft Internet Explorer.
public function isMicrosoftEdge(): boolReturns true when the browser is Microsoft Edge.
public function versionAtLeast(string $version): boolReturns true when the parsed browser version is at least the given version.
Example
<?php
declare(strict_types=1);
use Raxos\Http\HttpRequest;
$request = HttpRequest::createFromGlobals();
$agent = $request->userAgent();
if ($agent !== null && $agent->isChrome() && $agent->versionAtLeast('120')) {
// serve the modern bundle
}