. */ namespace SP\Modules\Web\Forms; use SP\Domain\Client\Models\Client; use SP\Domain\Core\Acl\AclActionsInterface; use SP\Domain\Core\Exceptions\ValidationException; /** * Class ClientForm * * @package SP\Modules\Web\Forms */ final class ClientForm extends FormBase implements FormInterface { protected ?Client $clientData = null; /** * Validar el formulario * * @param int $action * @param int|null $id * * @return ClientForm|FormInterface * @throws ValidationException */ public function validateFor(int $action, ?int $id = null): FormInterface { if ($id !== null) { $this->itemId = $id; } switch ($action) { case AclActionsInterface::CLIENT_CREATE: case AclActionsInterface::CLIENT_EDIT: $this->analyzeRequestData(); $this->checkCommon(); break; } return $this; } /** * Analizar los datos de la petición HTTP * * @return void */ protected function analyzeRequestData(): void { $this->clientData = new Client( [ 'id' => $this->itemId, 'name' => $this->request->analyzeString('name'), 'description' => $this->request->analyzeString('description'), 'isglobal' => $this->request->analyzeBool('isglobal', false) ] ); } /** * @throws ValidationException */ protected function checkCommon(): void { if (!$this->clientData->getName()) { throw new ValidationException(__u('A client name needed')); } } public function getItemData(): ?Client { return $this->clientData; } }