1<?php
2
3/**
4 * Device Detector - The Universal Device Detection library for parsing User Agents
5 *
6 * @link https://matomo.org
7 *
8 * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later
9 */
10
11declare(strict_types=1);
12
13namespace DeviceDetector\Parser\Device;
14
15/**
16 * Class HbbTv
17 *
18 * Device parser for hbbtv detection
19 */
20class HbbTv extends AbstractDeviceParser
21{
22    /**
23     * @var string
24     */
25    protected $fixtureFile = 'regexes/device/televisions.yml';
26
27    /**
28     * @var string
29     */
30    protected $parserName = 'tv';
31
32    /**
33     * Parses the current UA and checks whether it contains HbbTv or SmartTvA information
34     *
35     * @see televisions.yml for list of detected televisions
36     *
37     * @return array|null
38     */
39    public function parse(): ?array
40    {
41        // only parse user agents containing fragments: hbbtv or SmartTvA
42        if (null === $this->isHbbTv()) {
43            return null;
44        }
45
46        parent::parse();
47
48        // always set device type to tv, even if no model/brand could be found
49        if (null === $this->deviceType) {
50            $this->deviceType = self::DEVICE_TYPE_TV;
51        }
52
53        return $this->getResult();
54    }
55
56    /**
57     * Returns if the parsed UA was identified as a HbbTV device
58     *
59     * @return string|null
60     */
61    public function isHbbTv(): ?string
62    {
63        $regex = '(?:HbbTV|SmartTvA)/([1-9]{1}(?:\.[0-9]{1}){1,2})';
64        $match = $this->matchUserAgent($regex);
65
66        return $match[1] ?? null;
67    }
68}
69