Passed
Branch master (a71bcb)
by Henri
06:52
created

ExtraCheck::toNext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 1
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace HnrAzevedo\Validator;
4
5
Trait ExtraCheck{
6
    protected static array $data = [];
7
    protected static array $validators = [];
8
    protected static string $model = '';
9
    protected static array $required = [];
10
    protected static array $errors = [];
11
12
    protected static function checkRequireds()
13
    {
14
        if(count(self::$required) > 0){
15
            self::$errors[] = [
16
                'As seguintes informações não poderam ser validadas: '.implode(', ',array_keys(self::$required)).'.'
17
            ];
18
        }
19
    }
20
21
    public static function validateDate($date, $format = 'Y-m-d H:i:s')
22
    {
23
        $d = \DateTime::createFromFormat($format, $date);
24
        return $d && $d->format($format) == $date;
25
    }
26
27
    protected static function checkRequired(string $param): bool
28
    {
29
        return (array_key_exists('required',self::$validators[self::$model]->getRules(self::$data['role'])[$param]) && self::$validators[self::$model]->getRules(self::$data['role'])[$param]['required']);
30
    }
31
32
    protected static function toNext(string $param, $value)
33
    {
34
        return (self::checkRequired($param) || strlen($value > 0));
35
    }
36
37
    protected static function testArray(string $param, $value): ?array
38
    {
39
        if(!is_array(json_decode($value))){
40
            self::$errors[] = [
41
                $param => 'Era esperado uma informação em formato array para está informação.'
42
            ];
43
            return [];
44
        }
45
        return json_decode($value);
46
    }
47
}
48