Completed
Pull Request — master (#3)
by Mariano
09:11
created

Properties::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of php-simple-request.
4
 *
5
 * php-simple-request is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * php-simple-request is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with php-simple-request.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
namespace Mcustiel\SimpleRequest\Annotation\Validator;
19
20
use Mcustiel\SimpleRequest\Annotation\ValidatorAnnotation;
21
use Mcustiel\SimpleRequest\Validator\Properties as PropertiesValidator;
22
use Mcustiel\SimpleRequest\Exception\InvalidAnnotationException;
23
24
/**
25
 * @Annotation
26
 * @Target({ "PROPERTY", "ANNOTATION" })
27
 *
28
 * @author mcustiel
29
 */
30
class Properties extends ValidatorAnnotation
31
{
32
    public $properties;
33
    public $additionalProperties;
34
35 80
    public function __construct()
36
    {
37 80
        parent::__construct(PropertiesValidator::class);
38 80
    }
39
40 80
    public function getValue()
41
    {
42 80
        $count = count($this->properties);
43 80
        $this->validatePropertiesCountOrThrowException($count);
44
45
        return [
46 79
            'properties'           => $this->getPropertiesConfig($count),
47 79
            'additionalProperties' => $this->additionalProperties,
48 79
        ];
49
    }
50
51 79
    private function getPropertiesConfig($count)
52
    {
53 79
        $properties = array();
54 79
        for ($i = 0; $i < $count; $i += 2) {
55 79
            $properties[$this->properties[$i]] = $this->properties[$i + 1];
56 79
        }
57 79
        return $properties;
58
    }
59
60
61 80
    private function validatePropertiesCountOrThrowException($count)
62
    {
63 80
        if ($count % 2 != 0) {
64 1
            throw new InvalidAnnotationException(
65
                'Properties field must specify a set of (name, validator) pairs'
66 1
            );
67
        }
68 79
    }
69
}
70