misuzu/src/ClientInfo.php

146 lines
4.2 KiB
PHP
Raw Normal View History

<?php
namespace Misuzu;
use stdClass;
use JsonSerializable;
use RuntimeException;
use Stringable;
2024-09-30 17:38:08 +00:00
use DeviceDetector\{ClientHints,DeviceDetector};
use Index\Json\{JsonProperty,JsonSerializableTrait};
class ClientInfo implements Stringable, JsonSerializable {
2024-09-30 17:38:08 +00:00
use JsonSerializableTrait;
private const SERIALIZE_VERSION = 1;
public function __construct(
private array|bool|null $botInfo,
private ?array $clientInfo,
private ?array $osInfo,
private string $brandName,
private string $modelName
) {}
2024-09-30 17:38:08 +00:00
#[JsonProperty('version')]
public function getVersion(): int {
return self::SERIALIZE_VERSION;
}
#[JsonProperty('bot')]
public function getBotInfo(): bool|array|null {
if($this->botInfo === true || is_array($this->botInfo))
return $this->botInfo;
return null;
}
#[JsonProperty('client')]
public function getClientInfo(): ?array {
return $this->clientInfo;
}
#[JsonProperty('os')]
public function getOsInfo(): ?array {
return $this->osInfo;
}
#[JsonProperty('vendor', omitIfValue: '')]
public function getVendorName(): string {
return $this->brandName;
}
#[JsonProperty('model', omitIfValue: '')]
public function getModelName(): string {
return $this->modelName;
}
public function __toString(): string {
if($this->botInfo === true || is_array($this->botInfo)) {
if($this->botInfo === true)
return 'a bot';
return $this->botInfo['name'] ?? 'an unknown bot';
}
if(empty($this->clientInfo['name']))
return 'an unknown browser';
$string = $this->clientInfo['name'];
if(!empty($this->clientInfo['version']))
$string .= ' ' . $this->clientInfo['version'];
$hasOsInfo = !empty($this->osInfo['name']);
$hasModelName = !empty($this->modelName);
if($hasOsInfo || $hasModelName)
$string .= ' on ';
if($hasModelName) {
$deviceName = trim($this->brandName . ' ' . $this->modelName);
// most naive check in the world but it works well enough for this lol
$firstCharIsVowel = in_array(strtolower($deviceName[0]), ['a', 'i', 'u', 'e', 'o']);
$string .= ($firstCharIsVowel ? 'an' : 'a') . ' ' . $deviceName;
}
if($hasOsInfo) {
if($hasModelName)
$string .= ' running ';
$string .= $this->osInfo['name'];
if(!empty($this->osInfo['version']))
$string .= ' ' . $this->osInfo['version'];
if(!empty($this->osInfo['platform']))
$string .= ' (' . $this->osInfo['platform'] . ')';
}
return $string;
}
public function encode(): string {
return json_encode($this);
}
public static function decode(string $encoded): self {
$data = json_decode($encoded, true);
$version = $data['version'] ?? 0;
if($version < 0 || $version > self::SERIALIZE_VERSION)
throw new RuntimeException('$data does not contain a valid version argument');
2023-07-21 19:38:54 +00:00
return new ClientInfo(
$data['bot'] ?? null,
$data['client'] ?? null,
$data['os'] ?? null,
$data['vendor'] ?? '',
$data['model'] ?? ''
);
}
public static function parse(array|string $serverVarsOrUserAgent): self {
static $dd = null;
$dd ??= new DeviceDetector();
if(is_string($serverVarsOrUserAgent)) {
$dd->setUserAgent($serverVarsOrUserAgent);
} else {
$dd->setUserAgent(
array_key_exists('HTTP_USER_AGENT', $serverVarsOrUserAgent)
? $serverVarsOrUserAgent['HTTP_USER_AGENT'] : ''
);
$dd->setClientHints(ClientHints::factory($serverVarsOrUserAgent));
}
$dd->parse();
2023-07-21 19:38:54 +00:00
return new ClientInfo(
$dd->getBot(),
$dd->getClient(),
$dd->getOs(),
$dd->getBrandName(),
$dd->getModel()
);
}
public static function fromRequest(): self {
return self::parse($_SERVER);
}
}