51 lines
1.2 KiB
PHP
51 lines
1.2 KiB
PHP
<?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.";
|
|
}
|
|
}
|