From 0cf672c219b6518ed0bd70f70e15084850152547 Mon Sep 17 00:00:00 2001 From: Christian Neff Date: Fri, 17 May 2024 02:00:47 +0200 Subject: [PATCH] feat: Add UserAgentCheck constructor accepting an array of user agents This commit refactors the `UserAgentCheck` class by adding a constructor that accepts an array of user agents. The constructor now loops through the user agents and adds them to the respective arrays (`browsers` or `bots`) based on their type. This change improves the convenience of the `UserAgentCheck` class. --- lib/Check/UserAgentCheck.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/Check/UserAgentCheck.php b/lib/Check/UserAgentCheck.php index fbe1223..32807c5 100644 --- a/lib/Check/UserAgentCheck.php +++ b/lib/Check/UserAgentCheck.php @@ -32,6 +32,26 @@ class UserAgentCheck extends AbstractCheck */ protected $bots = []; + /** + * Constructor. + * + * @param UserAgentInterface[] $userAgents The UserAgent objects to use + * + * @throws \InvalidArgumentException If an unsupported user agent 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'); + } + } + } + /** * {@inheritdoc} */