PhpGetBrowser::setData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace BrowscapPHP\Formatter;
5
6
/**
7
 * Formatter to output the data like the native get_browser function
8
 */
9
final class PhpGetBrowser implements FormatterInterface
10
{
11
    /**
12
     * Variable to save the settings in, type depends on implementation
13
     *
14
     * @var array
15
     */
16
    private $data = [];
17
18
    /**
19
     * a list of possible properties
20
     *
21
     * @var array
22
     */
23
    private $defaultProperties = [
24
        'browser_name_regex' => null,
25
        'browser_name_pattern' => null,
26
        'Parent' => null,
27
        'Comment' => 'Default Browser',
28
        'Browser' => 'Default Browser',
29
        'Browser_Type' => 'unknown',
30
        'Browser_Bits' => '0',
31
        'Browser_Maker' => 'unknown',
32
        'Browser_Modus' => 'unknown',
33
        'Version' => '0.0',
34
        'MajorVer' => '0',
35
        'MinorVer' => '0',
36
        'Platform' => 'unknown',
37
        'Platform_Version' => 'unknown',
38
        'Platform_Description' => 'unknown',
39
        'Platform_Bits' => '0',
40
        'Platform_Maker' => 'unknown',
41
        'Alpha' => false,
42
        'Beta' => false,
43
        'Win16' => false,
44
        'Win32' => false,
45
        'Win64' => false,
46
        'Frames' => false,
47
        'IFrames' => false,
48
        'Tables' => false,
49
        'Cookies' => false,
50
        'BackgroundSounds' => false,
51
        'JavaScript' => false,
52
        'VBScript' => false,
53
        'JavaApplets' => false,
54
        'ActiveXControls' => false,
55
        'isMobileDevice' => false,
56
        'isTablet' => false,
57
        'isSyndicationReader' => false,
58
        'Crawler' => false,
59
        'isFake' => false,
60
        'isAnonymized' => false,
61
        'isModified' => false,
62
        'CssVersion' => '0',
63
        'AolVersion' => '0',
64
        'Device_Name' => 'unknown',
65
        'Device_Maker' => 'unknown',
66
        'Device_Type' => 'unknown',
67
        'Device_Pointing_Method' => 'unknown',
68
        'Device_Code_Name' => 'unknown',
69
        'Device_Brand_Name' => 'unknown',
70
        'RenderingEngine_Name' => 'unknown',
71
        'RenderingEngine_Version' => 'unknown',
72
        'RenderingEngine_Description' => 'unknown',
73
        'RenderingEngine_Maker' => 'unknown',
74
    ];
75
76
    /**
77
     * Sets the data (done by the parser)
78
     *
79
     * @param array $settings
80
     */
81 3
    public function setData(array $settings) : void
82
    {
83 3
        foreach ($settings as $key => $value) {
84 3
            $this->data[strtolower($key)] = $value;
85
        }
86 3
    }
87
88
    /**
89
     * Gets the data (in the preferred format)
90
     *
91
     * @return \stdClass
92
     */
93 3
    public function getData() : \stdClass
94
    {
95 3
        $output = new \stdClass();
96
97 3
        $propertyNames = array_keys($this->defaultProperties);
98 3
        foreach ($propertyNames as $property) {
99 3
            $key = strtolower($property);
100
101 3
            if (array_key_exists($key, $this->data)) {
102 3
                $output->{$key} = $this->data[$key];
103 3
            } elseif ('parent' !== $key) {
104 3
                $output->{$key} = $this->defaultProperties[$property];
105
            }
106
        }
107
108
        // Don't want to normally do this, just if it exists in the data file
109
        // for our test runs
110 3
        if (array_key_exists('patternid', $this->data)) {
111 1
            $output->patternid = $this->data['patternid'];
112
        }
113
114 3
        return $output;
115
    }
116
}
117