<?php

namespace Modules\Stock\Validator;

use Entity\Model\Srubcomp;
use Exception;
use Helpers\Trad\Trad;
use Helpers\Validator\Validator;

class RubriqueValidator extends Validator
{

    /**
     * @param array<string,mixed> $rubrique
     * @return $this
     * @throws Exception
     */
    public function validateCode(array $rubrique, bool $checkUnicity = true): self
    {
        $nomChamp = $this->trad->t('Code');
        $this->isCode($nomChamp, $rubrique['CODE'], 0, 30);
        if ((new Srubcomp($this->pdo))->find($rubrique['CODE']) && $checkUnicity) {
            $this->setError(Trad::strBuild($this->trad->t('Le code @1@ est déjà utilisé'), $rubrique['CODE']));
        }

        return $this;
    }

    /**
     * @param array<string,mixed> $rubrique
     * @return $this
     */
    public function validateLibelle(array $rubrique): self
    {
        $nomChamp = $this->trad->t('Libellé');
        $this->isObligatoire($rubrique['LIBELLE'], $nomChamp);
        $this->isStringAvecEspace($nomChamp, $rubrique['LIBELLE'], 0, 300);

        return $this;
    }
}
