Completed
Push — release/1.0 ( 41b41a...7d7258 )
by Johnny
01:57
created

AbstractReport::__isset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Redbox\Scan\Report;
3
4
// TODO: Make this class inevitable on the items so we can make the codebase in ScanService its scan() smaller
5
6
/**
7
 * This AbstractReport report class contains all the base tools
8
 * needed for Redbox\Scan\Report\Report.
9
 *
10
 * @package Redbox\Scan\Report
11
 */
12
abstract class AbstractReport
13
{
14
    /**
15
     * Return the items
16
     *
17
     * @var array
18
     */
19
    protected $items = [];
20
21
    /**
22
     * @var array
23
     */
24
    protected $newfiles = [];
25
    /**
26
     * @var array
27
     */
28
    protected $modifiedFiles = [];
29
30
    /**
31
     * Report title
32
     *
33
     * @var string
34
     */
35
    protected $name = null;
36
37
    /**
38
     * Report path
39
     *
40
     * @var string
41
     */
42
    protected $path = null;
43
44
    /**
45
     * Report data
46
     *
47
     * @var \DateTime
48
     */
49
    protected $date = null;
50
51
52
    public function __construct($array = [])
53
    {
54
        $this->mapTypes($array);
55
    }
56
57
    /**
58
     * Convert a string to camelCase
59
     * @param  string $value
60
     * @return string
61
     */
62
    public static function camelCase($value)
63
    {
64
        $value = ucwords(str_replace(array('-', '_'), ' ', $value));
65
        $value = str_replace(' ', '', $value);
66
        $value[0] = strtolower($value[0]);
67
        return $value;
68
    }
69
70
    /**
71
     * Initialize this object's properties from an array.
72
     *
73
     * @param array $array Used to seed this object's properties.
74
     * @return void
75
     */
76
    protected function mapTypes($array)
77
    {
78
79
        // Hard initialise simple types, lazy load more complex ones.
80
        foreach ($array as $key => $val) {
81
            if (!property_exists($this, $this->keyType($key)) &&
82
                property_exists($this, $key)) {
83
                $this->$key = $val;
84
                unset($array[$key]);
85
            } elseif (property_exists($this, $camelKey = self::camelCase($key))) {
86
                // This checks if property exists as camelCase, leaving it in array as snake_case
87
                // in case of backwards compatibility issues.
88
                $this->$camelKey = $val;
89
            }
90
        }
91
        $this->modelData = $array;
0 ignored issues
show
Bug introduced by
The property modelData does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
92
    }
93
94
    /**
95
     * @param array $items
96
     */
97
    public function setItems($items)
98
    {
99
        $this->items = $items;
100
    }
101
102
    /**
103
     * @param array $newfiles
104
     */
105
    public function setNewfiles($newfiles)
106
    {
107
        $this->newfiles = $newfiles;
108
    }
109
110
    /**
111
     * @param array $modifiedFiles
112
     */
113
    public function setModifiedFiles($modifiedFiles)
114
    {
115
        $this->modifiedFiles = $modifiedFiles;
116
    }
117
118
    /**
119
     * @param string $name
120
     */
121
    public function setName($name)
122
    {
123
        $this->name = $name;
124
    }
125
126
    /**
127
     * @param \DateTime $date
128
     */
129
    public function setDate($date)
130
    {
131
        $this->date = $date;
132
    }
133
134
    /**
135
     * @param string $path
136
     */
137
    public function setPath($path)
138
    {
139
        $this->path = $path;
140
    }
141
142
    /**
143
     * @return array
144
     */
145
    public function getItems()
146
    {
147
        return $this->items;
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getName()
154
    {
155
        return $this->name;
156
    }
157
158
    /**
159
     * @return string
160
     */
161
    public function getPath()
162
    {
163
        return $this->path;
164
    }
165
166
    /**
167
     * @return array
168
     */
169
    public function getNewfiles()
170
    {
171
        return $this->newfiles;
172
    }
173
174
    /**
175
     * @return array
176
     */
177
    public function getModifiedFiles()
178
    {
179
        return $this->modifiedFiles;
180
    }
181
182
    /**
183
     * @return \DateTime
184
     */
185
    public function getDate()
186
    {
187
        return $this->date;
188
    }
189
190
    protected function keyType($key)
191
    {
192
        return $key."Type";
193
    }
194
195
    protected function dataType($key)
196
    {
197
        return $key."DataType";
198
    }
199
200
    public function __isset($key)
201
    {
202
        return isset($this->modelData[$key]);
203
    }
204
205
    public function __unset($key)
206
    {
207
        unset($this->modelData[$key]);
208
    }
209
210
211
    /**
212
     * This function will convert our report into an array. This array
213
     * could later be converted into any output the adaptor wishes.
214
     *
215
     * @return array
216
     */
217
    abstract public function toArray();
218
}