ValidateTrait::errors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
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