Completed
Branch 3.0.0 (3f0ab7)
by Serhii
03:15
created

DetectorRule::loadRulesFromFile()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 2
b 0
f 0
nc 4
nop 0
dl 0
loc 19
rs 8.8571
1
<?php
2
/**
3
 * @author Sergey Nehaenko <[email protected]>
4
 * @license GPL
5
 * @copyright Sergey Nehaenko &copy 2016
6
 * @version 3.0.0
7
 * @project browser-detector
8
 */
9
10
namespace EndorphinStudio\Detector;
11
12
13
class DetectorRule
14
{
15
    /**
16
     * @return string
17
     */
18
    public function getObjectType()
19
    {
20
        return $this->ObjectType;
21
    }
22
23
    /**
24
     * @return string
25
     */
26
    public function getObjectProperty()
27
    {
28
        return $this->ObjectProperty;
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    public function getObjectPropertyValue()
35
    {
36
        return $this->ObjectPropertyValue;
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public function getTargetType()
43
    {
44
        return $this->TargetType;
45
    }
46
47
    /**
48
     * @return boolean
49
     */
50
    public function isTargetValue()
51
    {
52
        return $this->TargetValue;
53
    }
54
55
    /**
56
     * @param string $ObjectType
57
     */
58
    public function setObjectType($ObjectType)
59
    {
60
        $this->ObjectType = $ObjectType;
61
    }
62
63
    /**
64
     * @param boolean $TargetValue
65
     */
66
    public function setTargetValue($TargetValue)
67
    {
68
        $this->TargetValue = $TargetValue;
69
    }
70
71
    /**
72
     * @param string $TargetType
73
     */
74
    public function setTargetType($TargetType)
75
    {
76
        $this->TargetType = $TargetType;
77
    }
78
79
    /**
80
     * @param string $ObjectPropertyValue
81
     */
82
    public function setObjectPropertyValue($ObjectPropertyValue)
83
    {
84
        $this->ObjectPropertyValue = $ObjectPropertyValue;
85
    }
86
87
    /**
88
     * @param string $ObjectProperty
89
     */
90
    public function setObjectProperty($ObjectProperty)
91
    {
92
        $this->ObjectProperty = $ObjectProperty;
93
    }
94
    /** @var string EndorphinStudio\Detector\DetectorResult object name */
95
    private $ObjectType;
96
    /** @var string EndorphinStudio\Detector\DetectorResult property name */
97
    private $ObjectProperty;
98
    /** @var string EndorphinStudio\Detector\DetectorResult property value */
99
    private $ObjectPropertyValue;
100
    /** @var string EndorphinStudio\Detector\DetectorResult property name */
101
    private $TargetType;
102
    /** @var boolean boolean value */
103
    private $TargetValue;
104
105
    public function __construct(\SimpleXMLElement $xmlData)
106
    {
107
        if($xmlData !== null)
108
        {
109
            foreach ($xmlData->children() as $child) {
110
                switch ($child->getName())
111
                {
112
                    case 'objectType':
113
                        $this->ObjectType = $child->__toString();
114
                        break;
115
                    case 'objectProperty':
116
                        $this->ObjectProperty = $child->__toString();
117
                        break;
118
                    case 'objectPropertyValue':
119
                        $this->ObjectPropertyValue = $child->__toString();
120
                        break;
121
                    case 'targetType':
122
                        $this->TargetType = $child->__toString();
123
                        break;
124
                    case 'targetProperty':
125
                        $boolVal = $child->__toString();
126
                        if($boolVal == 'true')
127
                        {
128
                            $this->TargetValue = true;
129
                        }
130
                        else
131
                        {
132
                            $this->TargetValue = false;
133
                        }
134
                        break;
135
                }
136
            }
137
        }
138
    }
139
140
    /**
141
     * @return array Array of EndorphinStudio\Detector\DetectorRule objects
142
     */
143
    public static function loadRulesFromFile()
144
    {
145
        $path = str_replace('src','data',__DIR__).'/rules/';
146
        $files = scandir($path);
147
        $rules = array();
148
        foreach($files as $file)
149
        {
150
            if($file != '.' && $file != '..')
151
            {
152
                $xmlObj = simplexml_load_file($path.$file);
153
                foreach($xmlObj->children() as $rule)
154
                {
155
                    $rules[] = new self($rule);
156
                }
157
            }
158
        }
159
160
        return $rules;
161
    }
162
}