Passed
Push — master ( c34495...8adf73 )
by Darío
03:13
created

Entity   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 83
ccs 18
cts 19
cp 0.9474
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getTableName() 0 3 1
A getChangedFields() 0 3 1
A setTableName() 0 3 1
A exchangeArray() 0 12 4
A __construct() 0 4 2
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Db;
12
13
/**
14
 * Entity class
15
 *
16
 * This class represents an abstract database entity, often a table
17
 */
18
abstract class Entity
19
{
20
    /**
21
     * the table's name
22
     *
23
     * @var string
24
     */
25
    private $tableName;
26
27
    /**
28
     * List of fields changed
29
     *
30
     * @var array
31
     */
32
    private $changedFields = [];
33
34
    /**
35
     * Returns the tableName property
36
     *
37
     * @return string
38
     */
39 8
    public function getTableName()
40
    {
41 8
        return $this->tableName;
42
    }
43
44
    /**
45
     * Returns a list with the fields changed
46
     *
47
     * @return array
48
     */
49 1
    public function getChangedFields()
50
    {
51 1
        return $this->changedFields;
52
    }
53
54
    /**
55
     * Sets the tableName property
56
     *
57
     * @param string $tableName
58
     *
59
     * @return null
60
     */
61 22
    public function setTableName($tableName)
62
    {
63 22
        $this->tableName = $tableName;
64 22
    }
65
66
    /**
67
     * Sets all entity properties passed in the array
68
     *
69
     * @param array $data
70
     *
71
     * @return null
72
     */
73 2
    public function exchangeArray($data)
74
    {
75 2
        foreach ($data as $prop => $value) {
76 2
            if (property_exists($this, $prop)) {
77 2
                $this->$prop = $value;
78
79 2
                if (!in_array($prop, $this->changedFields)) {
80 2
                    $this->changedFields[] = $prop;
81
                }
82
            } else {
83
                throw new \LogicException(
84 2
                    "The property '$prop' does not exists in the class ' " . get_class($this) . " '"
85
                );
86
            }
87
        }
88 2
    }
89
90
    /**
91
     * Constructor
92
     *
93
     * @param array $data
94
     *
95
     * @return null
96
     */
97 22
    public function __construct($data)
98
    {
99 22
        foreach ($data as $prop => $value) {
100 2
            $this->$prop = $value;
101
        }
102 22
    }
103
}
104