StoreUserAppSettings.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Http\Requests;
  3. use Illuminate\Foundation\Http\FormRequest;
  4. class StoreUserAppSettings extends FormRequest
  5. {
  6. /**
  7. * Determine if the user is authorized to make this request.
  8. */
  9. public function authorize(): bool
  10. {
  11. if(!$this->user() || $this->user()->status) {
  12. return false;
  13. }
  14. return true;
  15. }
  16. /**
  17. * Get the validation rules that apply to the request.
  18. *
  19. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
  20. */
  21. public function rules(): array
  22. {
  23. return [
  24. 'common' => 'required|array',
  25. 'common.timelines.show_public' => 'required|boolean',
  26. 'common.timelines.show_network' => 'required|boolean',
  27. 'common.timelines.hide_likes_shares' => 'required|boolean',
  28. 'common.media.hide_public_behind_cw' => 'required|boolean',
  29. 'common.media.always_show_cw' => 'required|boolean',
  30. 'common.media.show_alt_text' => 'required|boolean',
  31. 'common.appearance.links_use_in_app_browser' => 'required|boolean',
  32. 'common.appearance.theme' => 'required|string|in:light,dark,system',
  33. ];
  34. }
  35. /**
  36. * Prepare inputs for validation.
  37. *
  38. * @return void
  39. */
  40. protected function prepareForValidation()
  41. {
  42. $this->merge([
  43. 'common' => array_merge(
  44. $this->input('common'),
  45. [
  46. 'timelines' => [
  47. 'show_public' => $this->toBoolean($this->input('common.timelines.show_public')),
  48. 'show_network' => $this->toBoolean($this->input('common.timelines.show_network')),
  49. 'hide_likes_shares' => $this->toBoolean($this->input('common.timelines.hide_likes_shares'))
  50. ],
  51. 'media' => [
  52. 'hide_public_behind_cw' => $this->toBoolean($this->input('common.media.hide_public_behind_cw')),
  53. 'always_show_cw' => $this->toBoolean($this->input('common.media.always_show_cw')),
  54. 'show_alt_text' => $this->toBoolean($this->input('common.media.show_alt_text')),
  55. ],
  56. 'appearance' => [
  57. 'links_use_in_app_browser' => $this->toBoolean($this->input('common.appearance.links_use_in_app_browser')),
  58. 'theme' => $this->input('common.appearance.theme'),
  59. ]
  60. ]
  61. )
  62. ]);
  63. }
  64. /**
  65. * Convert to boolean
  66. *
  67. * @param $booleable
  68. * @return boolean
  69. */
  70. private function toBoolean($booleable)
  71. {
  72. return filter_var($booleable, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
  73. }
  74. }