File: Version.php

package info (click to toggle)
matomo-device-detector 6.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 19,124 kB
  • sloc: php: 7,493; xml: 79; makefile: 15; sh: 3
file content (85 lines) | stat: -rw-r--r-- 1,861 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php

/**
 * Device Detector - The Universal Device Detection library for parsing User Agents
 *
 * @link https://matomo.org
 *
 * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later
 */

declare(strict_types=1);

namespace DeviceDetector\Parser\Client\Browser\Engine;

use DeviceDetector\Parser\Client\AbstractClientParser;

/**
 * Class Version
 *
 * Client parser for browser engine version detection
 */
class Version extends AbstractClientParser
{
    /**
     * @var string
     */
    private $engine;

    /**
     * Version constructor.
     *
     * @param string $ua
     * @param string $engine
     */
    public function __construct(string $ua, string $engine)
    {
        parent::__construct($ua);

        $this->engine = $engine;
    }

    /**
     * @inheritdoc
     */
    public function parse(): ?array
    {
        if (empty($this->engine)) {
            return null;
        }

        if ('Gecko' === $this->engine || 'Clecko' === $this->engine) {
            $pattern = '~[ ](?:rv[: ]([0-9.]+)).*(?:g|cl)ecko/[0-9]{8,10}~i';

            if (\preg_match($pattern, $this->userAgent, $matches)) {
                return ['version' => \array_pop($matches)];
            }
        }

        $engineToken = $this->engine;

        if ('Blink' === $this->engine) {
            $engineToken = 'Chr[o0]me|Chromium|Cronet';
        }

        if ('Arachne' === $this->engine) {
            $engineToken = 'Arachne\/5\.';
        }

        if ('LibWeb' === $this->engine) {
            $engineToken = 'LibWeb\+LibJs';
        }

        \preg_match(
            "~(?:{$engineToken})\s*[/_]?\s*((?(?=\d+\.\d)\d+[.\d]*|\d{1,7}(?=(?:\D|$))))~i",
            $this->userAgent,
            $matches
        );

        if (!$matches) {
            return null;
        }

        return ['version' => \array_pop($matches)];
    }
}