Completed
Push — master ( 99d97a...dc1e39 )
by Lee
02:08
created

NotEmptyString::isValid()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 5
nop 1
dl 0
loc 20
ccs 12
cts 12
cp 1
crap 4
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace Lavibi\Popoya;
4
5
class NotEmptyString extends AbstractValidator
6
{
7
    const E_EMPTY = 'empty';
8
    const E_NOT_STRING = 'not_string';
9
10
    protected $messages = [
11
        self::E_EMPTY => 'The given value is empty string.',
12
        self::E_NOT_STRING => 'The given value is not string.'
13
    ];
14
15
    protected $defaultOptions = [
16
        'trim' => true
17
    ];
18
19 5
    public function isValid($value)
20
    {
21 5
        $this->standardValue = null;
22 5
        $this->value = $value;
23
24 5
        if (!is_string($this->value)) {
25 3
            $this->setError(static::E_NOT_STRING);
26 3
            return false;
27
        }
28
29 5
        if ($this->options['trim'] === true) {
30 4
            $this->value = trim($this->value);
31
        }
32
33 5
        if ($this->value === '') {
34 3
            $this->setError(static::E_EMPTY);
35 3
            return false;
36
        }
37
38 3
        return true;
39
    }
40
}
41