Skip to content

Commit

Permalink
Fix CompositeValidator linting issues in manual mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Horat1us committed Oct 26, 2018
1 parent 1fd8295 commit 8e7bc11
Showing 1 changed file with 40 additions and 22 deletions.
62 changes: 40 additions & 22 deletions src/Validators/CompositeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,25 @@

namespace Horat1us\Yii\Validators;

use yii\base\InvalidConfigException;
use Yii;
use yii\base\Model;
use yii\base;
use yii\helpers\ArrayHelper;
use yii\validators\Validator;

/**
* Class CompositeValidator
* @package Horat1us\Yii\Validators
*/
class CompositeValidator extends Validator
{
/**
* @var array
*/
/** @var array */
public $rules = [];
/**
* @var boolean
*/

/** @var bool */
public $allowMessageFromRule = true;
/**
* @var Validator[]
*/
private $_validators = [];

/** @var Validator[] */
private $validators = [];

/**
* @inheritdoc
Expand All @@ -43,20 +42,21 @@ public function init()

/**
* @param integer $index
* @param Model|null $model
* @param base\Model|null $model
* @return Validator
* @throws base\InvalidConfigException
*/
private function getValidator($index, $model = null)
{
if (!isset($this->_validators[$index])) {
$this->_validators[$index] = $this->createEmbeddedValidator($this->rules[$index], $model);
if (!isset($this->validators[$index])) {
$this->validators[$index] = $this->createEmbeddedValidator($this->rules[$index], $model);
}
return $this->_validators[$index];
return $this->validators[$index];
}

/**
* @param array $rule
* @param Model|null $model
* @param base\Model|null $model
* @throws \yii\base\InvalidConfigException
* @return Validator validator instance
*/
Expand All @@ -66,16 +66,18 @@ private function createEmbeddedValidator($rule, $model)
return $rule;
} elseif (is_array($rule) && isset($rule[0]) && isset($rule[1])) {
if (!is_object($model)) {
$model = new Model(); // mock up context model
$model = new base\Model(); // mock up context model
}
return Validator::createValidator($rule[1], $model, $this->attributes, array_slice($rule, 2));
} else {
throw new InvalidConfigException('Invalid validation rule: a rule must be an array specifying validator type.');
throw new base\InvalidConfigException(
'Invalid validation rule: a rule must be an array specifying validator type.'
);
}
}

/**
* @param Model $model
* @param base\Model $model
* @param string $attribute
* @param Validator $validator
* @param array $originalErrors
Expand All @@ -89,7 +91,14 @@ private function validateInternal(&$model, &$attribute, &$validator, &$originalE
$items = ArrayHelper::getValue($value, $current[0]);
if ($items) {
foreach ($items as $i => $item) {
$this->validateInternal($model, $attribute, $validator, $originalErrors, $value, "{$current[0]}.{$i}{$current[1]}");
$this->validateInternal(
$model,
$attribute,
$validator,
$originalErrors,
$value,
"{$current[0]}.{$i}{$current[1]}"
);
}
}
} else {
Expand Down Expand Up @@ -118,6 +127,7 @@ private function validateInternal(&$model, &$attribute, &$validator, &$originalE

/**
* @inheritdoc
* @throws base\InvalidConfigException
*/
public function validateAttribute($model, $attribute)
{
Expand All @@ -127,14 +137,22 @@ public function validateAttribute($model, $attribute)
$originalErrors = $model->getErrors($attribute);
$targets = (array)$rule[0];
foreach ($targets as $target) {
$this->validateInternal($model, $attribute, $validator, $originalErrors, $value, $target);
$this->validateInternal(
$model,
$attribute,
$validator,
$originalErrors,
$value,
$target
);
}
$model->$attribute = $value;
}
}

/**
* @inheritdoc
* @throws base\InvalidConfigException
*/
protected function validateValue($value)
{
Expand Down

0 comments on commit 8e7bc11

Please sign in to comment.