Completed
Pull Request — master (#3)
by Rougin
04:33
created

ValidateTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 83.33%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 16 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 3
        }
41
42 3
        return $validated;
0 ignored issues
show
Bug introduced by
The variable $validated does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
43
    }
44
45
    /**
46
     * Returns a listing of error messages.
47
     *
48
     * @return array
49
     */
50
    public function validationErrors()
51
    {
52
        return $this->validationErrors;
53
    }
54
}
55