TestValidator.php 1.06 KB
Newer Older
Suralc committed
1 2 3 4 5 6 7 8 9
<?php

namespace yiiunit\data\validators;


use yii\validators\Validator;

class TestValidator extends Validator
{
Alexander Makarov committed
10
	private $_validatedAttributes = [];
Suralc committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
	private $_setErrorOnValidateAttribute = false;

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

	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 isset($this->_validatedAttributes[$attr]) ? $this->_validatedAttributes[$attr] : 0;
	}

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

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