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

IterableStringableGuard   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 25
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validateIterableElement() 0 3 1
A validationShortCircuit() 0 4 3
1
<?php
2
declare(strict_types=1);
3
4
namespace InputGuard\Guards;
5
6
use InputGuard\Guards\Bases\SingleIterableInput;
7
use InputGuard\Guards\Bases\StringBase;
8
9
/**
10
 * Base valid inputs:
11
 * 1) An empty iterable.
12
 * 2) An iterable of scalars (can be cast back and forth by PHP), and a class with a __toString() method.
13
 *
14
 * Modifiable validations:
15
 * 1) Number of elements in the iterable.
16
 * 2) Character length for each element.
17
 * 3) Regex on each element.
18
 */
19
class IterableStringableGuard implements Guard
20
{
21
    use ErrorMessagesBase;
22
    use SingleIterableInput;
23
    use StringBase {
24
        // Use the iterable's validation as the primary validation logic and rename the string validation method.
25
        SingleIterableInput::validation insteadof StringBase;
26
        StringBase::validation as stringValidation;
27
    }
28
29 6
    public function __construct($input, ?iterable $default = null)
30
    {
31 6
        $this->input = $input;
32 6
        $this->value = $default;
33 6
    }
34
35 4
    protected function validationShortCircuit($input): bool
36
    {
37
        // Short circuit for anything not a integer, float, string, boolean, or object with a __toString method.
38 4
        return is_scalar($input) || (\is_object($input) && method_exists($input, '__toString'));
39
    }
40
41 4
    protected function validateIterableElement($element, &$value): bool
42
    {
43 4
        return $this->stringValidation($element, $value);
44
    }
45
}
46