PropertyHolder   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 0
dl 0
loc 200
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getPropertyType() 0 100 6
B checkValueInArray() 0 75 7
1
<?php
2
declare(strict_types = 1);
3
4
namespace BrowscapPHP\Data;
5
6
final class PropertyHolder
7
{
8
    public const TYPE_STRING = 'string';
9
    public const TYPE_GENERIC = 'generic';
10
    public const TYPE_NUMBER = 'number';
11
    public const TYPE_BOOLEAN = 'boolean';
12
    public const TYPE_IN_ARRAY = 'in_array';
13
14
    /**
15
     * Get the type of a property.
16
     *
17
     * @param string $propertyName
18
     *
19
     * @return string
20
     */
21
    public function getPropertyType(string $propertyName) : string
22
    {
23
        $stringProperties = [
24
            'Comment' => 1,
25
            'Browser' => 1,
26
            'Browser_Maker' => 1,
27
            'Browser_Modus' => 1,
28
            'Platform' => 1,
29
            'Platform_Name' => 1,
30
            'Platform_Description' => 1,
31
            'Device_Name' => 1,
32
            'Platform_Maker' => 1,
33
            'Device_Code_Name' => 1,
34
            'Device_Maker' => 1,
35
            'Device_Brand_Name' => 1,
36
            'RenderingEngine_Name' => 1,
37
            'RenderingEngine_Description' => 1,
38
            'RenderingEngine_Maker' => 1,
39
            'Parent' => 1,
40
            'PropertyName' => 1,
41
            'CDF' => 1,
42
            'PatternId' => 1,
43
        ];
44
45
        if (array_key_exists($propertyName, $stringProperties)) {
46
            return self::TYPE_STRING;
47
        }
48
49
        $arrayProperties = [
50
            'Browser_Type' => 1,
51
            'Device_Type' => 1,
52
            'Device_Pointing_Method' => 1,
53
            'Browser_Bits' => 1,
54
            'Platform_Bits' => 1,
55
        ];
56
57
        if (array_key_exists($propertyName, $arrayProperties)) {
58
            return self::TYPE_IN_ARRAY;
59
        }
60
61
        $genericProperties = [
62
            'Platform_Version' => 1,
63
            'RenderingEngine_Version' => 1,
64
            'Released' => 1,
65
            'Format' => 1,
66
            'Type' => 1,
67
        ];
68
69
        if (array_key_exists($propertyName, $genericProperties)) {
70
            return self::TYPE_GENERIC;
71
        }
72
73
        $numericProperties = [
74
            'Version' => 1,
75
            'CssVersion' => 1,
76
            'AolVersion' => 1,
77
            'MajorVer' => 1,
78
            'MinorVer' => 1,
79
            'aolVersion' => 1,
80
        ];
81
82
        if (array_key_exists($propertyName, $numericProperties)) {
83
            return self::TYPE_NUMBER;
84
        }
85
86
        $booleanProperties = [
87
            'Alpha' => 1,
88
            'Beta' => 1,
89
            'Win16' => 1,
90
            'Win32' => 1,
91
            'Win64' => 1,
92
            'Frames' => 1,
93
            'IFrames' => 1,
94
            'Tables' => 1,
95
            'Cookies' => 1,
96
            'BackgroundSounds' => 1,
97
            'JavaScript' => 1,
98
            'VBScript' => 1,
99
            'JavaApplets' => 1,
100
            'ActiveXControls' => 1,
101
            'isMobileDevice' => 1,
102
            'isTablet' => 1,
103
            'isSyndicationReader' => 1,
104
            'Crawler' => 1,
105
            'MasterParent' => 1,
106
            'LiteMode' => 1,
107
            'isFake' => 1,
108
            'isAnonymized' => 1,
109
            'isModified' => 1,
110
            'isBanned' => 1,
111
            'supportsCSS' => 1,
112
            'AOL' => 1,
113
        ];
114
115
        if (array_key_exists($propertyName, $booleanProperties)) {
116
            return self::TYPE_BOOLEAN;
117
        }
118
119
        throw new \InvalidArgumentException("Property {$propertyName} did not have a defined property type");
120
    }
121
122
    /**
123
     * @param string $property
124
     * @param string $value
125
     *
126
     * @throws \InvalidArgumentException
127
     *
128
     * @return string
129
     */
130
    public function checkValueInArray(string $property, string $value) : string
131
    {
132
        switch ($property) {
133
            case 'Browser_Type':
134
                $allowedValues = [
135
                    'Useragent Anonymizer' => 1,
136
                    'Browser' => 1,
137
                    'Offline Browser' => 1,
138
                    'Multimedia Player' => 1,
139
                    'Library' => 1,
140
                    'Feed Reader' => 1,
141
                    'Email Client' => 1,
142
                    'Bot/Crawler' => 1,
143
                    'Application' => 1,
144
                    'Tool' => 1,
145
                    'unknown' => 1,
146
                ];
147
148
                break;
149
            case 'Device_Type':
150
                $allowedValues = [
151
                    'Console' => 1,
152
                    'TV Device' => 1,
153
                    'Tablet' => 1,
154
                    'Mobile Phone' => 1,
155
                    'Smartphone' => 1,    // actual mobile phone with touchscreen
156
                    'Feature Phone' => 1, // older mobile phone
157
                    'Mobile Device' => 1,
158
                    'FonePad' => 1,       // Tablet sized device with the capability to make phone calls
159
                    'Desktop' => 1,
160
                    'Ebook Reader' => 1,
161
                    'Car Entertainment System' => 1,
162
                    'Digital Camera' => 1,
163
                    'unknown' => 1,
164
                ];
165
166
                break;
167
            case 'Device_Pointing_Method':
168
                // This property is taken from http://www.scientiamobile.com/wurflCapability
169
                $allowedValues = [
170
                    'joystick' => 1,
171
                    'stylus' => 1,
172
                    'touchscreen' => 1,
173
                    'clickwheel' => 1,
174
                    'trackpad' => 1,
175
                    'trackball' => 1,
176
                    'mouse' => 1,
177
                    'unknown' => 1,
178
                ];
179
180
                break;
181
            case 'Browser_Bits':
182
            case 'Platform_Bits':
183
                $allowedValues = [
184
                    '0' => 1,
185
                    '8' => 1,
186
                    '16' => 1,
187
                    '32' => 1,
188
                    '64' => 1,
189
                ];
190
191
                break;
192
            default:
193
                throw new \InvalidArgumentException('Property "' . $property . '" is not defined to be validated');
194
        }
195
196
        if (array_key_exists($value, $allowedValues)) {
197
            return $value;
198
        }
199
200
        throw new \InvalidArgumentException(
201
            'invalid value given for Property "' . $property . '": given value "' . $value . '", allowed: '
202
            . \ExceptionalJSON\encode($allowedValues)
203
        );
204
    }
205
}
206