File manager - Edit - /var/www/html/portal/app/Http/Requests/ContratoRequest.php
Back
<?php namespace App\Http\Requests; use App\Helpers\ContratosAccessHelper; use App\Models\Contratos\Contrato; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Support\Str; use Illuminate\Validation\Rule; use Illuminate\Validation\Validator; class ContratoRequest extends FormRequest { protected function prepareForValidation(): void { $favorecido = preg_replace('/\D+/', '', (string) $this->input('con_favorecido', '')); $user = auth()->user(); $this->merge([ 'con_favorecido' => $favorecido !== '' ? Str::limit($favorecido, 14, '') : null, 'con_secretaria' => ContratosAccessHelper::hasGlobalAccess() ? $this->input('con_secretaria') : $user?->structure_id, ]); } public function authorize(): bool { return true; } public function rules(): array { return [ 'con_numero' => ['nullable', 'string', 'max:50'], 'con_ano' => ['nullable', 'string', 'size:4'], 'con_titulo' => ['required', 'string', 'max:1024'], 'con_modalidade' => ['nullable', 'integer', Rule::in(array_keys(Contrato::MODALIDADES))], 'con_favorecido' => ['nullable', 'string', 'max:14'], 'fav_nome' => ['nullable', 'string', 'max:200'], 'con_dataini' => ['nullable', 'string', 'max:10'], 'con_datafim' => ['nullable', 'string', 'max:10'], 'con_valor' => ['nullable', 'string', 'max:20'], 'con_processo' => ['nullable', 'string', 'max:50'], 'licitacao_procedimento_id' => ['nullable', 'integer', 'exists:licitacoes_procedimentos,id'], 'con_secretaria' => [ContratosAccessHelper::hasGlobalAccess() ? 'nullable' : 'required', 'uuid', 'exists:structures,id'], 'con_secretaria_gestora' => ['nullable', 'uuid', 'exists:structures,id'], 'con_datapub' => ['nullable', 'string', 'max:10'], 'con_dataassinatura' => ['nullable', 'string', 'max:10'], 'con_fiscais' => ['nullable', 'string'], 'con_status' => ['nullable', 'integer', Rule::in(array_keys(Contrato::STATUS))], 'con_covid' => ['nullable', 'string', 'max:1'], ]; } /** * @return array<string, string> */ public function messages(): array { return [ 'con_titulo.required' => __('* O título do contrato é obrigatório.'), 'con_titulo.max' => __('* O título não pode ultrapassar 1024 caracteres.'), 'con_ano.size' => __('* O ano deve conter 4 dígitos.'), 'con_modalidade.in' => __('* A modalidade informada é inválida.'), 'con_status.in' => __('* A situação informada é inválida.'), 'con_secretaria.required' => __('* A secretaria é obrigatória.'), 'con_secretaria.exists' => __('* A secretaria informada é inválida.'), 'con_secretaria_gestora.exists' => __('* A secretaria gestora informada é inválida.'), 'licitacao_procedimento_id.exists' => __('* A licitação vinculada é inválida.'), ]; } public function withValidator(Validator $validator): void { $validator->after(function (Validator $validator) { $cnpj = (string) ($this->input('con_favorecido') ?? ''); if ($cnpj !== '' && !$this->isValidCnpj($cnpj)) { $validator->errors()->add('con_favorecido', __('* Informe um CNPJ válido.')); } }); } private function isValidCnpj(string $cnpj): bool { $cnpj = preg_replace('/\D+/', '', $cnpj); if (strlen($cnpj) !== 14) { return false; } if (preg_match('/^(\d)\1{13}$/', $cnpj)) { return false; } for ($t = 12; $t < 14; $t++) { $sum = 0; $weight = $t - 7; for ($i = 0; $i < $t; $i++) { $sum += (int) $cnpj[$i] * $weight; $weight = ($weight === 2) ? 9 : $weight - 1; } $digit = ((10 * $sum) % 11) % 10; if ((int) $cnpj[$t] !== $digit) { return false; } } return true; } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.01 |
proxy
|
phpinfo
|
Settings