Passed
Push — master ( 1cc59e...a8a804 )
by Travis
02:24
created

StringGuard::value()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace InputGuard\Guards;
5
6
use InputGuard\Guards\Bases\SingleInput;
7
use InputGuard\Guards\Bases\StringBase;
8
9
/**
10
 * Base valid inputs:
11
 * 1) An empty string.
12
 * 2) A scalar (can be cast back and forth by PHP).
13
 *
14
 * Modifiable validations:
15
 * 1) Character length for the string.
16
 * 2) Regex on the string.
17
 */
18
class StringGuard implements Guard
19
{
20
    use ErrorMessagesBase;
21
    use StringBase;
22
    use SingleInput;
23
24 12
    public function __construct($input, ?int $default = null)
25
    {
26 12
        $this->input = $input;
27 12
        $this->value = $default;
28 12
    }
29
30 11
    protected function validationShortCircuit($input): bool
31
    {
32
        // The is_scalar is a short circuit for anything not a integer, float, string or boolean.
33 11
        return $this->strict ? \is_string($input) : \is_scalar($input);
0 ignored issues
show
Coding Style introduced by
Inline IF statements are not allowed
Loading history...
34
    }
35
36 10
    public function value(): ?string
37
    {
38 10
        $this->success();
39
40 10
        return $this->value;
41
    }
42
}
43