Passed
Push — master ( c78efc...84ef81 )
by Darío
01:42
created

Entity::getChangedFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
     * The connection parameters
36
     *
37
     * @var array
38
     */
39
    private $connectionParams = [];
40
41
    /**
42
     * Returns the tableName property
43
     *
44
     * @return string
45
     */
46
    public function getTableName()
47
    {
48
        return $this->tableName;
49
    }
50
51
    /**
52
     * Returns a list with the fields changed
53
     *
54
     * @return array
55
     */
56
    public function getChangedFields()
57
    {
58
        return $this->changedFields;
59
    }
60
61
    /**
62
     * Returns the connectionParams property
63
     *
64
     * @return array
65
     */
66
    public function getConnectionParams()
67
    {
68
        return $this->connectionParams;
69
    }
70
71
    /**
72
     * Sets the tableName property
73
     *
74
     * @param string $tableName
75
     *
76
     * @return null
77
     */
78
    public function setTableName($tableName)
79
    {
80
        $this->tableName = $tableName;
81
    }
82
83
    /**
84
     * Sets the connectionParams property
85
     *
86
     * @param array $connectionParams
87
     *
88
     * @return null
89
     */
90
    public function setConnectionParams($connectionParams)
91
    {
92
        $this->connectionParams = $connectionParams;
93
    }
94
95
    /**
96
     * Sets all entity properties passed in the array
97
     *
98
     * @param array $data
99
     *
100
     * @return null
101
     */
102
    public function exchangeArray($data)
103
    {
104
        foreach ($data as $prop => $value)
105
        {
106
            if (property_exists($this, $prop))
107
            {
108
                $this->$prop = $value;
109
110
                if (!in_array($prop, $this->changedFields))
111
                    $this->changedFields[] = $prop;
112
            }
113
            else
114
                throw new \LogicException("The property '$prop' does not exists in the class ' " . get_class($this) . " '");
115
        }
116
    }
117
118
    /**
119
     * Constructor
120
     *
121
     * @param array $data
122
     *
123
     * @return null
124
     */
125
    public function __construct($data)
126
    {
127
        foreach ($data as $prop => $value)
128
        {
129
            $this->$prop = $value;
130
        }
131
    }
132
}