|
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
|
|
|
// @TODO: move to a builder class |
|
13
|
28 |
|
protected function createResponseFromData($data) |
|
14
|
|
|
{ |
|
15
|
28 |
|
if (isset($data['rows']) || (isset($data['data']) && ArrayUtils::isNumericKeys($data['data']))) { |
|
16
|
6 |
|
$response = new EntryCollection($data); |
|
17
|
6 |
|
} else { |
|
18
|
22 |
|
$response = new Entry($data); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
28 |
|
return $response; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
protected function parseData($tableName, array $data) |
|
25
|
|
|
{ |
|
26
|
|
|
$method = 'parseOn' . StringUtils::underscoreToCamelCase($tableName, true); |
|
27
|
|
|
if (method_exists($this, $method)) { |
|
28
|
|
|
$data = call_user_func_array([$this, $method], [$data]); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return $data; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function parseFile($path) |
|
35
|
|
|
{ |
|
36
|
|
|
$attributes = []; |
|
37
|
|
|
if (file_exists($path)) { |
|
38
|
|
|
$ext = pathinfo($path, PATHINFO_EXTENSION); |
|
39
|
|
|
$mimeType = mime_content_type($path); |
|
40
|
|
|
$attributes['name'] = pathinfo($path, PATHINFO_FILENAME) . '.' . $ext; |
|
41
|
|
|
$attributes['type'] = $mimeType; |
|
42
|
|
|
$content = file_get_contents($path); |
|
43
|
|
|
$base64 = 'data:' . $mimeType . ';base64,' . base64_encode($content); |
|
44
|
|
|
$attributes['data'] = $base64; |
|
45
|
|
|
} else { |
|
46
|
|
|
throw new \Exception('Missing "file" or "data" attribute.'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $attributes; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function parseOnDirectusUsers($data) |
|
53
|
|
|
{ |
|
54
|
|
|
$data = ArrayUtils::omit($data, ['id', 'user', 'access_token', 'last_login', 'last_access', 'last_page']); |
|
55
|
|
|
if (ArrayUtils::has($data, 'password')) { |
|
56
|
|
|
// @TODO: use Auth hash password |
|
57
|
|
|
$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT, ['cost' => 12]); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (ArrayUtils::has($data, 'avatar_file_id')) { |
|
61
|
|
|
$data['avatar_file_id'] = $this->parseFile($data['avatar_file_id']); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $data; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected function parseOnDirectusFiles($data) |
|
68
|
|
|
{ |
|
69
|
|
|
// @TODO: omit columns such id or user. |
|
70
|
|
|
$data = ArrayUtils::omit($data, ['id', 'user']); |
|
71
|
|
|
|
|
72
|
|
|
return $data; |
|
73
|
|
|
} |
|
74
|
|
|
} |