Completed
Branch 1.0 (4bdad9)
by Mike
04:29
created

Registry::handleIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
c 2
b 0
f 0
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Mediadevs\Validator\Helpers;
4
5
use ReflectionException;
6
use Mediadevs\Validator\Services\FilterProvider;
7
8
class Registry
9
{
10
    /**
11
     * The registry types for this library, these are de default properties for each filter class.
12
     */
13
    public const REGISTRY_TYPES = array('filters', 'aliases', 'messages');
14
15
    /**
16
     * All the registered classes will be stored in here.
17
     *
18
     * @var array
19
     */
20
    private $registry = array(
21
        'filters'   => array(),
22
        'messages'  => array(),
23
    );
24
25
    /**
26
     * Loading all the classes from \Services\FilterProvider and parsing the filters to collect the configuration
27
     * Using te previously collected configuration to generate a registry which can be used for validation.
28
     *
29
     * @return bool
30
     * @throws ReflectionException
31
     */
32
    public function load(): bool
33
    {
34
        // Parsing through all the providers
35
       foreach (FilterProvider::collect() as $provider) {
36
           $filter = new ExtractFilters($provider);
37
38
           // Registering the properties
39
           $this->registry['filters'] += $filter->getFilters();
40
           $this->registry['messages'] += $filter->getMessages();
41
       }
42
43
       return true;
44
    }
45
46
    /**
47
     * Collecting the registry based upon the given target.
48
     *
49
     * @param string $type
50
     * @param string $index
51
     *
52
     * @return string|void
53
     */
54
    public function getRegistry(string $type, string $index): string
55
    {
56
        $targetIndex = null;
57
58
        // Whether the type exists inside the $data array
59
        if (self::hasType($type)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasType() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        if (self::/** @scrutinizer ignore-call */ hasType($type)) {
Loading history...
Coding Style introduced by
Blank line found at start of control structure
Loading history...
60
61
            // Whether the $data array has the given index
62
            if (self::hasIndex($type, $index)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasIndex() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            if (self::/** @scrutinizer ignore-call */ hasIndex($type, $index)) {
Loading history...
63
                $targetIndex = $index;
64
            }
65
66
            // In this case the $data array has no index of $index, now we'll try to reverse search by value
67
            if (self::hasValue($type, $index)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasValue() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
            if (self::/** @scrutinizer ignore-call */ hasValue($type, $index)) {
Loading history...
68
                $targetIndex = self::getIndexByValue($type, $index);
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Help...stry::getIndexByValue() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
                /** @scrutinizer ignore-call */ 
69
                $targetIndex = self::getIndexByValue($type, $index);
Loading history...
69
            }
70
        }
71
72
        return self::getValuesByIndex($type, $targetIndex);
0 ignored issues
show
Bug introduced by
It seems like $targetIndex can also be of type null; however, parameter $index of Mediadevs\Validator\Help...try::getValuesByIndex() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
        return self::getValuesByIndex($type, /** @scrutinizer ignore-type */ $targetIndex);
Loading history...
Bug Best Practice introduced by
The method Mediadevs\Validator\Help...try::getValuesByIndex() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        return self::/** @scrutinizer ignore-call */ getValuesByIndex($type, $targetIndex);
Loading history...
73
    }
74
75
    /**
76
     * Whether the given type is a valid one.
77
     *
78
     * @param string $type
79
     *
80
     * @return bool
81
     */
82
    public function isValidType(string $type): bool
83
    {
84
        return (bool) in_array($type, self::REGISTRY_TYPES) ? true : false;
85
    }
86
87
    /**
88
     * Validating whether registry contains the given type.
89
     *
90
     * @param string $type
91
     *
92
     * @return bool
93
     */
94
    public function hasType(string $type): bool
95
    {
96
        // Whether the given type is a valid one.
97
        if (self::isValidType($type)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Help...Registry::isValidType() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
        if (self::/** @scrutinizer ignore-call */ isValidType($type)) {
Loading history...
98
            return (bool) array_key_exists($type, $this->registry) ? true : false;
99
        }
100
101
        return (bool) false;
102
    }
103
104
    /**
105
     * Validating whether the data contains the given index.
106
     *
107
     * @param string $type
108
     * @param string $index
109
     *
110
     * @return bool
111
     */
112
    public function hasIndex(string $type, string $index): bool
113
    {
114
        if (self::hasType($type)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasType() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
        if (self::/** @scrutinizer ignore-call */ hasType($type)) {
Loading history...
115
            return (bool) array_key_exists($index, $this->registry[$type]) ? true : false;
116
        }
117
118
        return (bool) false;
119
    }
120
121
    /**
122
     * Validating whether the data contains the given value.
123
     *
124
     * @param string $type
125
     * @param string $value
126
     *
127
     * @return bool
128
     */
129
    public function hasValue(string $type, string $value): bool
130
    {
131
        if (self::hasType($type)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasType() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
        if (self::/** @scrutinizer ignore-call */ hasType($type)) {
Loading history...
132
            // Reversing the data array; ['key' => 'value'] will now be ['value' => 'key']
133
            $reverseData = array_flip($this->registry[$type]);
134
135
            return (bool) (isset($reverseData[$value])) ? true : false;
136
        }
137
138
        return (bool) false;
139
    }
140
141
    /**
142
     * Validating whether the given index has a value.
143
     *
144
     * @param string $type
145
     * @param string $index
146
     *
147
     * @return bool
148
     */
149
    public function indexHasValue(string $type, string $index): bool
150
    {
151
        if (self::hasType($type) && self::hasIndex($type, $index)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasType() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

151
        if (self::/** @scrutinizer ignore-call */ hasType($type) && self::hasIndex($type, $index)) {
Loading history...
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasIndex() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

151
        if (self::hasType($type) && self::/** @scrutinizer ignore-call */ hasIndex($type, $index)) {
Loading history...
152
            return (bool) !empty($this->registry[$type][$index]) ? true : false;
153
        }
154
155
        return (bool) false;
156
    }
157
158
    /**
159
     * Collecting the index by the given value.
160
     *
161
     * @param string $type
162
     * @param string $value
163
     *
164
     * @return string|void
165
     */
166
    public function getIndexByValue(string $type, string $value)
167
    {
168
        if (self::hasType($type) && self::hasValue($type, $value)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasType() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

168
        if (self::/** @scrutinizer ignore-call */ hasType($type) && self::hasValue($type, $value)) {
Loading history...
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasValue() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

168
        if (self::hasType($type) && self::/** @scrutinizer ignore-call */ hasValue($type, $value)) {
Loading history...
169
            // Reversing the data array; ['key' => 'value'] will now be ['value' => 'key']
170
            $reverseData = array_flip($this->registry[$type]);
171
172
            return $reverseData[$type][$value];
173
        }
174
    }
175
176
    /**
177
     * Collecting multiple values based on the given index.
178
     *
179
     * @param string $type
180
     * @param string $index
181
     *
182
     * @return array|void
183
     */
184
    public function getValuesByIndex(string $type, string $index)
185
    {
186
        if (self::hasType($type) && self::hasIndex($type, $index) && self::indexHasValue($type, $index)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasType() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

186
        if (self::/** @scrutinizer ignore-call */ hasType($type) && self::hasIndex($type, $index) && self::indexHasValue($type, $index)) {
Loading history...
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasIndex() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

186
        if (self::hasType($type) && self::/** @scrutinizer ignore-call */ hasIndex($type, $index) && self::indexHasValue($type, $index)) {
Loading history...
Bug Best Practice introduced by
The method Mediadevs\Validator\Help...gistry::indexHasValue() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

186
        if (self::hasType($type) && self::hasIndex($type, $index) && self::/** @scrutinizer ignore-call */ indexHasValue($type, $index)) {
Loading history...
187
            return $this->registry[$type][$index];
188
        }
189
    }
190
}
191