<?php

namespace Application\Service\Extension\ValidatorV2\validators;

use Application\Service\Extension\ValidatorV2\ValidatorInterface;

abstract class BaseValidator implements ValidatorInterface
{
    protected string $message = '';

    protected array $error = [];
    /**
     * Validation attributes.
     * @var array
     */
    protected array $validationAttributes = [];

    public function setValidationAttributes($validationAttributes)
    {
        if (is_array($validationAttributes)) {
            $this->validationAttributes = $validationAttributes;
        } elseif (is_string($validationAttributes)) {
            $this->validationAttributes[] = $validationAttributes;
        }
    }

    public function isValid(&$value)
    {
        foreach ($this->validationAttributes as $validationAttribute) {
            $this->valid($value[$validationAttribute] ?? null, $validationAttribute);
            if ($this->hasError()) {
                return $this->getError();
            }
        }

        return true;
    }

    public function valid($value, $attribute)
    {
        return null;
    }

    public function hasError(): bool
    {
        return (bool) $this->error;
    }

    public function getError()
    {
        return $this->error;
    }

    public function message(string $message)
    {
        $this->message = $message;
        return $this;
    }
}