Completed
Push — master ( 0eeb04...4002ae )
by Oscar
02:31
created

AbstractRow   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 10
c 5
b 0
f 0
lcom 1
cbo 1
dl 0
loc 128
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTable() 0 4 1
A getDatabase() 0 4 1
A jsonSerialize() 0 4 1
A __toString() 0 4 1
toArray() 0 1 ?
__get() 0 1 ?
__set() 0 1 ?
A delete() 0 14 2
A __call() 0 20 3
1
<?php
2
3
namespace SimpleCrud;
4
5
use JsonSerializable;
6
use SimpleCrud\Scheme\Scheme;
7
8
/**
9
 * Base class used by Row and RowCollection.
10
 *
11
 * @property mixed $id
12
 */
13
abstract class AbstractRow implements JsonSerializable
14
{
15
    protected $table;
16
17
    /**
18
     * Constructor.
19
     *
20
     * @param Table $table
21
     */
22
    public function __construct(Table $table)
23
    {
24
        $this->table = $table;
25
    }
26
27
    /**
28
     * Returns the table associated with this row.
29
     *
30
     * @return Table
31
     */
32
    public function getTable()
33
    {
34
        return $this->table;
35
    }
36
37
    /**
38
     * Returns the database associated with this row.
39
     *
40
     * @return SimpleCrud
41
     */
42
    public function getDatabase()
43
    {
44
        return $this->getTable()->getDatabase();
45
    }
46
47
    /**
48
     * @see JsonSerializable
49
     *
50
     * @return array
51
     */
52
    public function jsonSerialize()
53
    {
54
        return $this->toArray();
55
    }
56
57
    /**
58
     * Magic method to stringify the values.
59
     *
60
     * @return string
61
     */
62
    public function __toString()
63
    {
64
        return json_encode($this, JSON_NUMERIC_CHECK);
65
    }
66
67
    /**
68
     * Converts this object into an array.
69
     * 
70
     * @param array $bannedEntities
71
     *
72
     * @return array
73
     */
74
    abstract public function toArray(array $bannedEntities = []);
75
76
    /**
77
     * Magic method to return properties.
78
     * 
79
     * @param string $name
80
     *
81
     * @return mixed
82
     */
83
    abstract public function __get($name);
84
85
    /**
86
     * Magic method to edit a property.
87
     * 
88
     * @param string $name
89
     * @param mixed  $value
90
     *
91
     * @return mixed
92
     */
93
    abstract public function __set($name, $value);
94
95
    /**
96
     * Deletes the row(s) in the database.
97
     *
98
     * @return self
99
     */
100
    public function delete()
101
    {
102
        $id = $this->id;
103
104
        if (!empty($id)) {
105
            $this->getTable()->delete()
0 ignored issues
show
Documentation Bug introduced by
The method delete does not exist on object<SimpleCrud\Table>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
106
                ->byId($id)
107
                ->run();
108
109
            $this->id = null;
110
        }
111
112
        return $this;
113
    }
114
115
    /**
116
     * Magic method to execute queries.
117
     *
118
     * @param string $name
119
     */
120
    public function __call($name, $arguments)
121
    {
122
        $scheme = $this->getTable()->getScheme();
123
124
        if (!isset($scheme['relations'][$name])) {
125
            throw new \BadMethodCallException(sprintf('Call to undefined method %s', $name));
126
        }
127
128
        $table = $this->getTable();
129
        $relation = $table->getScheme()['relations'][$name];
130
        $related = $table->getDatabase()->$name;
131
132
        switch ($relation[0]) {
133
            case Scheme::HAS_ONE:
134
                return $related->select()->one()->relatedWith($this);
135
136
            default:
137
                return $related->select()->relatedWith($this);
138
        }
139
    }
140
}
141