Completed
Push — master ( 56127f...c3d14e )
by Travis
03:10
created

IterableStringGuard::validateIterableElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
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\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).
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 IterableStringGuard 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 4
    public function __construct($input, ?iterable $default = null)
30
    {
31 4
        $this->input = $input;
32 4
        $this->value = $default;
33 4
    }
34
35 3
    protected function extraStringValidation($input): bool
36
    {
37
        // Short circuit for anything not a integer, float, string or boolean.
38 3
        return \is_scalar($input);
39
    }
40
41 3
    protected function validateIterableElement($element, &$value): bool
42
    {
43 3
        return $this->stringValidation($element, $value);
44
    }
45
}
46