Skip to content

Commit

Permalink
馃敤 CollectionRule enhancement #29
Browse files Browse the repository at this point in the history
  • Loading branch information
elie29 committed Dec 6, 2019
1 parent 696d7b4 commit 520a2eb
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/Rule/CollectionRule.php
Expand Up @@ -85,32 +85,32 @@ protected function isValid(): int
}

$validatedContext = [];
foreach ($collection as $data) {
$validatedData = [];
foreach ($this->rules as $rule) {
$class = $this->resolve($rule, $data);
// Apply each rule to all data keys
foreach ($this->rules as $rule) {
$class = $this->resolve($rule);
foreach ($collection as $k => $data) {
$class->setValue($data[$class->getKey()] ?? null);
if ($class->validate() === RuleInterface::ERROR) {
$this->error = $class->getError();
return RuleInterface::ERROR;
}
$validatedData[$class->getKey()] = $class->getValue();
$validatedContext[$k][$class->getKey()] = $class->getValue();
}
$validatedContext[] = $validatedData;
}

$this->value = $validatedContext;

return RuleInterface::VALID;
}

protected function resolve(array $rule, $data): RuleInterface
protected function resolve(array $rule): RuleInterface
{
// The first element must be the key context
$key = $rule[0];
// The second element must be the class validator name
$class = $rule[1];

return new $class($key, $data[$key] ?? null, $rule);
return new $class($key, null, $rule);
}

public function getValue()
Expand Down

0 comments on commit 520a2eb

Please sign in to comment.