<?php

namespace AutoAdmin\Bundle\Menu\Validator;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Json;
use Symfony\Component\Validator\Constraints\JsonValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

class ValidJsonValidator extends JsonValidator
{
    public function validate(mixed $value, Constraint $constraint): void
    {
        if (!$constraint instanceof ValidJson) {
            throw new UnexpectedTypeException($constraint, ValidJson::class);
        }

        if (!is_array($value)) {
            return;
        }

        if (isset($value['error'])) {
            $this->context->buildViolation($constraint->message)
                ->setCode(Json::INVALID_JSON_ERROR)
                ->addViolation();
        }
    }
}
