<?php

/**
 * @link https://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license https://www.yiiframework.com/license/
 */

declare(strict_types=1);

namespace yiiunit\data\validators;

use yii\validators\Validator;

class TestValidator extends Validator
{
    private array $_validatedAttributes = [];
    private bool $_setErrorOnValidateAttribute = false;

    public function validateAttribute($object, $attribute): void
    {
        $this->markAttributeValidated($attribute);
        if ($this->_setErrorOnValidateAttribute == true) {
            $this->addError($object, $attribute, sprintf('%s##%s', $attribute, $object::class));
        }
    }

    protected function markAttributeValidated($attr, $increaseBy = 1)
    {
        if (!isset($this->_validatedAttributes[$attr])) {
            $this->_validatedAttributes[$attr] = 1;
        } else {
            $this->_validatedAttributes[$attr] = $this->_validatedAttributes[$attr] + $increaseBy;
        }
    }

    public function countAttributeValidations($attr)
    {
        return $this->_validatedAttributes[$attr] ?? 0;
    }

    public function isAttributeValidated($attr)
    {
        return isset($this->_validatedAttributes[$attr]);
    }

    public function enableErrorOnValidateAttribute(): void
    {
        $this->_setErrorOnValidateAttribute = true;
    }
}
