Skip to content

Commit 6739581

Browse files
committed
Adds UserAgentString object.
1 parent 530f6c6 commit 6739581

2 files changed

Lines changed: 135 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace DevCoding\Object\Internet\Header;
4+
5+
use DevCoding\Object\Internet\HeaderBag;
6+
7+
abstract class AbstractHeader
8+
{
9+
/** @var string */
10+
protected $value;
11+
12+
public function __construct(string $value)
13+
{
14+
$this->value = $value;
15+
}
16+
17+
/**
18+
* @return string
19+
*/
20+
public function __toString()
21+
{
22+
return $this->value ?? '';
23+
}
24+
25+
/**
26+
* @return array
27+
*/
28+
abstract protected function getKeys();
29+
30+
/**
31+
* @return AbstractHeader
32+
*/
33+
public static function fromHeaders()
34+
{
35+
return static::fromHeaderBag(new HeaderBag());
36+
}
37+
38+
/**
39+
* @return AbstractHeader
40+
*
41+
* @throws \ReflectionException
42+
*/
43+
public static function fromHeaderBag(HeaderBag $HeaderBag)
44+
{
45+
$rc = new \ReflectionClass(get_class());
46+
$ob = $rc->newInstanceWithoutConstructor();
47+
48+
return new static($HeaderBag->resolve($ob->getKeys()));
49+
}
50+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace DevCoding\Object\Internet\Header;
4+
5+
/**
6+
* Object representing a User-Agent header.
7+
*
8+
* @author AMJones <am@jonesiscoding.com>
9+
* @license https://github.com/deviscoding/objection/blob/main/LICENSE
10+
*
11+
* @package DevCoding\Object\Internet\Header
12+
*/
13+
class UserAgentString extends AbstractHeader
14+
{
15+
const BOTS = [
16+
'crawler', 'bot', 'spider', 'archiver', 'scraper', 'stripper', 'wget', 'curl', 'AppEngine-Google',
17+
'AdsBot-Google', 'AdsBot-Google-Mobile-Apps', 'Mediapartners-Google', 'Slurp', 'facebookexternalhit',
18+
'Zeus 32297 Webster Pro', '008', 'PagePeeker', 'Nutch', 'grub-client', 'NewsGator', 'Yandex',
19+
];
20+
21+
const HEADERS = [
22+
'USER_AGENT',
23+
'X_OPERAMINI_PHONE_UA',
24+
'X_DEVICE_USER_AGENT',
25+
'X_ORIGINAL_USER_AGENT',
26+
'X_SKYFIRE_PHONE',
27+
'X_BOLT_PHONE_UA',
28+
'DEVICE_STOCK_UA',
29+
'X_UCBROWSER_DEVICE_UA',
30+
];
31+
32+
public function isBot()
33+
{
34+
return $this->isMatch(sprintf('#(%s)#i', implode('|', static::BOTS)));
35+
}
36+
37+
public function isMatch($inc, $exc = null, &$matches = [])
38+
{
39+
if (!empty($this->_ua))
40+
{
41+
if (preg_match($inc, (string) $this, $matches))
42+
{
43+
if (empty($exc) || !preg_match($exc, (string) $this))
44+
{
45+
return true;
46+
}
47+
}
48+
}
49+
50+
return false;
51+
}
52+
53+
/**
54+
* @param string $inc Regex Pattern to match this browser, including delimiters and options
55+
* @param string|null $exc Optional Regex Pattern of exclusions, including delimiters and options
56+
* @param \Closure|null $normalizer Normalizer for regex matches. Must return an array.
57+
*
58+
* @return array|bool
59+
*/
60+
public function getMatches($inc, $exc = null, $normalizer = null)
61+
{
62+
$matches = [];
63+
if ($this->isMatch($inc, $exc, $matches))
64+
{
65+
if ($normalizer instanceof \Closure)
66+
{
67+
return $normalizer($matches);
68+
}
69+
else
70+
{
71+
return $matches;
72+
}
73+
}
74+
75+
return false;
76+
}
77+
78+
/**
79+
* @return string[]
80+
*/
81+
protected function getKeys()
82+
{
83+
return static::HEADERS;
84+
}
85+
}

0 commit comments

Comments
 (0)