$this->isAuthorized()); } /** * @throws Exception */ protected function getRequestRules(): array { $request = $this->getFormRequest(); return method_exists($request, 'rules') ? $request->rules() : []; } /** * Get the messages from the form request. * * @throws Exception */ protected function getRequestMessages(): array { $messages = $this->getFormRequest()->messages(); return ! empty($messages) ? $messages : []; } /** * This is a near-copy of passesAuthorization on a standard * FormRequest. It has been adjusted for Livewire. * * @return bool * * @throws Exception */ protected function isAuthorized(): bool { $request = $this->getFormRequest(); if (method_exists($request, 'authorize')) { return app()->call([$request, 'authorize']); } return true; } /** * Get the form request from the Livewire component. * * @return FormRequest * * @throws Exception */ protected function getFormRequest(): FormRequest { if (! property_exists($this, 'request')) { throw new Exception('Please specify a `request` parameter and set it to a FormRequest'); } if (! new $this->request instanceof FormRequest) { throw new Exception("$this->request is not a valid `FormRequest`."); } /** @var FormRequest $request */ $request = $this->request::createFrom(request()); $request->attributes->set('__instance', $this); return $request; } /** * @throws Exception */ protected function getRules(): array { $rules = $this->getRequestRules(); if (! empty($rules)) { return $rules; } if (method_exists($this, 'rules')) { return $this->rules(); } if (property_exists($this, 'rules')) { return $this->rules; } return []; } /** * @throws Exception */ protected function getMessages(): array { $messages = $this->getRequestMessages(); if (! empty($messages)) { return $messages; } if (method_exists($this, 'messages')) { return $this->messages(); } if (property_exists($this, 'messages')) { return $this->messages; } return []; } }