Completed
Push — master ( fd0e92...ea7c8b )
by Neomerx
04:49
created

FormValidationSettings::get()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
ccs 13
cts 13
cp 1
cc 2
eloc 15
nc 1
nop 0
crap 2
1
<?php namespace Limoncello\Application\Packages\FormValidation;
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 Generator;
20
use Limoncello\Application\Contracts\Validation\FormRuleSetInterface;
21
use Limoncello\Application\FormValidation\Execution\FormRuleSerializer;
22
use Limoncello\Application\FormValidation\Validator;
23
use Limoncello\Contracts\Settings\SettingsInterface;
24
25
/**
26
 * @package Limoncello\Application
27
 */
28
abstract class FormValidationSettings implements SettingsInterface
29
{
30
    /**
31
     * @param string $path
32
     * @param string $implementClassName
33
     *
34
     * @return Generator
35
     */
36
    abstract protected function selectClasses(string $path, string $implementClassName): Generator;
37
38
    /** Settings key */
39
    const KEY_VALIDATORS_FOLDER = 0;
40
41
    /** Config key */
42
    const KEY_VALIDATORS_FILE_MASK = self::KEY_VALIDATORS_FOLDER + 1;
43
44
    /** Config key */
45
    const KEY_VALIDATION_RULE_SETS_DATA = self::KEY_VALIDATORS_FILE_MASK + 1;
46
47
    /** Config key */
48
    const KEY_MESSAGES_NAMESPACE = self::KEY_VALIDATION_RULE_SETS_DATA + 1;
49
50
    /** Settings key */
51
    protected const KEY_LAST = self::KEY_MESSAGES_NAMESPACE;
52
53
    /**
54
     * @inheritdoc
55
     */
56 3
    final public function get(): array
57
    {
58 3
        $defaults = $this->getSettings();
59
60 3
        $validatorsFolder   = $defaults[static::KEY_VALIDATORS_FOLDER] ?? null;
61 3
        $validatorsFileMask = $defaults[static::KEY_VALIDATORS_FILE_MASK] ?? null;
62 3
        $validatorsPath     = $validatorsFolder . DIRECTORY_SEPARATOR . $validatorsFileMask;
63
64 3
        assert(
65 3
            $validatorsFolder !== null && empty(glob($validatorsFolder)) === false,
66 3
            "Invalid Validators folder `$validatorsFolder`."
67
        );
68
69 3
        $messagesNamespace = $defaults[static::KEY_MESSAGES_NAMESPACE] ?? null;
70 3
        assert(
71 3
            !empty($messagesNamespace),
72
            'Localization namespace have to be specified. ' .
73 3
            'Otherwise it is not possible to convert validation errors to string representation.'
74
        );
75
76
77
        return $defaults + [
78 3
                static::KEY_VALIDATION_RULE_SETS_DATA => $this->createValidationRulesSetData($validatorsPath),
79
            ];
80
    }
81
82
    /**
83
     * @return array
84
     */
85 3
    protected function getSettings(): array
86
    {
87
        return [
88 3
            static::KEY_VALIDATORS_FILE_MASK => '*.php',
89 3
            static::KEY_MESSAGES_NAMESPACE   => Validator::RESOURCES_NAMESPACE,
90
        ];
91
    }
92
93
    /**
94
     * @param string $validatorsPath
95
     *
96
     * @return array
97
     */
98 3
    private function createValidationRulesSetData(string $validatorsPath): array
99
    {
100 3
        $serializer = new FormRuleSerializer();
101 3
        foreach ($this->selectClasses($validatorsPath, FormRuleSetInterface::class) as $setClass) {
102
            /** @var string $setName */
103 3
            $setName = $setClass;
104 3
            assert(
105 3
                is_string($setClass) &&
106 3
                class_exists($setClass) &&
107 3
                array_key_exists(FormRuleSetInterface::class, class_implements($setClass))
108
            );
109
            /** @var FormRuleSetInterface $setClass */
110 3
            $serializer->addResourceRules($setName, $setClass::getAttributeRules());
111
        }
112
113 3
        $ruleSetsData = $serializer->getData();
114
115 3
        return $ruleSetsData;
116
    }
117
}
118