1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Directus\SDK; |
4
|
|
|
|
5
|
|
|
use Directus\SDK\Response\EntryCollection; |
6
|
|
|
use Directus\SDK\Response\Entry; |
7
|
|
|
use Directus\Util\ArrayUtils; |
8
|
|
|
use Directus\Util\StringUtils; |
9
|
|
|
|
10
|
|
|
abstract class AbstractClient implements RequestsInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Container |
14
|
|
|
*/ |
15
|
|
|
protected $container; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param Container $container |
19
|
|
|
*/ |
20
|
|
|
public function setContainer(Container $container) |
21
|
|
|
{ |
22
|
|
|
$this->container = $container; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return Container |
27
|
|
|
*/ |
28
|
|
|
public function getContainer() |
29
|
|
|
{ |
30
|
|
|
return $this->container; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// @TODO: move to a builder class |
34
|
|
|
protected function createResponseFromData($data) |
35
|
|
|
{ |
36
|
|
|
if (isset($data['rows']) || (isset($data['data']) && ArrayUtils::isNumericKeys($data['data']))) { |
37
|
|
|
$response = new EntryCollection($data); |
38
|
|
|
} else { |
39
|
|
|
$response = new Entry($data); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $response; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function processData($tableName, array $data) |
46
|
|
|
{ |
47
|
|
|
$method = 'processDataOn' . StringUtils::underscoreToCamelCase($tableName, true); |
48
|
|
|
if (method_exists($this, $method)) { |
49
|
|
|
$data = call_user_func_array([$this, $method], [$data]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $data; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function processFile(File $file) |
56
|
|
|
{ |
57
|
|
|
$data = $file->toArray(); |
58
|
|
|
// Not container, we are using remote :) |
59
|
|
|
if (!$this->container) { |
60
|
|
|
return $data; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$Files = $this->container->get('files'); |
64
|
|
|
|
65
|
|
|
if (!array_key_exists('type', $data) || strpos($data['type'], 'embed/') === 0) { |
66
|
|
|
$recordData = $Files->saveEmbedData($data); |
67
|
|
|
} else { |
68
|
|
|
$recordData = $Files->saveData($data['data'], $data['name']); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return array_merge($recordData, ArrayUtils::omit($data, ['data', 'name'])); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function processDataOnDirectusUsers($data) |
75
|
|
|
{ |
76
|
|
|
$data = ArrayUtils::omit($data, ['id', 'user', 'access_token', 'last_login', 'last_access', 'last_page']); |
77
|
|
|
if (ArrayUtils::has($data, 'password')) { |
78
|
|
|
// @TODO: use Auth hash password |
79
|
|
|
$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT, ['cost' => 12]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (ArrayUtils::has($data, 'avatar_file_id')) { |
83
|
|
|
$data['avatar_file_id'] = $this->processFile($data['avatar_file_id']); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $data; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function processOnDirectusFiles($data) |
90
|
|
|
{ |
91
|
|
|
// @NOTE: omit columns such id or user. |
92
|
|
|
$data = ArrayUtils::omit($data, ['id', 'user']); |
93
|
|
|
|
94
|
|
|
return $data; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function processDataOnDirectusPreferences($data) |
98
|
|
|
{ |
99
|
|
|
// @NOTE: omit columns such id or user. |
100
|
|
|
$data = ArrayUtils::omit($data, ['id']); |
101
|
|
|
|
102
|
|
|
return $data; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function processDataOnDirectusBookmarks($data) |
106
|
|
|
{ |
107
|
|
|
// @NOTE: omit columns such id or user. |
108
|
|
|
$data = ArrayUtils::omit($data, ['id']); |
109
|
|
|
|
110
|
|
|
return $data; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function parseColumnData($data) |
114
|
|
|
{ |
115
|
|
|
$requiredAttributes = ['name', 'table', 'type', 'ui']; |
116
|
|
|
if (!ArrayUtils::contains($data, $requiredAttributes)) { |
117
|
|
|
throw new \Exception(sprintf('%s are required', implode(',', $requiredAttributes))); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$data = ArrayUtils::aliasKeys($data, [ |
121
|
|
|
'table_name' => 'table', |
122
|
|
|
'column_name' => 'name', |
123
|
|
|
'data_type' => 'type', |
124
|
|
|
'char_length' => 'length' |
125
|
|
|
]); |
126
|
|
|
|
127
|
|
|
return $data; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function requiredAttributes(array $attributes, array $data) |
131
|
|
|
{ |
132
|
|
View Code Duplication |
if (!ArrayUtils::contains($data, $attributes)) { |
|
|
|
|
133
|
|
|
throw new \Exception(sprintf('These attributes are required: %s', implode(',', $attributes))); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
protected function requiredOneAttribute(array $attributes, array $data) |
138
|
|
|
{ |
139
|
|
View Code Duplication |
if (!ArrayUtils::containsSome($data, $attributes)) { |
|
|
|
|
140
|
|
|
throw new \Exception(sprintf('These attributes are required: %s', implode(',', $attributes))); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
protected function getMessagesTo(array $data) |
145
|
|
|
{ |
146
|
|
|
$isGroup = ArrayUtils::has($data, 'toGroup'); |
147
|
|
|
$to = ArrayUtils::get($data, 'to', ArrayUtils::get($data, 'toGroup', [])); |
148
|
|
|
|
149
|
|
|
if (!is_array($to)) { |
150
|
|
|
$to = explode(',', $to); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$toIds = array_map(function($id) use ($isGroup) { |
154
|
|
|
return sprintf('%s_%s', ($isGroup ? 1 : 0), $id); |
155
|
|
|
}, $to); |
156
|
|
|
|
157
|
|
|
return implode(',', $toIds); |
158
|
|
|
} |
159
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.