Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from awurth/asserter
Add data collector
- Loading branch information
Showing
16 changed files
with
515 additions
and
89 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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Awurth Validator package. | ||
* | ||
* (c) Alexis Wurth <awurth.dev@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Awurth\Validator; | ||
|
||
use Respect\Validation\Exceptions\NestedValidationException; | ||
|
||
final class Asserter implements AsserterInterface | ||
{ | ||
/** | ||
* @param array<string, string> $messages | ||
*/ | ||
public function __construct( | ||
private readonly ValidationFailureCollectionFactoryInterface $validationFailureCollectionFactory, | ||
private readonly ValidationFailureFactoryInterface $validationFailureFactory, | ||
private readonly array $messages = [], | ||
) { | ||
} | ||
|
||
/** | ||
* @param array<string, string> $messages | ||
*/ | ||
public static function create(array $messages = []): self | ||
{ | ||
return new self(new ValidationFailureCollectionFactory(), new ValidationFailureFactory(), $messages); | ||
} | ||
|
||
public function assert(mixed $subject, ValidationInterface $validation, array $messages = []): ValidationFailureCollectionInterface | ||
{ | ||
$failures = $this->validationFailureCollectionFactory->create(); | ||
|
||
try { | ||
$validation->getRules()->assert($subject); | ||
} catch (NestedValidationException $exception) { | ||
if ($message = $validation->getMessage()) { | ||
$failures->add( | ||
$this->validationFailureFactory->create($message, $subject, $validation->getProperty()) | ||
); | ||
|
||
return $failures; | ||
} | ||
|
||
$exceptionMessages = $this->extractMessagesFromException($exception, $validation, $messages); | ||
foreach ($exceptionMessages as $ruleName => $message) { | ||
$failures->add( | ||
$this->validationFailureFactory->create($message, $subject, $validation->getProperty(), $ruleName) | ||
); | ||
} | ||
} | ||
|
||
return $failures; | ||
} | ||
|
||
/** | ||
* @param array<string, string> $messages | ||
* | ||
* @return array<string, string> | ||
*/ | ||
private function extractMessagesFromException(NestedValidationException $exception, ValidationInterface $validation, array $messages = []): array | ||
{ | ||
$definedMessages = \array_replace($this->messages, $messages, $validation->getMessages()); | ||
|
||
$errors = []; | ||
foreach ($exception->getMessages($definedMessages) as $name => $error) { | ||
if (\is_array($error)) { | ||
$errors = [...$errors, ...$error]; | ||
} else { | ||
$errors[$name] = $error; | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Awurth Validator package. | ||
* | ||
* (c) Alexis Wurth <awurth.dev@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Awurth\Validator; | ||
|
||
interface AsserterInterface | ||
{ | ||
/** | ||
* @param array<string, string> $messages | ||
*/ | ||
public function assert(mixed $subject, ValidationInterface $validation, array $messages = []): ValidationFailureCollectionInterface; | ||
} |
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Awurth Validator package. | ||
* | ||
* (c) Alexis Wurth <awurth.dev@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Awurth\Validator; | ||
|
||
final class DataCollectorAsserter implements DataCollectorAsserterInterface | ||
{ | ||
private readonly ValidatedValueCollectionInterface $data; | ||
|
||
public function __construct(private readonly AsserterInterface $asserter) | ||
{ | ||
$this->data = new ValidatedValueCollection(); | ||
} | ||
|
||
public static function create(): self | ||
{ | ||
return new self(Asserter::create()); | ||
} | ||
|
||
public function assert(mixed $subject, ValidationInterface $validation, array $messages = []): ValidationFailureCollectionInterface | ||
{ | ||
$this->data->add(new ValidatedValue($validation, $subject)); | ||
|
||
return $this->asserter->assert($subject, $validation, $messages); | ||
} | ||
|
||
public function getData(): ValidatedValueCollectionInterface | ||
{ | ||
return $this->data; | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Awurth Validator package. | ||
* | ||
* (c) Alexis Wurth <awurth.dev@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Awurth\Validator; | ||
|
||
interface DataCollectorAsserterInterface extends AsserterInterface | ||
{ | ||
public function getData(): ValidatedValueCollectionInterface; | ||
} |
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
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
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
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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Awurth Validator package. | ||
* | ||
* (c) Alexis Wurth <awurth.dev@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Awurth\Validator; | ||
|
||
final class ValidatedValue implements ValidatedValueInterface | ||
{ | ||
public function __construct(private readonly ValidationInterface $validation, private readonly mixed $value) | ||
{ | ||
} | ||
|
||
public function getValidation(): ValidationInterface | ||
{ | ||
return $this->validation; | ||
} | ||
|
||
public function getValue(): mixed | ||
{ | ||
return $this->value; | ||
} | ||
} |
Oops, something went wrong.