-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed previously introduced bug, add integration test
- Loading branch information
Showing
3 changed files
with
50 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,6 @@ | |
|
||
namespace Respect\Validation\Exceptions; | ||
|
||
use function count; | ||
use function current; | ||
|
||
/** | ||
* @author Alexandre Gomes Gaigalas <[email protected]> | ||
* @author Henrique Moody <[email protected]> | ||
|
@@ -38,27 +35,16 @@ final class EachException extends NestedValidationException | |
*/ | ||
public function getMessages(array $templates = []): array | ||
{ | ||
$messages = [$this->getId() => $this->renderMessage($this, $templates)]; | ||
$messages = []; | ||
$count = -1; | ||
foreach ($this->getChildren() as $exception) { | ||
$count++; | ||
$id = $exception->getId(); | ||
if (!$exception instanceof NestedValidationException) { | ||
$messages[$id . '.' . $count] = $this->renderMessage( | ||
$exception, | ||
$this->findTemplates($templates, $this->getId()) | ||
); | ||
continue; | ||
} | ||
|
||
$messages[$id . '.' . $count] = $exception->getMessages( | ||
$this->findTemplates($templates, $id, $this->getId()) | ||
$messages[$id . '.' . $count] = $this->renderMessage( | ||
$exception, | ||
$this->findTemplates($templates, $this->getId()) | ||
); | ||
if (count($messages[$id . '.' . $count]) > 1) { | ||
continue; | ||
} | ||
|
||
$messages[$id . '.' . $count] = current($messages[$exception->getId()]); | ||
} | ||
|
||
return $messages; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--CREDITS-- | ||
Alexandre Gomes Gaigalas <[email protected]> | ||
--FILE-- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
use Respect\Validation\Exceptions\NestedValidationException; | ||
use Respect\Validation\Validator; | ||
|
||
$cars = [ | ||
['manufacturer' => 'Honda', 'model' => 'Accord'], | ||
['manufacturer' => 'Toyota', 'model' => 'Rav4'], | ||
['manufacturer' => 'Ford', 'model' => 'notarealcar'], | ||
['manufacturer' => 'Honda', 'model' => 'not valid'], | ||
]; | ||
|
||
try { | ||
Validator::arrayType()->each( | ||
Validator::oneOf( | ||
Validator::key('manufacturer', Validator::equals('Honda')) | ||
->key('model', Validator::in(['Accord', 'Fit'])), | ||
Validator::key('manufacturer', Validator::equals('Toyota')) | ||
->key('model', Validator::in(['Rav4', 'Camry'])), | ||
Validator::key('manufacturer', Validator::equals('Ford')) | ||
->key('model', Validator::in(['F150', 'Bronco'])) | ||
) | ||
)->assert($cars); | ||
} catch (NestedValidationException $e) { | ||
var_dump($e->getMessages()); | ||
} | ||
|
||
|
||
?> | ||
--EXPECT-- | ||
array(1) { | ||
["each"]=> | ||
array(2) { | ||
["validator.0"]=> | ||
string(92) "All of the required rules must pass for `{ "manufacturer": "Ford", "model": "notarealcar" }`" | ||
["validator.1"]=> | ||
string(91) "All of the required rules must pass for `{ "manufacturer": "Honda", "model": "not valid" }`" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters