-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathModelConfig.php
121 lines (96 loc) · 3.32 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
<?php
namespace Voice\JsonQueryBuilder\Config;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class ModelConfig
{
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::get('asseco-voice.search.modelOptions'));
}
protected function getConfig(): array
{
return Config::get('asseco-voice.search.modelOptions.' . get_class($this->model));
}
public function getReturns()
{
if (array_key_exists('returns', $this->config) && $this->config['returns']) {
return $this->config['returns'];
}
return ['*'];
}
public function getRelations()
{
if (array_key_exists('relations', $this->config) && $this->config['relations']) {
return $this->config['relations'];
}
return [];
}
public function getOrderBy(): array
{
$parameters = [];
if (array_key_exists('orderBy', $this->config) && $this->config['orderBy']) {
foreach ($this->config['orderBy'] 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('eloquentExclusion', $this->config) && $this->config['eloquentExclusion']) {
$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);
} else if (count($fillable) > 0) {
$forbiddenKeys = array_diff(array_keys($this->getModelColumns()), $fillable);
}
}
return $forbiddenKeys;
}
protected function getForbiddenColumns(array $forbiddenKeys): array
{
if (array_key_exists('forbiddenColumns', $this->config) && $this->config['forbiddenColumns']) {
$forbiddenKeys = array_merge($forbiddenKeys, $this->config['forbiddenColumns']);
}
return $forbiddenKeys;
}
/**
* 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();
$columns = Schema::getColumnListing($table);
$modelColumns = [];
foreach ($columns as $column) {
$modelColumns[$column] = DB::getSchemaBuilder()->getColumnType($table, $column);
}
return $modelColumns;
}
}