method()) { case 'POST': { return $this->getPostRules(); } case 'PUT': { return $this->getPutRules(); } default: { return []; } } } private function getPostRules() { return [ 'nome_fantasia' => [ 'required', 'string', 'between:3,255', 'unique:\App\Models\Cadastro\Estabelecimento\Unidade,nome_fantasia,NULL,id,deleted_at,NULL' ], 'razao_social' => [ 'required', 'string', 'between:3,255', ], 'cnpj' => [ 'nullable', 'digits:14', new Cnpj(['attribute' => 'CNPJ']), ], 'tipo_id' => [ 'required', 'integer' ], 'esfera_administrativa_id' => [ 'required', 'integer' ], 'natureza_juridica_id' => [ 'required', 'integer' ], 'pertence_sus' => [ 'required', 'boolean' ], 'unidade_referencia' => [ 'required', 'boolean' ], 'unidade_prestadora' => [ 'required', 'boolean' ], 'gestor_id' => [ 'nullable', 'integer' ], 'cnes' => [ 'nullable', 'string', 'between:7,7', ], 'email' => [ 'nullable', 'email:rfc,dns', 'max:180' ], 'site' => [ 'nullable', 'string', 'max:180' ], 'infraestruturas' => [ 'required', 'array' ], 'infraestruturas.*.infraestrutura_id' => [ 'nullable', 'integer' ], 'infraestruturas.*.principal' => [ 'required', 'boolean' ], 'infraestruturas.*.nome' => [ 'nullable', 'required_if:infraestruturas.*.principal,0', 'string', 'between:3,255', ], 'infraestruturas.*.telefone' => [ 'required', 'digits_between:10,11' ], 'infraestruturas.*.endereco' => [ 'required', 'array' ], 'infraestruturas.*.endereco.logradouro_id' => [ 'required', 'integer' ], 'infraestruturas.*.endereco.numero' => [ 'nullable', 'digits_between:1,6', ], 'infraestruturas.*.endereco.complemento' => [ 'nullable', 'string', 'not_regex:' . ImportacaoCns::regexValidacao(), new CnsTermoValido(['attribute' => 'Complemento']) ], 'infraestruturas.*.endereco.zona_id' => [ 'required', 'integer', ], 'infraestruturas.*.centros_custo' => [ 'array' ], 'infraestruturas.*.centros_custo.*.setor_id' => [ 'required', 'integer' ], 'infraestruturas.*.centros_custo.*.salas' => [ 'array' ], 'infraestruturas.*.centros_custo.*.salas.*.nome' => [ 'required', 'string', 'between:3,30', ] ]; } private function getPutRules() { $custom = []; if (is_array($this->infraestruturas)) { foreach ($this->infraestruturas as $i => $infraestrutura) { if (isset($infraestrutura['centros_custo'])) { foreach ($infraestrutura['centros_custo'] as $j => $centroCusto) { $custom["infraestruturas.{$i}.centros_custo.{$j}.setor_id"] = [ 'required', 'integer', Rule::unique('\App\Models\Cadastro\Estabelecimento\CentroCusto', 'setor_id') ->where(function ($query) use ($infraestrutura) { return $query ->where('infraestrutura_id', $infraestrutura['id'] ?? null) ->whereNull('deleted_at'); }) ->ignore($centroCusto['id'] ?? null) ]; if (isset($centroCusto['salas'])) { foreach ($centroCusto['salas'] as $k => $sala) { $custom["infraestruturas.{$i}.centros_custo.{$j}.salas.{$k}.nome"] = [ 'required', 'string', 'between:3,30', Rule::unique('\App\Models\Cadastro\Estabelecimento\Sala', 'nome') ->where(function ($query) use ($centroCusto) { return $query ->where('centro_custo_id', $centroCusto['id'] ?? null) ->whereNull('deleted_at'); }) ->ignore($sala['id'] ?? null) ]; } } } } } } return array_merge([ 'nome_fantasia' => [ 'required', 'string', 'between:3,255', "unique:\App\Models\Cadastro\Estabelecimento\Unidade,nome_fantasia,{$this->unidade},id,deleted_at,NULL" ], 'razao_social' => [ 'required', 'string', 'between:3,255', ], 'cnpj' => [ 'nullable', 'digits:14', new Cnpj(['attribute' => 'CNPJ']), ], 'tipo_id' => [ 'required', 'integer' ], 'esfera_administrativa_id' => [ 'required', 'integer' ], 'natureza_juridica_id' => [ 'required', 'integer' ], 'pertence_sus' => [ 'required', 'boolean' ], 'unidade_referencia' => [ 'required', 'boolean' ], 'unidade_prestadora' => [ 'required', 'boolean' ], 'gestor_id' => [ 'nullable', 'integer' ], 'cnes' => [ 'nullable', 'string', 'between:7,7', ], 'email' => [ 'nullable', 'email:rfc,dns', 'max:180' ], 'site' => [ 'nullable', 'string', 'max:180' ], 'infraestruturas' => [ 'required', 'array' ], 'infraestruturas.*.id' => [ 'nullable', 'integer' ], 'infraestruturas.*.principal' => [ 'required', 'boolean' ], 'infraestruturas.*.nome' => [ 'nullable', 'required_if:infraestruturas.*.principal,0', 'string', 'between:3,255', ], 'infraestruturas.*.telefone' => [ 'required', 'digits_between:10,11' ], 'infraestruturas.*.endereco' => [ 'required', 'array' ], 'infraestruturas.*.endereco.logradouro_id' => [ 'required', 'integer' ], 'infraestruturas.*.endereco.numero' => [ 'nullable', 'integer', 'max:999999', ], 'infraestruturas.*.endereco.complemento' => [ 'nullable', 'string', 'not_regex:' . ImportacaoCns::regexValidacao(), new CnsTermoValido(['attribute' => 'Complemento']) ], 'infraestruturas.*.endereco.zona_id' => [ 'required', 'integer', ], 'infraestruturas.*.centros_custo' => [ 'array' ], 'infraestruturas.*.centros_custo.*.id' => [ 'nullable', 'integer' ], 'infraestruturas.*.centros_custo.*.salas' => [ 'array' ], 'infraestruturas.*.centros_custo.*.salas.*.id' => [ 'nullable', 'integer' ] ], $custom); } public function messages() { return [ 'nome_fantasia.unique' => 'Já existe uma unidade com o Nome fantasia informado.', ]; } }