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

ValidateTrait::validationErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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