From 1e1621235e00a0b345a6d4d8063c4e2445edd9a2 Mon Sep 17 00:00:00 2001 From: Christian Neff Date: Fri, 17 May 2024 17:22:44 +0200 Subject: [PATCH] feat: Add getter and adder for UAs to UserAgentCheck --- lib/Check/UserAgentCheck.php | 48 +++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/lib/Check/UserAgentCheck.php b/lib/Check/UserAgentCheck.php index c72b644..c0a983c 100644 --- a/lib/Check/UserAgentCheck.php +++ b/lib/Check/UserAgentCheck.php @@ -37,18 +37,16 @@ class UserAgentCheck extends AbstractCheck * * @param UserAgentInterface[] $userAgents The UserAgent objects to use * - * @throws \InvalidArgumentException If an unsupported user agent type is given. + * @throws \InvalidArgumentException If an unsupported type is given. */ public function __construct(array $userAgents = []) { foreach ($userAgents as $userAgent) { - if ($userAgent instanceof BrowserInterface) { - $this->browsers[] = $userAgent; - } elseif ($userAgent instanceof BotInterface) { - $this->bots[] = $userAgent; - } else { - throw new \InvalidArgumentException('Unsupported user agent type'); + if (!$userAgent instanceof UserAgentInterface) { + throw new \InvalidArgumentException('Unsupported type'); } + + $this->addUserAgent($userAgent); } } @@ -58,7 +56,7 @@ public function __construct(array $userAgents = []) public function checkVisitor(Visitor $visitor) { /** @var UserAgentInterface[] $userAgents */ - $userAgents = array_merge($this->bots, $this->browsers); + $userAgents = $this->getUserAgents(); foreach ($userAgents as $userAgent) { if ($userAgent->is($visitor->getUserAgent())) { @@ -69,6 +67,24 @@ public function checkVisitor(Visitor $visitor) return CheckInterface::RESULT_OKAY; } + /** + * Adds the given user agent. + * + * @param UserAgentInterface $userAgent The UserAgent object to add + * + * @throws \InvalidArgumentException If an unsupported user agent type is given. + */ + public function addUserAgent(UserAgentInterface $userAgent) + { + if ($userAgent instanceof BrowserInterface) { + $this->browsers[] = $userAgent; + } elseif ($userAgent instanceof BotInterface) { + $this->bots[] = $userAgent; + } else { + throw new \InvalidArgumentException('Unsupported user agent type'); + } + } + /** * Adds the given browser. * @@ -90,7 +106,17 @@ public function addBot(BotInterface $userAgent) } /** - * Gets list of browsers. + * Gets the list of user agents. + * + * @return UserAgentInterface[] + */ + public function getUserAgents() + { + return array_merge($this->bots, $this->browsers); + } + + /** + * Gets the list of browsers. * * @return BrowserInterface[] */ @@ -100,12 +126,10 @@ public function getBrowsers() } /** - * Gets list of bots. + * Gets the list of bots. * * @return BotInterface[] */ public function getBots() { return $this->bots; - } -}