Passed
Push — master ( 5c25b8...502eee )
by Gabor
05:16
created

NotEmptyValidator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 56
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B validate() 0 22 5
A getValidData() 0 4 1
A getErrors() 0 4 1
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Validator;
15
16
/**
17
 * class NotEmptyValidator.
18
 */
19
class NotEmptyValidator implements ValidatorInterface
20
{
21
    /** @var array */
22
    private $errors;
23
    /** @var array */
24
    private $validData;
25
26
    /**
27
     * Validates data. Returns true when data is not empty.
28
     *
29
     * @param array $values
30
     * @return bool
31
     */
32
    public function validate(array $values) : bool
33
    {
34
        $isEmpty = true;
35
36
        foreach ($values as $key => $data) {
37
            if (is_string($data)) {
38
                $data = trim($data);
39
            }
40
41
            if (!empty($data)) {
42
                $isEmpty = false;
43
                $this->validData[$key] = $data;
44
            }
45
        }
46
47
        if ($isEmpty) {
48
            $this->errors[] = 'This field is mandatory and cannot be empty';
49
            return false;
50
        }
51
52
        return true;
53
    }
54
55
    /**
56
     * Retrieve valid data.
57
     *
58
     * @return array
59
     */
60
    public function getValidData() : array
61
    {
62
        return $this->validData;
63
    }
64
65
    /**
66
     * Gets errors from validation.
67
     *
68
     * @return array
69
     */
70
    public function getErrors() : array
71
    {
72
        return $this->errors;
73
    }
74
}
75