1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Directus\SDK; |
4
|
|
|
|
5
|
|
|
use Directus\Database\Connection; |
6
|
|
|
use Directus\Database\TableGateway\BaseTableGateway; |
7
|
|
|
use Directus\Database\TableGateway\RelationalTableGatewayWithConditions; |
8
|
|
|
use Zend\Db\Sql\Select; |
9
|
|
|
|
10
|
|
|
class ClientLocal implements RequestsInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var BaseTableGateway[] |
14
|
|
|
*/ |
15
|
|
|
protected $tableGateways = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Connection |
19
|
|
|
*/ |
20
|
|
|
protected $connection = null; |
21
|
|
|
|
22
|
|
|
public function __construct($connection) |
23
|
|
|
{ |
24
|
|
|
$this->connection = $connection; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get a table gateway for the given table name |
29
|
|
|
* |
30
|
|
|
* @param $tableName |
31
|
|
|
* |
32
|
|
|
* @return RelationalTableGatewayWithConditions |
33
|
|
|
*/ |
34
|
|
|
protected function getTableGateway($tableName) |
35
|
|
|
{ |
36
|
|
|
if (!array_key_exists($tableName, $this->tableGateways)) { |
37
|
|
|
$this->tableGateways[$tableName] = new RelationalTableGatewayWithConditions($tableName, $this->connection); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $this->tableGateways[$tableName]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getEntries($tableName, array $options = []) |
44
|
|
|
{ |
45
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
46
|
|
|
|
47
|
|
|
return $tableGateway->getEntries($options); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getEntry($id, $tableName, array $options = []) |
51
|
|
|
{ |
52
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
53
|
|
|
|
54
|
|
|
return $tableGateway->getEntries(array_merge(['id' => $id], $options)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritDoc |
59
|
|
|
*/ |
60
|
|
|
public function fetchTables() |
61
|
|
|
{ |
62
|
|
|
// TODO: Implement fetchTables() method. |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritDoc |
67
|
|
|
*/ |
68
|
|
|
public function fetchTableInfo($tableName) |
69
|
|
|
{ |
70
|
|
|
// TODO: Implement fetchTableInfo() method. |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritDoc |
75
|
|
|
*/ |
76
|
|
|
public function fetchColumns($tableName) |
77
|
|
|
{ |
78
|
|
|
// TODO: Implement fetchColumns() method. |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritDoc |
83
|
|
|
*/ |
84
|
|
|
public function fetchColumnInfo($tableName, $columnName) |
85
|
|
|
{ |
86
|
|
|
// TODO: Implement fetchColumnInfo() method. |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritDoc |
91
|
|
|
*/ |
92
|
|
|
public function fetchItems($tableName = null, $conditions = []) |
93
|
|
|
{ |
94
|
|
|
if ($tableName == null) { |
95
|
|
|
$tableName = $this->getTable(); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$select = new Select($tableName); |
99
|
|
|
|
100
|
|
|
// Conditional to honor the active column, (does not check if column exists) |
101
|
|
|
if (isset($conditions['active'])) { |
102
|
|
|
$select->where->equalTo('active', $conditions['active']); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Order by "id desc" by default or by a parameter value |
106
|
|
|
if (isset($conditions['sort'])) { |
107
|
|
|
$select->order($conditions['sort']); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->selectWith($select); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @inheritDoc |
115
|
|
|
*/ |
116
|
|
|
public function fetchItem($tableName, $itemID) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
// TODO: Implement fetchItem() method. |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @inheritDoc |
123
|
|
|
*/ |
124
|
|
|
public function fetchGroups() |
125
|
|
|
{ |
126
|
|
|
// TODO: Implement fetchGroups() method. |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheritDoc |
131
|
|
|
*/ |
132
|
|
|
public function fetchGroupInfo($groupID) |
133
|
|
|
{ |
134
|
|
|
// TODO: Implement fetchGroupInfo() method. |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @inheritDoc |
139
|
|
|
*/ |
140
|
|
|
public function fetchGroupPrivileges($groupID) |
141
|
|
|
{ |
142
|
|
|
// TODO: Implement fetchGroupPrivileges() method. |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @inheritDoc |
147
|
|
|
*/ |
148
|
|
|
public function fetchFiles() |
149
|
|
|
{ |
150
|
|
|
// TODO: Implement fetchFiles() method. |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @inheritDoc |
155
|
|
|
*/ |
156
|
|
|
public function fetchFileInfo($fileID) |
157
|
|
|
{ |
158
|
|
|
// TODO: Implement fetchFileInfo() method. |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @inheritDoc |
163
|
|
|
*/ |
164
|
|
|
public function fetchSettings() |
165
|
|
|
{ |
166
|
|
|
// TODO: Implement fetchSettings() method. |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @inheritDoc |
171
|
|
|
*/ |
172
|
|
|
public function fetchSettingCollection($collectionName) |
173
|
|
|
{ |
174
|
|
|
// TODO: Implement fetchSettingCollection() method. |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.