Test Failed
Push — master ( cf61d2...d4f0dd )
by Robin
02:18
created

Model::fill()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.0416
1
<?php
2
3
namespace Konsulting\JustGivingApiSdk\ResourceClients\Models;
4
5
use Konsulting\JustGivingApiSdk\Exceptions\InvalidPropertyException;
6
7
class Model
8
{
9
    /**
10
     * Allow data to be filled via the constructor.
11
     *
12
     * @param iterable $data
13
     */
14 5
    public function __construct($data = null)
15
    {
16 5
        if (isset($data)) {
17 5
            $this->fill($data);
18
        }
19 5
    }
20
21
    /**
22
     * Get an associative array of the attributes.
23
     *
24
     * @return array
25
     */
26 5
    public function getAttributes()
27
    {
28 5
        return get_object_vars($this);
29
    }
30
31
    /**
32
     * Get the names of the attributes on this model.
33
     *
34
     * @return array
35
     */
36
    public function getAttributeNames()
37
    {
38
        return array_keys($this->getAttributes());
39
    }
40
41
    /**
42
     * Populate the model with the supplied data.
43
     *
44
     * @param iterable $data
45
     * @return $this
46
     * @throws InvalidPropertyException
47
     */
48 5
    public function fill($data)
49
    {
50 5
        foreach ($data as $key => $value) {
51 5
            if (! property_exists($this, $key)) {
52
                throw new InvalidPropertyException($key . ' is not a property on ' . get_class($this));
53
            }
54
55 5
            $this->$key = $value;
56
        }
57
58 5
        return $this;
59
    }
60
}
61