Passed
Pull Request — master (#343)
by Alejandro
05:42
created

InputFactoryTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 24
rs 10
c 0
b 0
f 0
ccs 16
cts 16
cp 1
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createBooleanInput() 0 13 1
A createInput() 0 7 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Common\Validation;
5
6
use Zend\Filter;
7
use Zend\InputFilter\Input;
8
use Zend\Validator;
9
10
trait InputFactoryTrait
11
{
12 15
    private function createInput($name, $required = true): Input
13
    {
14 15
        $input = new Input($name);
15 15
        $input->setRequired($required)
16 15
              ->getFilterChain()->attach(new Filter\StripTags())
17 15
                                ->attach(new Filter\StringTrim());
18 15
        return $input;
19
    }
20
21 15
    private function createBooleanInput(string $name, bool $required = true): Input
22
    {
23 15
        $input = $this->createInput($name, $required);
24 15
        $input->getFilterChain()->attach(new Filter\Boolean());
25 15
        $input->getValidatorChain()->attach(new Validator\NotEmpty(['type' => [
26 15
            Validator\NotEmpty::OBJECT,
27 15
            Validator\NotEmpty::SPACE,
28 15
            Validator\NotEmpty::NULL,
29 15
            Validator\NotEmpty::EMPTY_ARRAY,
30 15
            Validator\NotEmpty::STRING,
31
        ]]));
32
33 15
        return $input;
34
    }
35
}
36