Completed
Push — master ( d6a1bd...9f3e35 )
by Darío
04:50 queued 02:08
created

Entity::getConnectionParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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
     * 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 18
    public function setTableName($tableName)
62
    {
63 18
        $this->tableName = $tableName;
64 18
    }
65
66
    /**
67
     * Sets the connectionParams property
68
     *
69
     * @param array $connectionParams
70
     *
71
     * @return null
72
     */
73
    public function setConnectionParams($connectionParams)
74
    {
75
        $this->connectionParams = $connectionParams;
0 ignored issues
show
Bug Best Practice introduced by
The property connectionParams does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
76
    }
77
78
    /**
79
     * Sets all entity properties passed in the array
80
     *
81
     * @param array $data
82
     *
83
     * @return null
84
     */
85 2
    public function exchangeArray($data)
86
    {
87 2
        foreach ($data as $prop => $value)
88
        {
89 2
            if (property_exists($this, $prop))
90
            {
91 2
                $this->$prop = $value;
92
93 2
                if (!in_array($prop, $this->changedFields))
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
94 2
                    $this->changedFields[] = $prop;
95
            }
96
            else
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ELSE keyword; newline found
Loading history...
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
97 2
                throw new \LogicException("The property '$prop' does not exists in the class ' " . get_class($this) . " '");
98
        }
99 2
    }
100
101
    /**
102
     * Constructor
103
     *
104
     * @param array $data
105
     *
106
     * @return null
107
     */
108 18
    public function __construct($data)
109
    {
110 18
        foreach ($data as $prop => $value)
111
        {
112 2
            $this->$prop = $value;
113
        }
114
    }
115
}