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
|
28 |
|
protected function createResponseFromData($data) |
35
|
|
|
{ |
36
|
28 |
|
if (isset($data['rows']) || (isset($data['data']) && ArrayUtils::isNumericKeys($data['data']))) { |
37
|
6 |
|
$response = new EntryCollection($data); |
38
|
6 |
|
} else { |
39
|
22 |
|
$response = new Entry($data); |
40
|
|
|
} |
41
|
|
|
|
42
|
28 |
|
return $response; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function parseData($tableName, array $data) |
46
|
|
|
{ |
47
|
|
|
$method = 'parseOn' . 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 parseFile(File $file) |
56
|
|
|
{ |
57
|
|
|
$Files = $this->container->get('files'); |
58
|
|
|
$data = $file->toArray(); |
59
|
|
|
if (!array_key_exists('type', $data) || strpos($data['type'], 'embed/') === 0) { |
60
|
|
|
$recordData = $Files->saveEmbedData($data); |
61
|
|
|
} else { |
62
|
|
|
$recordData = $Files->saveData($data['data'], $data['name']); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return array_merge($recordData, ArrayUtils::omit($data, ['data', 'name'])); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function parseOnDirectusUsers($data) |
69
|
|
|
{ |
70
|
|
|
$data = ArrayUtils::omit($data, ['id', 'user', 'access_token', 'last_login', 'last_access', 'last_page']); |
71
|
|
|
if (ArrayUtils::has($data, 'password')) { |
72
|
|
|
// @TODO: use Auth hash password |
73
|
|
|
$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT, ['cost' => 12]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (ArrayUtils::has($data, 'avatar_file_id')) { |
77
|
|
|
$data['avatar_file_id'] = $this->parseFile($data['avatar_file_id']); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $data; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function parseOnDirectusFiles($data) |
84
|
|
|
{ |
85
|
|
|
// @TODO: omit columns such id or user. |
86
|
|
|
$data = ArrayUtils::omit($data, ['id', 'user']); |
87
|
|
|
|
88
|
|
|
return $data; |
89
|
|
|
} |
90
|
|
|
} |