Completed
Push — master ( 9f3e35...87170e )
by Darío
02:47
created

Entity::getChangedFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 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
        {
77 2
            if (property_exists($this, $prop))
78
            {
79 2
                $this->$prop = $value;
80
81 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...
82 2
                    $this->changedFields[] = $prop;
83
            }
84
            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...
85 2
                throw new \LogicException("The property '$prop' does not exists in the class ' " . get_class($this) . " '");
86
        }
87 2
    }
88
89
    /**
90
     * Constructor
91
     *
92
     * @param array $data
93
     *
94
     * @return null
95
     */
96 18
    public function __construct($data)
97
    {
98 18
        foreach ($data as $prop => $value)
99
        {
100 2
            $this->$prop = $value;
101
        }
102
    }
103
}