Completed
Push — master ( 84e362...ac6c1f )
by Rougin
02:01
created

ValidateTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 59
c 0
b 0
f 0
ccs 15
cts 15
cp 1
rs 10

3 Methods

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