InputFactoryTrait::createDateInput()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Common\Validation;
6
7
use DateTime;
8
use Laminas\Filter;
9
use Laminas\InputFilter\ArrayInput;
10
use Laminas\InputFilter\Input;
11
use Laminas\Validator;
12
13
use function Functional\map;
14
15
trait InputFactoryTrait
16
{
17 9
    private function createInput(string $name, bool $required = true): Input
18
    {
19 9
        $input = new Input($name);
20 9
        $input->setRequired($required)
21 9
              ->getFilterChain()->attach(new Filter\StripTags())
22 9
                                ->attach(new Filter\StringTrim());
23 9
        return $input;
24
    }
25
26 3
    private function createBooleanInput(string $name, bool $required = true): Input
27
    {
28 3
        $input = $this->createInput($name, $required);
29 3
        $input->getFilterChain()->attach(new Filter\Boolean());
30 3
        $input->getValidatorChain()->attach(new Validator\NotEmpty(['type' => [
31 3
            Validator\NotEmpty::OBJECT,
32 3
            Validator\NotEmpty::SPACE,
33 3
            Validator\NotEmpty::NULL,
34 3
            Validator\NotEmpty::EMPTY_ARRAY,
35 3
            Validator\NotEmpty::STRING,
36
        ]]));
37
38 3
        return $input;
39
    }
40
41 3
    private function createDateInput(
42
        string $name,
43
        bool $required = true,
44
        array $formats = [DateTime::ATOM, 'Y-m-d']
45
    ): Input {
46 3
        $input = $this->createInput($name, $required);
47 3
        $input->getValidatorChain()->attach(new ExcludingValidatorChain(...map(
48 3
            $formats,
49 3
            fn (string $format) => new Validator\Date(['format' => $format]),
50
        )));
51 3
        return $input;
52
    }
53
54 3
    private function createArrayInput(string $name, bool $required = true): Input
55
    {
56 3
        $input = new ArrayInput($name);
57 3
        $input->setRequired($required)
58 3
              ->getFilterChain()->attach(new Filter\StripTags())
59 3
                                ->attach(new Filter\StringTrim());
60 3
        return $input;
61
    }
62
}
63