respect-validation/tests/integration/translator-check.phpt
Henrique Moody ab602ae1bb
Improve how to customize the Factory
The constructor of "Factory" has three arguments and, even though none
of them are needed, they are all required. Those arguments allow users
to customize the namespaces of rules and exceptions, and also to define
a callable that will translate the template messages.

This commit will remove those parameters from the constructor of
"Factory," and create methods that will allow users to customize the
namespaces and the translator.

The methods that this commit will create will not change the state of
"Factory," but they will create a clone with the customizations. It is
imperative that the "Factory" is immutable. Since the "Factory" is a
Singleton, allowing it to change could cause unexpected behaviors.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2019-05-12 12:24:44 +02:00

31 lines
697 B
PHP

--CREDITS--
Henrique Moody <henriquemoody@gmail.com>
--FILE--
<?php
declare(strict_types=1);
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Factory;
use Respect\Validation\Validator;
Factory::setDefaultInstance(
(new Factory())
->withTranslator(static function (string $message): string {
return [
'{{name}} must be of type string' => '{{name}} deve ser do tipo string',
][$message];
})
);
try {
Validator::stringType()->length(2, 15)->check(0);
} catch (ValidationException $exception) {
echo $exception->getMessage();
}
?>
--EXPECT--
0 deve ser do tipo string