Passed
Push — master ( 7ed858...861abc )
by Gabor
02:38
created

NotEmptyValidator::setOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2018 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
use RuntimeException;
17
18
/**
19
 * class NotEmptyValidator.
20
 */
21
class NotEmptyValidator implements ValidatorInterface
22
{
23
    /**
24
     * @var array
25
     */
26
    private $errors;
27
    /**
28
     * @var array
29
     */
30
    private $validData;
31
32
    /**
33
     * Set validator options.
34
     * This validator does not accept any option.
35
     *
36
     * @param array $options
37
     * @throws RuntimeException
38
     */
39
    public function setOptions(array $options) : void
40
    {
41
        unset($options);
42
    }
43
44
    /**
45
     * Validates data. Returns true when data is not empty.
46
     *
47
     * @param  array $values
48
     * @return bool
49
     */
50 1
    public function validate(array $values) : bool
51
    {
52 1
        $isEmpty = true;
53
54 1
        foreach ($values as $key => $data) {
55 1
            if (is_string($data)) {
56 1
                $data = trim($data);
57
            }
58
59 1
            if (!empty($data)) {
60 1
                $isEmpty = false;
61 1
                $this->validData[$key] = $data;
62
            }
63
        }
64
65 1
        if ($isEmpty) {
66 1
            $this->errors[] = 'This field is mandatory and cannot be empty';
67 1
            return false;
68
        }
69
70 1
        return true;
71
    }
72
73
    /**
74
     * Retrieve valid data.
75
     *
76
     * @return array
77
     */
78 1
    public function getValidData() : array
79
    {
80 1
        return $this->validData;
81
    }
82
83
    /**
84
     * Gets errors from validation.
85
     *
86
     * @return array
87
     */
88 1
    public function getErrors() : array
89
    {
90 1
        return $this->errors;
91
    }
92
}
93