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.

111 lines
3.2 KiB

2 years ago
  1. <?php
  2. namespace App\Models\Traits;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\SoftDeletes;
  5. use Illuminate\Http\JsonResponse;
  6. use Illuminate\Support\Facades\Validator;
  7. use Illuminate\Validation\ValidationException;
  8. use Symfony\Component\HttpFoundation\Response;
  9. trait Validatable
  10. {
  11. private $validationState;
  12. public $autoValidation = true;
  13. public function autoValidate($validate = true)
  14. {
  15. $this->autoValidation = $validate;
  16. }
  17. protected static function bootValidatable()
  18. {
  19. static::saving(function (Model $model) {
  20. if (!isset($model->autoValidation) || (isset($model->autoValidation) && $model->autoValidation)) {
  21. $model->validate();
  22. //todo:add default values to request.
  23. }
  24. });
  25. if (collect(class_uses_recursive(static::class))->contains(SoftDeletes::class)) {
  26. static::restoring(function (Model $model) {
  27. $model->validationState = $model->autoValidation;
  28. $model->autoValidate(false);
  29. });
  30. static::restored(function (Model $model) {
  31. $model->autoValidate($model->validationState);
  32. });
  33. }
  34. }
  35. /**
  36. * Common Rules for prevent duplicate!
  37. *
  38. * @return array
  39. */
  40. public abstract function rules(): array;
  41. public function validate($fields = [], $abort = true)
  42. {
  43. $modelDocument = app('modelDocument');
  44. $rules = $this->rules();
  45. $rules = array_merge($rules, $this->createValidations());
  46. $rules = array_merge($rules, $this->createValidations('category_data'));
  47. $validator = Validator::make($this->toArray(), $rules);
  48. if ($abort && $validator->fails()) {
  49. throw new ValidationException($validator,
  50. new JsonResponse([
  51. "message" => "The given data was invalid.",
  52. "errors" => $validator->errors()->getMessages()
  53. ], Response::HTTP_UNPROCESSABLE_ENTITY)
  54. );
  55. }
  56. //todo:shite code
  57. $fields = json_decode($this->attributes['fields'] ?? '');
  58. collect($modelDocument?->fields)->filter(fn($f) => !empty($f['info']['default']))
  59. ->each(function ($field) use (&$fields) {
  60. $fields->{$field['name']} = $fields->{$field['name']} ?? $field['info']['default'];
  61. });
  62. if ($this->attributes['fields'] ?? false){
  63. $this->attributes['fields'] = json_encode($fields);
  64. }
  65. return $abort ? $this : true;
  66. }
  67. public function getLoads()
  68. {
  69. return [];
  70. }
  71. public function allowLoad()
  72. {
  73. $includes = request()->filled('include') ? explode(',', trim(request()->include, ',')) : [];
  74. $getLoad = $this->getLoads();
  75. $allow_load = [];
  76. foreach ($includes as $include) {
  77. $include = trim($include);
  78. if (in_array($include, $getLoad, true)) {
  79. array_push($allow_load, $include);
  80. } else {
  81. abort(Response::HTTP_BAD_REQUEST, "Requested include $include are not allowed");
  82. }
  83. }
  84. $this->load($allow_load);
  85. return $this;
  86. }
  87. }