Skip to content

Commit

Permalink
config as object
Browse files Browse the repository at this point in the history
  • Loading branch information
TomK committed Aug 24, 2020
1 parent 97fa476 commit e21760a
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/Validators/RemoteValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Packaged\Validate\IValidator;
use Packaged\Validate\SerializableValidator;
use Packaged\Validate\Validation;
use stdClass;

class RemoteValidator extends AbstractSerializableValidator implements IDataSetValidator
{
Expand All @@ -24,8 +25,8 @@ public static function deserialize($configuration): SerializableValidator
$validator->_validators = $configuration->validators;
foreach($validator->_validators as $k => $v)
{
$validator->_validators[$k]['validator'] = Validation::fromJsonObject($v['validator']);
$validator->_validators[$k]['remoteValidator'] = Validation::fromJsonObject($v['remoteValidator']);
$validator->_validators[$k]->validator = Validation::fromJsonObject($v->validator);
$validator->_validators[$k]->remoteValidator = Validation::fromJsonObject($v->remoteValidator);
}
return $validator;
}
Expand All @@ -48,13 +49,13 @@ protected function _doValidate($values): Generator
$secondary = [];
foreach($this->_validators as $validator)
{
if($validator['remoteValidator']->isValid($values[$validator['remoteField']] ?? null))
if($validator->remoteValidator->isValid($values[$validator->remoteField] ?? null))
{
if(!isset($secondary[$this->_field]))
{
$secondary[$this->_field] = [];
}
$secondary[$this->_field][] = $validator['validator'];
$secondary[$this->_field][] = $validator->validator;
}
}

Expand All @@ -64,19 +65,22 @@ protected function _doValidate($values): Generator
foreach($validators as $validator)
{
/** @var IValidator $validator */
yield $validator->validate($fieldValue);
foreach($validator->validate($fieldValue) as $error)
{
yield $error;
}
}
}
}

//Validate field if remote validator passes
public function addValidator(IValidator $validator, string $remoteField, IValidator $remoteValidator)
{
$this->_validators[] = [
'validator' => $validator,
'remoteField' => $remoteField,
'remoteValidator' => $remoteValidator,
];
$config = new stdClass();
$config->validator = $validator;
$config->remoteField = $remoteField;
$config->remoteValidator = $remoteValidator;
$this->_validators[] = $config;
return $this;
}
}

0 comments on commit e21760a

Please sign in to comment.