149 lines
4.6 KiB
PHP
149 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Cadastro\Localizacao;
|
|
|
|
use App\Http\Requests\FormRequest;
|
|
use App\Rules\CnsTermoValido;
|
|
use App\Utils\DataUtil;
|
|
use App\Utils\ImportacaoCns;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class LogradouroRequest extends FormRequest
|
|
{
|
|
private $regexCep = '/[^0-9]/';
|
|
private $tamanhoNome = '3,250';
|
|
|
|
public function authorize()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
protected function prepareForValidation()
|
|
{
|
|
$this->merge([
|
|
'logradouro_nome' => html_entity_decode($this->logradouro_nome, ENT_QUOTES, 'UTF-8'),
|
|
]);
|
|
}
|
|
|
|
public function rules()
|
|
{
|
|
switch ($this->method()) {
|
|
|
|
case 'POST': {
|
|
|
|
return $this->getPostRules();
|
|
}
|
|
case 'PUT': {
|
|
|
|
return $this->getPutRules();
|
|
}
|
|
default: {
|
|
|
|
return [];
|
|
}
|
|
}
|
|
}
|
|
|
|
private function getPostRules()
|
|
{
|
|
if (DataUtil::getBooleanValueFromStringBoolean($this->search_by_base_endereco_nacional) === true) {
|
|
$rules = [
|
|
'municipio_id' => ['required', 'integer'],
|
|
'logradouro_nome' => [
|
|
'required',
|
|
'string',
|
|
'between:' . $this->tamanhoNome,
|
|
'not_regex:' . ImportacaoCns::regexValidacao(),
|
|
new CnsTermoValido(['attribute' => 'Nome']),
|
|
],
|
|
'logradouro_cep' => [
|
|
'required',
|
|
'not_regex:' . $this->regexCep,
|
|
],
|
|
];
|
|
} else {
|
|
$rules = [
|
|
'municipio_id' => ['required', 'integer'],
|
|
'bairro_id' => ['required', 'integer'],
|
|
'tipo_id' => ['required', 'integer'],
|
|
'nome' => [
|
|
'required',
|
|
'string',
|
|
'between:' . $this->tamanhoNome,
|
|
'not_regex:' . ImportacaoCns::regexValidacao(),
|
|
new CnsTermoValido(['attribute' => 'Nome']),
|
|
Rule::unique('\App\Models\Cadastro\Localizacao\Logradouro')->where(function ($query) {
|
|
|
|
return $query
|
|
->where('municipio_id', $this->municipio_id)
|
|
->where('bairro_id', $this->bairro_id)
|
|
->where('tipo_id', $this->tipo_id)
|
|
->whereNull('deleted_at');
|
|
}),
|
|
],
|
|
'cep' => [
|
|
'required',
|
|
'not_regex:' . $this->regexCep,
|
|
],
|
|
];
|
|
}
|
|
|
|
return $rules;
|
|
}
|
|
|
|
private function getPutRules()
|
|
{
|
|
if (DataUtil::getBooleanValueFromStringBoolean($this->search_by_base_endereco_nacional) === true) {
|
|
$rules = [
|
|
'municipio_id' => ['required', 'integer'],
|
|
'nome' => [
|
|
'required',
|
|
'string',
|
|
'between:' . $this->tamanhoNome,
|
|
'not_regex:' . ImportacaoCns::regexValidacao(),
|
|
new CnsTermoValido(['attribute' => 'Nome']),
|
|
],
|
|
'cep' => [
|
|
'required',
|
|
'not_regex:' . $this->regexCep,
|
|
],
|
|
];
|
|
} else {
|
|
|
|
$rules = [
|
|
'municipio_id' => ['required', 'integer'],
|
|
'bairro_id' => ['required', 'integer'],
|
|
'tipo_id' => ['required', 'integer'],
|
|
'nome' => [
|
|
'required',
|
|
'string',
|
|
'between:' . $this->tamanhoNome,
|
|
'not_regex:' . ImportacaoCns::regexValidacao(),
|
|
new CnsTermoValido(['attribute' => 'Nome']),
|
|
Rule::unique('\App\Models\Cadastro\Localizacao\Logradouro')->where(function ($query) {
|
|
|
|
return $query
|
|
->where('municipio_id', $this->municipio_id)
|
|
->where('bairro_id', $this->bairro_id)
|
|
->where('tipo_id', $this->tipo_id)
|
|
->whereNull('deleted_at');
|
|
})->ignore($this->logradouro)
|
|
],
|
|
'cep' => [
|
|
'required',
|
|
'not_regex:' . $this->regexCep,
|
|
],
|
|
];
|
|
}
|
|
|
|
return $rules;
|
|
}
|
|
|
|
public function messages()
|
|
{
|
|
return [
|
|
'nome.unique' => 'Um Logradouro com o mesmo Tipo, Bairro, Município e Nome já foi inserido anteriormente.'
|
|
];
|
|
}
|
|
}
|