Completed
Push — master ( 1776d4...322507 )
by Neomerx
02:47
created

FormValidatorFactory::createValidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
crap 2
1
<?php namespace Limoncello\Application\FormValidation\Execution;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Application\Contracts\Validation\FormValidatorFactoryInterface;
20
use Limoncello\Application\Contracts\Validation\FormValidatorInterface;
21
use Limoncello\Application\FormValidation\Validator;
22
use Limoncello\Application\Packages\FormValidation\FormValidationSettings as S;
23
use Limoncello\Container\Traits\HasContainerTrait;
24
use Limoncello\Contracts\L10n\FormatterFactoryInterface;
25
use Limoncello\Contracts\Settings\SettingsProviderInterface;
26
use Psr\Container\ContainerInterface;
27
28
/**
29
 * @package Limoncello\Flute
30
 */
31
class FormValidatorFactory implements FormValidatorFactoryInterface
32
{
33
    use HasContainerTrait;
34
35
    /**
36
     * @param ContainerInterface $container
37
     */
38
    public function __construct(ContainerInterface $container)
39
    {
40
        $this->setContainer($container);
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function createValidator(string $class): FormValidatorInterface
47
    {
48
        /** @var SettingsProviderInterface $settingsProvider */
49
        $settingsProvider  = $this->getContainer()->get(SettingsProviderInterface::class);
50
        $settings          = $settingsProvider->get(S::class);
51
        $ruleSetsData      = $settings[S::KEY_VALIDATION_RULE_SETS_DATA];
52
        $messagesNamespace = $settings[S::KEY_MESSAGES_NAMESPACE];
53
54
        /** @var FormatterFactoryInterface $formatterFactory */
55
        $formatterFactory = $this->getContainer()->get(FormatterFactoryInterface::class);
56
        $formatter        = $formatterFactory->createFormatter($messagesNamespace);
57
58
        $validator = new Validator($class, $ruleSetsData, $this->getContainer(), $formatter);
59
60
        return $validator;
61
    }
62
}
63