ArticlesListParametersValidator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Stp\SndApi\News\Validator;
4
5
use DateTime;
6
use Stp\SndApi\Common\ValidatorInterface;
7
8
class ArticlesListParametersValidator implements ValidatorInterface
9
{
10
    private $allowedMethodsParameters = [
11
        'desked' => [
12
            'areaLimit',
13
            'offset',
14
            'limit'
15
        ],
16
        'auto' => [
17
            'offset',
18
            'limit',
19
            'since',
20
            'until',
21
            'contentType',
22
            'includeSubsections',
23
            'homeSectionOnly'
24
        ]
25
    ];
26
27
    private $allowedParameterTypes = [
28
        'includeSubsections' => 'boolean',
29
        'homeSectionOnly' => 'boolean',
30
    ];
31
32
    private $method;
33
34
    private $parameters;
35
36
    /**
37
     * @param string $method
38
     * @param array $parameters
39
     */
40
    public function __construct($method, $parameters)
41
    {
42
        $this->method = $method;
43
        $this->parameters = $parameters;
44
    }
45
46
    private function validateBooleanString($value)
47
    {
48
        return $value === 'true' || $value === 'false';
49
    }
50
51
    private function entryIsValid($key, $value)
52
    {
53
        if (!in_array($key, $this->allowedMethodsParameters[$this->method])) {
54
            return false;
55
        }
56
57
        if (!array_key_exists($key, $this->allowedParameterTypes)) {
58
            return true;
59
        }
60
61
        return $this->validateBooleanString($value);
62
    }
63
64
    /**
65
     * @return bool
66
     */
67
    public function isValid()
68
    {
69
        foreach ($this->parameters as $parameterName => $parameterValue) {
70
            if (!$this->entryIsValid($parameterName, $parameterValue)) {
71
                return false;
72
            }
73
        }
74
75
        return true;
76
    }
77
}
78