DefaultValidator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 46
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A extractRules() 0 22 4
A getFieldRules() 0 9 6
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2014-2018 James Ekow Abaka Ainooson
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace ntentan\nibii;
28
29
use ntentan\utils\Validator;
30
31
class DefaultValidator extends Validator
32
{
33
    private $model;
34
35
    /**
36
     * @param RecordWrapper
37
     */
38 10
    public function __construct(RecordWrapper $model, $mode)
39
    {
40 10
        $this->model = $model;
41 10
        $this->registerValidation('unique', '\ntentan\nibii\UniqueValidation', ['model' => $model, 'mode' => $mode]);
42 10
    }
43
44 10
    public function extractRules()
45
    {
46 10
        $pk = null;
47 10
        $rules = [];
48 10
        $description = $this->model->getDescription();
49
50 10
        if ($description->getAutoPrimaryKey()) {
51 10
            $pk = $description->getPrimaryKey()[0];
52
        }
53
54 10
        $fields = $description->getFields();
55 10
        foreach ($fields as $field) {
56 10
            $this->getFieldRules($rules, $field, $pk);
57
        }
58
59 10
        $unique = $description->getUniqueKeys();
60 10
        foreach ($unique as $constraints) {
61 6
            $rules['unique'][] = [$constraints['fields']];
62
        }
63
64 10
        $this->setRules($rules);
65 10
    }
66
67 10
    private function getFieldRules(&$rules, $field, $pk)
68
    {
69 10
        if ($field['required'] && $field['name'] != $pk && $field['default'] === null) {
70 10
            $rules['required'][] = $field['name'];
71
        }
72 10
        if ($field['type'] === 'integer' || $field['type'] === 'double') {
73 10
            $rules['numeric'][] = $field['name'];
74
        }
75 10
    }
76
}
77