Completed
Push — min-php71 ( f5a25a )
by James
05:53
created

PropertyHolder   A

Complexity

Total Complexity 13

Size/Duplication

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