<?php

namespace App\Modules\CategoryProvider\Domain;

use App\Modules\Shared\Domain\QueryBuilder\Operator\OperatorBuilder;
use App\Modules\Shared\Domain\Storage\Criteria\ConcreteStorageCriteria;

class ExistsCategoryProvider extends ConcreteStorageCriteria
{
    public function __construct(
        IdCategoryProvider $idCategoryProvider
    ) {
        $op = new OperatorBuilder();
        parent::__construct(
            CategoryProvider::class,
            operator: $op->equal('`Id_CategoryProvider`', $idCategoryProvider->value()),
            limit: 1
        );
    }

    public function analize($data)
    {
        if (!count($data)) {
            throw new CategoryProviderNotExists();
        }
        return CategoryProvider::fromDTO($data[0]);
    }
}
