This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ValidatorServiceProvider.php
66 lines (55 loc) · 2.14 KB
/
ValidatorServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\Provider;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Silex\Provider\Validator\ConstraintValidatorFactory;
use Symfony\Component\Validator\Validator;
use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
use Symfony\Component\Validator\Validation;
/**
* Symfony Validator component Provider.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ValidatorServiceProvider implements ServiceProviderInterface
{
public function register(Container $app)
{
$app['validator'] = function ($app) {
return $app['validator.builder']->getValidator();
};
$app['validator.builder'] = function ($app) {
$builder = Validation::createValidatorBuilder();
$builder->setConstraintValidatorFactory($app['validator.validator_factory']);
$builder->setTranslationDomain($app['validator.translation_domain']);
$builder->addObjectInitializers($app['validator.object_initializers']);
$builder->setMetadataFactory($app['validator.mapping.class_metadata_factory']);
if (isset($app['translator'])) {
$builder->setTranslator($app['translator']);
}
return $builder;
};
$app['validator.mapping.class_metadata_factory'] = function ($app) {
return new LazyLoadingMetadataFactory(new StaticMethodLoader());
};
$app['validator.validator_factory'] = function () use ($app) {
return new ConstraintValidatorFactory($app, $app['validator.validator_service_ids']);
};
$app['validator.object_initializers'] = function ($app) {
return [];
};
$app['validator.validator_service_ids'] = [];
$app['validator.translation_domain'] = function () {
return 'validators';
};
}
}