* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ namespace PrestaShop\PrestaShop\Core\ConstraintValidator; use PrestaShop\PrestaShop\Core\ConstraintValidator\Constraints\DefaultLanguage; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; /** * Class DefaultLanguageValidator is responsilbe for doing the actual validation under DefaultLanguage constraint. */ class DefaultLanguageValidator extends ConstraintValidator { /** * @var int */ private $defaultLanguageId; /** * @param int $defaultLanguageId */ public function __construct($defaultLanguageId) { $this->defaultLanguageId = $defaultLanguageId; } /** * {@inheritdoc} */ public function validate($value, Constraint $constraint) { if (!$constraint instanceof DefaultLanguage) { throw new UnexpectedTypeException($constraint, DefaultLanguage::class); } if (!is_array($value)) { throw new UnexpectedTypeException($value, 'array'); } if (empty($value[$this->defaultLanguageId])) { $this->context->buildViolation($constraint->message) ->setTranslationDomain('Admin.Notifications.Error') ->setParameter( '%field_name%', $this->context->getObject() ? $this->context->getObject()->getName() : '' ) ->addViolation() ; } } }