Completed
Pull Request — master (#3)
by Rougin
02:59
created

ValidateTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 42
ccs 11
cts 12
cp 0.9167
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 18 3
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 $validationErrors = [];
21
22
    /**
23
     * Validates the specified data based on the validation rules.
24
     *
25
     * @param  array $data
26
     * @return boolean
27
     */
28 3
    public function validate(array $data = [])
29
    {
30 3
        $this->load->library('form_validation');
31
32 3
        if (! empty($data)) {
33 3
            $this->form_validation->set_data($data);
34 3
        }
35
36 3
        $this->form_validation->set_rules($this->validation_rules);
37
38 3
        if ($this->form_validation->run() === false) {
39 3
            $this->validationErrors = $this->form_validation->error_array();
40
41 3
            return false;
42
        }
43
44
        return true;
45
    }
46
47
    /**
48
     * Returns a listing of error messages.
49
     *
50
     * @return array
51
     */
52 3
    public function validationErrors()
53
    {
54 3
        return $this->validationErrors;
55
    }
56
}
57