Passed
Push — master ( d4f0dd...ebfa4f )
by Robin
05:37
created

Model::fill()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
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 32
    public function __construct($data = null)
15
    {
16 32
        if (isset($data)) {
17 27
            $this->fill($data);
18
        }
19 32
    }
20
21
    /**
22
     * Get an associative array of the attributes.
23
     *
24
     * @return array
25
     */
26 31
    public function getAttributes()
27
    {
28 31
        return get_object_vars($this);
29
    }
30
31
    /**
32
     * Get the names of the attributes on this model.
33
     *
34
     * @return array
35
     */
36 1
    public function getAttributeNames()
37
    {
38 1
        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 30
    public function fill($data)
49
    {
50 30
        foreach ($data as $key => $value) {
51 30
            if (! property_exists($this, $key)) {
52 1
                throw new InvalidPropertyException($key . ' is not a property on ' . get_class($this));
53
            }
54
55 30
            $this->$key = $value;
56
        }
57
58 30
        return $this;
59
    }
60
}
61