This commit is contained in:
50
api/app/Rules/Sigtap/ClassificacaoIsValidForServico.php
Normal file
50
api/app/Rules/Sigtap/ClassificacaoIsValidForServico.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules\Sigtap;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ClassificacaoIsValidForServico implements Rule
|
||||
{
|
||||
private $servicoId;
|
||||
|
||||
public function __construct($servicoId)
|
||||
{
|
||||
$this->servicoId = $servicoId;
|
||||
}
|
||||
|
||||
public function passes($attribute, $value)
|
||||
{
|
||||
return self::isValid($this->servicoId, $value);
|
||||
}
|
||||
|
||||
public static function isValid($servicoId, $value)
|
||||
{
|
||||
$servicoIds = collect($servicoId)
|
||||
->map(fn($s) => $s->id ?? null)
|
||||
->filter()
|
||||
->unique()
|
||||
->values();
|
||||
|
||||
$classificacaoIds = collect($value)
|
||||
->flatMap(fn($sub) => collect($sub)->pluck('id'))
|
||||
->filter()
|
||||
->unique()
|
||||
->values();
|
||||
|
||||
if ($servicoIds->isEmpty() || $classificacaoIds->isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return DB::table('sigtap.classificacao')
|
||||
->whereIn('id', $classificacaoIds->all())
|
||||
->whereIn('servico_id', $servicoIds->all())
|
||||
->exists();
|
||||
}
|
||||
|
||||
public function message()
|
||||
{
|
||||
return "Classificação inválida para para o serviço informado.";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user