You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
4.0 KiB
140 lines
4.0 KiB
<?php
|
|
|
|
namespace App\Models\Traits;
|
|
|
|
use App\Rules\ExistsRule;
|
|
use App\Rules\UniqueRule;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Support\Str;
|
|
use Jenssegers\Mongodb\Eloquent\Model as Document;
|
|
|
|
|
|
trait ValidationMaker
|
|
{
|
|
private Model|Document|null $modelDocument;
|
|
private array $field = [];
|
|
private array $rules = [];
|
|
|
|
protected function createValidations($prefix = 'fields'): array
|
|
{
|
|
$this->modelDocument = app('modelDocument');
|
|
|
|
if (empty($this->modelDocument)) {
|
|
return $this->rules;
|
|
}
|
|
|
|
|
|
foreach ($this->modelDocument->{$prefix} ?? [] as $field) {
|
|
$this->field = $field;
|
|
$this->field['request_name'] = $prefix . '.' . $this->field['name'];
|
|
$this->required();
|
|
$this->type();
|
|
$this->primitiveType();
|
|
$this->unique();
|
|
$this->limit('min');
|
|
$this->limit('max');
|
|
$this->in();
|
|
}
|
|
|
|
return $this->rules;
|
|
}
|
|
|
|
private function isArray(): bool
|
|
{
|
|
return $this->field['info']['multi'] == true || $this->field['info']['multi'] == "true";
|
|
}
|
|
|
|
private function isResource(): bool
|
|
{
|
|
return str_contains($this->field['info']['type'], 'resource:');
|
|
}
|
|
|
|
private function isSetting(): bool
|
|
{
|
|
return str_contains($this->field['info']['type'], 'setting:');
|
|
}
|
|
|
|
private function complexName(): string
|
|
{
|
|
return $this->field['request_name'] . ($this->isArray() ? '.*' : '');
|
|
}
|
|
|
|
private function required()
|
|
{
|
|
$this->rules[$this->field['request_name']][] = $this->field['info']['required'] ? 'required' : 'sometimes';
|
|
}
|
|
|
|
private function unique()
|
|
{
|
|
if ($this->field['info']['unique'] == true || $this->field['info']['unique'] == "true") {
|
|
$rule = new UniqueRule($this->getTable(), str_replace('.', '->', $this->field['request_name']));
|
|
$rule = in_array(SoftDeletes::class, array_keys(class_uses($this))) ? $rule->withoutTrashed() : $rule;
|
|
$rule = $this->getTable() === 'resources' ? $rule->where('model_id', $this->modelDocument->id) : $rule;
|
|
$this->rules[$this->field['request_name']][] = $rule->ignore($this->id);
|
|
}
|
|
|
|
}
|
|
|
|
private function type()
|
|
{
|
|
if ($this->isArray()) {
|
|
$this->rules[$this->field['request_name']][] = 'array';
|
|
}
|
|
|
|
if ($this->isResource()) {
|
|
$documentName = Str::studly(str_replace('resource:', '', $this->field['info']['type'])) . "Document";
|
|
$this->rules[$this->field['request_name']][] = new ExistsRule(new ('App\Documents\\' . $documentName));
|
|
}
|
|
|
|
|
|
if ($this->isSetting()) {
|
|
//todo:setting checking
|
|
}
|
|
|
|
}
|
|
|
|
private function primitiveType()
|
|
{
|
|
if ($this->isResource() || $this->isSetting()) {
|
|
return;
|
|
}
|
|
$name = $this->complexName();
|
|
|
|
$this->rules[$name][] = match ($this->field['info']['type']) {
|
|
'int', 'float' => 'numeric',
|
|
'string' => 'string',
|
|
'email' => 'email',
|
|
'boolean' => 'boolean',
|
|
'date' => 'date',
|
|
};
|
|
}
|
|
|
|
private function limit($rule)
|
|
{
|
|
$ruleValue = trim($this->field['info'][$rule]);
|
|
$rule = !empty($ruleValue) && $ruleValue != "null" ? ($rule . ':' . $ruleValue) : null;
|
|
|
|
if (empty($rule)) {
|
|
return;
|
|
}
|
|
|
|
if ($this->field['info']['type'] == 'date') {
|
|
$rule = str_replace(['min:', 'max:'], ['after_or_equal:', 'before_or_equal:'], $rule);
|
|
}
|
|
$name = $this->complexName();
|
|
$this->rules[$name][] = $rule;
|
|
}
|
|
|
|
private function in()
|
|
{
|
|
$in = $this->field['info']['in'];
|
|
if (empty($in) || $in == "null") {
|
|
return;
|
|
}
|
|
$in = is_array($in) ? $in : explode(',', $in);
|
|
$name = $this->complexName();
|
|
$this->rules[$name][] = 'in:' . implode(',', $in);
|
|
}
|
|
|
|
}
|