CreateOrUpdateRequest.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Http\Requests\API\Library;
  3. use App\Http\Requests\RuleHelper;
  4. use App\Models\Enums\LibraryACL;
  5. use App\Models\Enums\LibraryType;
  6. use Illuminate\Foundation\Http\FormRequest;
  7. use Illuminate\Validation\Rule;
  8. use Illuminate\Validation\Rules\Enum;
  9. class CreateOrUpdateRequest extends FormRequest
  10. {
  11. use RuleHelper;
  12. /**
  13. * Determine if the user is authorized to make this request.
  14. */
  15. public function authorize(): bool
  16. {
  17. return true;
  18. }
  19. /**
  20. * Get the validation rules that apply to the request.
  21. *
  22. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  23. */
  24. public function rules(): array
  25. {
  26. $rules = [
  27. 'name' => 'required',
  28. 'type' => [
  29. 'required',
  30. new Enum(LibraryType::class)
  31. ],
  32. 'acl' => [
  33. 'required',
  34. new Enum(LibraryACL::class),
  35. ],
  36. 'whitelist' => $this->usersCompanyRules(),
  37. ];
  38. switch ($this->request->get("type")) {
  39. case LibraryType::ASSET->value:
  40. $rules['asset_id'] = [
  41. 'required',
  42. Rule::exists('assets', 'id')->where($this->userCompanyWhere()),
  43. ];
  44. break;
  45. case LibraryType::PROJECT->value:
  46. $rules['project_id'] = [
  47. 'required',
  48. Rule::exists('projects', 'id')->where($this->userCompanyWhere()),
  49. ];
  50. break;
  51. }
  52. return $rules;
  53. }
  54. }