File: AbstractClientParser.php

package info (click to toggle)
matomo-device-detector 6.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,684 kB
  • sloc: php: 7,758; xml: 79; makefile: 15; sh: 3
file content (125 lines) | stat: -rw-r--r-- 3,440 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?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;

use DeviceDetector\Parser\AbstractParser;

abstract class AbstractClientParser extends AbstractParser
{
    /**
     * @var string
     */
    protected $fixtureFile = '';

    /**
     * @var string
     */
    protected $parserName = '';

    /**
     * Parses the current UA and checks whether it contains any client information
     *
     * @return array|null
     *
     * @throws \Exception
     *
     * @see $fixtureFile for file with list of detected clients
     *
     * Step 1: Build a big regex containing all regexes and match UA against it
     * -> If no matches found: return
     * -> Otherwise:
     * Step 2: Walk through the list of regexes in feed_readers.yml and try to match every one
     * -> Return the matched feed reader
     *
     * NOTE: Doing the big match before matching every single regex speeds up the detection
     *
     */
    public function parse(): ?array
    {
        $result = null;

        if ($this->preMatchOverall()) {
            foreach ($this->getRegexes() as $regex) {
                $matches = $this->matchUserAgent($regex['regex']);

                if ($matches) {
                    $result = [
                        'type'    => $this->parserName,
                        'name'    => $this->buildByMatch($regex['name'], $matches),
                        'version' => $this->buildVersion((string) $regex['version'], $matches),
                    ];

                    break;
                }
            }
        }

        return $result;
    }

    /**
     * Returns all names defined in the regexes
     *
     * Attention: This method might not return all names of detected clients
     *
     * @return array
     */
    public static function getAvailableClients(): array
    {
        $regexes = (new static())->getRegexes(); // @phpstan-ignore-line
        $names   = [];

        foreach ($regexes as $regex) {
            if (false !== \strpos($regex['name'], '$1') || false !== \strpos($regex['name'], '$2')) {
                continue;
            }

            $names[] = $regex['name'];
        }

        if (static::class === MobileApp::class) {
            \array_push(
                $names,
                // Microsoft Office $1
                'Microsoft Office Access',
                'Microsoft Office Excel',
                'Microsoft Office OneDrive for Business',
                'Microsoft Office OneNote',
                'Microsoft Office PowerPoint',
                'Microsoft Office Project',
                'Microsoft Office Publisher',
                'Microsoft Office Visio',
                'Microsoft Office Word',
                // Podkicker$1
                'Podkicker',
                'Podkicker Pro',
                'Podkicker Classic',
                // radio.$1
                'radio.at',
                'radio.de',
                'radio.dk',
                'radio.es',
                'radio.fr',
                'radio.it',
                'radio.pl',
                'radio.pt',
                'radio.se',
                'radio.net'
            );
        }

        \natcasesort($names);

        return \array_unique($names);
    }
}