ValidateTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 10
c 3
b 0
f 0
dl 0
loc 40
ccs 11
cts 11
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A errors() 0 3 1
A validate() 0 16 2
1
<?php
2
3
namespace Rougin\Wildfire\Traits;
4
5
/**
6
 * @property array<string, string>[] $rules
7
 * @property \CI_Form_validation     $form_validation
8
 * @property \CI_Loader              $load
9
 *
10
 * @package Wildfire
11
 *
12
 * @author Rougin Gutib <[email protected]>
13
 */
14
trait ValidateTrait
15
{
16
    /**
17
     * @var array<string, string>
18
     */
19
    protected $errors = array();
20
21
    /**
22
     * Returns a listing of error messages.
23
     *
24
     * @return array<string, string>
25
     */
26
    public function errors()
27
    {
28 3
        return $this->errors;
29
    }
30 3
31
    /**
32 3
     * Validates the specified data based on the validation rules.
33
     *
34 3
     * @param array<string, mixed> $data
35
     *
36 3
     * @return boolean
37
     */
38 3
    public function validate(array $data)
39 3
    {
40
        $this->load->library('form_validation');
41 3
42 3
        $validation = $this->form_validation;
43
44 3
        $validation->set_data($data);
45
46
        $validation->set_rules($this->rules);
47
48
        if (! $validation->run())
49
        {
50
            $this->errors = $validation->error_array();
51
        }
52 3
53
        return count($this->errors) === 0;
54 3
    }
55
}
56