Passed
Branch append-document-1.0 (2fd487)
by Jonathan
17:18
created

DocumentDivider::getHaystack()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 17
ccs 13
cts 13
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 9
nop 0
crap 5
1
<?php
2
3
/**
4
 * ReportingCloud PHP Wrapper
5
 *
6
 * PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH.
7
 *
8
 * @link      http://www.reporting.cloud to learn more about ReportingCloud
9
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
10
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
11
 * @copyright © 2018 Text Control GmbH
12
 */
13
14
namespace TxTextControl\ReportingCloud\Validator;
15
16
use ReflectionClass;
17
use ReflectionException;
18
use TxTextControl\ReportingCloud\ReportingCloud;
19
use Zend\Validator\Digits as DigitsValidator;
20
use Zend\Validator\InArray as InArrayValidator;
21
22
/**
23
 * DocumentDivider validator
24
 *
25
 * @package TxTextControl\ReportingCloud
26
 * @author  Jonathan Maron (@JonathanMaron)
27
 */
28
class DocumentDivider extends AbstractValidator
29
{
30
    /**
31
     * Array of supported document dividers
32
     *
33
     * @var array
34
     */
35
    protected $haystack;
36
37
    /**
38
     * Invalid type
39
     *
40
     * @const INVALID_TYPE
41
     */
42
    const INVALID_TYPE = 'invalidType';
43
44
    /**
45
     * Unsupported document divider
46
     *
47
     * @const UNSUPPORTED_DIVIDER
48
     */
49
    const UNSUPPORTED_DIVIDER = 'unsupportedDocumentDivider';
50
51
    /**
52
     * Message templates
53
     *
54
     * @var array
55
     */
56
    protected $messageTemplates
57
        = [
58
            self::INVALID_TYPE        => "'%value%' must be numeric",
59
            self::UNSUPPORTED_DIVIDER => "'%value%' contains an unsupported document divider",
60
        ];
61
62
    /**
63
     * Returns true, if value is valid. False otherwise.
64
     *
65
     * @param mixed $value
66
     *
67
     * @return bool
68
     */
69 6
    public function isValid($value)
70
    {
71 6
        $this->setValue($value);
72
73 6
        $digitsValidator = new DigitsValidator();
74 6
        if (!$digitsValidator->isValid($value)) {
75 2
            $this->error(self::INVALID_TYPE);
76
77 2
            return false;
78
        }
79
80
        $options = [
81 6
            'haystack' => $this->getHaystack(),
82 6
            'strict'   => InArrayValidator::COMPARE_NOT_STRICT,
83 3
        ];
84
85 6
        $inArrayValidator = new InArrayValidator($options);
86 6
        if (!$inArrayValidator->isValid($value)) {
87 2
            $this->error(self::UNSUPPORTED_DIVIDER);
88
89 2
            return false;
90
        }
91
92 4
        return true;
93
    }
94
95
    /**
96
     * Set array of supported document dividers
97
     *
98
     * @return array
99
     */
100 6
    public function getHaystack()
101
    {
102 6
        if (null === $this->haystack) {
103 6
            $haystack = [];
104
            try {
105 6
                $reflectionClass = new ReflectionClass(new ReportingCloud());
106 6
                foreach ($reflectionClass->getConstants() as $key => $value) {
107 6
                    if (0 === strpos($key, 'DOCUMENT_DIVIDER_')) {
108 6
                        $haystack[] = $value;
109 3
                    }
110 3
                }
111 6
                $this->setHaystack($haystack);
112 3
            } catch (ReflectionException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
113
            }
114 3
        }
115
116 6
        return $this->haystack;
117
    }
118
119
    /**
120
     * Get array of supported document dividers
121
     *
122
     * @param array $haystack Haystack
123
     *
124
     * @return DocumentDivider
125
     */
126 6
    public function setHaystack($haystack)
127
    {
128 6
        $this->haystack = $haystack;
129
130 6
        return $this;
131
    }
132
}
133