45 lines
1.1 KiB
PHP
45 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Rules\Sigtap;
|
|
|
|
use App\Models\Sigtap\Financiamento;
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
|
|
class FinanciamentoIsValidForCompetencia implements Rule
|
|
{
|
|
private $competenciaId;
|
|
|
|
public function __construct($competenciaId)
|
|
{
|
|
$this->competenciaId = $competenciaId;
|
|
}
|
|
|
|
public function passes($attribute, $value)
|
|
{
|
|
if (is_numeric($value)) {
|
|
return Financiamento::where('id', $value)
|
|
->where('competencia_id', $this->competenciaId)
|
|
->exists();
|
|
} else {
|
|
$response = true;
|
|
|
|
foreach ($value as $financiamentoId) {
|
|
$financiamento = Financiamento::where('id', $financiamentoId)
|
|
->where('competencia_id', $this->competenciaId)
|
|
->exists();
|
|
|
|
if ($financiamento === false) {
|
|
$response = false;
|
|
}
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
}
|
|
|
|
public function message()
|
|
{
|
|
return "Financiamento inválido para a competência informada.";
|
|
}
|
|
}
|