Completed
Push — develop ( fc40f2...fd0e92 )
by Neomerx
08:05 queued 06:22
created

FormValidationSettings::get()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

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