1 | <?php |
||
2 | namespace Redaxscript\Client; |
||
3 | |||
4 | use Redaxscript\Request; |
||
5 | use function floor; |
||
6 | use function intval; |
||
7 | use function stristr; |
||
8 | use function strlen; |
||
9 | use function strpos; |
||
10 | use function strtolower; |
||
11 | use function substr; |
||
12 | |||
13 | /** |
||
14 | * abstract class to create a client class |
||
15 | * |
||
16 | * @since 2.4.0 |
||
17 | * |
||
18 | * @package Redaxscript |
||
19 | * @category Client |
||
20 | * @author Henry Ruhs |
||
21 | * |
||
22 | * @method protected autorun() |
||
23 | */ |
||
24 | |||
25 | abstract class ClientAbstract implements ClientInterface |
||
26 | { |
||
27 | /** |
||
28 | * output of the client |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | |||
33 | protected $_output; |
||
34 | |||
35 | /** |
||
36 | * constructor of the class |
||
37 | * |
||
38 | * @since 2.4.0 |
||
39 | * |
||
40 | * @param Request $_request instance of the request class |
||
41 | */ |
||
42 | |||
43 | public function __construct(protected Request $_request) |
||
44 | { |
||
45 | $this->autorun(); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * get the output |
||
50 | * |
||
51 | 78 | * @since 5.0.0 |
|
52 | * |
||
53 | 78 | * @return string|null |
|
54 | 78 | */ |
|
55 | 78 | ||
56 | public function getOutput() : ?string |
||
57 | { |
||
58 | return $this->_output; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * detect the required type |
||
63 | * |
||
64 | * @since 2.4.0 |
||
65 | 78 | * |
|
66 | * @param array $setupArray array of client setup |
||
67 | 78 | * @param string $type type of the client |
|
68 | */ |
||
69 | |||
70 | protected function _detect(array $setupArray = [], string $type = null) : void |
||
71 | { |
||
72 | $userAgent = strtolower($this->_request->getServer('HTTP_USER_AGENT')); |
||
73 | |||
74 | /* process setup */ |
||
75 | |||
76 | foreach ($setupArray as $value) |
||
77 | { |
||
78 | if (stristr($userAgent, $value)) |
||
79 | 78 | { |
|
80 | /* general */ |
||
81 | 78 | ||
82 | $this->_output = $value; |
||
83 | |||
84 | /* version */ |
||
85 | 78 | ||
86 | if ($type === 'version') |
||
87 | 78 | { |
|
88 | $this->_output = floor(intval(substr($userAgent, strpos($userAgent, $value) + strlen($value) + 1, 3))); |
||
89 | } |
||
90 | } |
||
91 | 55 | } |
|
92 | } |
||
93 | } |
||
94 |