ValidateTrait::validate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 4
b 0
f 0
nc 2
nop 1
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Rougin\Credo\Traits;
4
5
/**
6
 * Validate Trait
7
 *
8
 * @property array               $rules
9
 * @property \CI_Form_validation $form_validation
10
 * @property \CI_Loader          $load
11
 *
12
 * @package Credo
13
 * @author  Rougin Gutib <[email protected]>
14
 */
15
trait ValidateTrait
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $errors = array();
21
22
    /**
23
     * Returns a listing of error messages.
24
     *
25
     * @return array
26
     */
27 3
    public function errors()
28
    {
29 3
        return $this->errors;
30
    }
31
32
    /**
33
     * Validates the specified data based on the validation rules.
34
     *
35
     * @param  array $data
36
     * @return boolean
37
     */
38 3
    public function validate(array $data)
39
    {
40 3
        $this->load->library('form_validation');
41
42 3
        $validation = $this->form_validation;
43
44 3
        $validation->set_data((array) $data);
45
46 3
        $validation->set_rules($this->rules);
47
48 3
        if ($validation->run() === false) {
49 3
            $errors = $validation->error_array();
50
51 3
            $this->errors = (array) $errors;
52 2
        }
53
54 3
        return $validation->run() === true;
55
    }
56
}
57