-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathModelConfig.php
156 lines (124 loc) · 4.4 KB
/
ModelConfig.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
declare(strict_types=1);
namespace Asseco\JsonQueryBuilder\Config;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
class ModelConfig
{
const CACHE_PREFIX = 'table_def_';
const CACHE_TTL = 86400;
private Model $model;
private array $config;
public function __construct(Model $model)
{
$this->model = $model;
$this->config = $this->hasConfig() ? $this->getConfig() : [];
}
public function hasConfig(): bool
{
return array_key_exists(get_class($this->model), config('asseco-json-query-builder.model_options'));
}
protected function getConfig(): array
{
return config('asseco-json-query-builder.model_options.' . get_class($this->model));
}
public function getReturns(): array
{
if (array_key_exists('returns', $this->config) && $this->config['returns']) {
return Arr::wrap($this->config['returns']);
}
return ['*'];
}
public function getRelations(): array
{
if (array_key_exists('relations', $this->config) && $this->config['relations']) {
return Arr::wrap($this->config['relations']);
}
return [];
}
public function getOrderBy(): array
{
$parameters = [];
if (array_key_exists('order_by', $this->config) && $this->config['order_by']) {
foreach ($this->config['order_by'] as $key => $value) {
$parameters[] = "$key=$value";
}
}
return $parameters;
}
/**
* Union of Eloquent exclusion (guarded/fillable) and forbidden columns.
*
* @param array $forbiddenKeys
* @return array
*/
public function getForbidden(array $forbiddenKeys)
{
$forbiddenKeys = $this->getEloquentExclusion($forbiddenKeys);
$forbiddenKeys = $this->getForbiddenColumns($forbiddenKeys);
return $forbiddenKeys;
}
protected function getEloquentExclusion($forbiddenKeys): array
{
if (!array_key_exists('eloquent_exclusion', $this->config) || !$this->config['eloquent_exclusion']) {
return $forbiddenKeys;
}
$guarded = $this->model->getGuarded();
$fillable = $this->model->getFillable();
if ($guarded[0] != '*') { // Guarded property is never empty. It is '*' by default.
$forbiddenKeys = array_merge($forbiddenKeys, $guarded);
} elseif (count($fillable) > 0) {
$forbiddenKeys = array_diff(array_keys($this->getModelColumns()), $fillable);
}
return $forbiddenKeys;
}
protected function getForbiddenColumns(array $forbiddenKeys): array
{
if (!array_key_exists('forbidden_columns', $this->config) || !$this->config['forbidden_columns']) {
return $forbiddenKeys;
}
return array_merge($forbiddenKeys, $this->config['forbidden_columns']);
}
/**
* Will return column and column type array for a calling model.
* Column types will equal Eloquent column types.
*
* @return array
*/
public function getModelColumns(): array
{
$table = $this->model->getTable();
$connection = $this->model->getConnection();
if (Cache::has(self::CACHE_PREFIX . $table)) {
return Cache::get(self::CACHE_PREFIX . $table);
}
$columns = $connection->getSchemaBuilder()->getColumnListing($table);
$modelColumns = [];
$this->registerEnumTypeForDoctrine($connection);
try {
foreach ($columns as $column) {
$modelColumns[$column] = $connection->getSchemaBuilder()->getColumnType($table, $column);
}
} catch (Exception) {
// leave model columns as an empty array and cache it.
}
Cache::put(self::CACHE_PREFIX . $table, $modelColumns, self::CACHE_TTL);
return $modelColumns;
}
/**
* Having 'enum' in table definition will throw Doctrine error because it is not defined in their types.
* Registering it manually.
*/
protected function registerEnumTypeForDoctrine($connection): void
{
if (!class_exists('Doctrine\DBAL\Driver\AbstractSQLiteDriver')) {
return;
}
$connection
->getDoctrineSchemaManager()
->getDatabasePlatform()
->registerDoctrineTypeMapping('enum', 'string');
}
}