Completed
Push — master ( 647899...d0238c )
by Mike
12:45
created

Registry::loadRegistries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Mediadevs\Validator\Helpers;
4
5
class Registry extends Singleton
6
{
7
    /**
8
     * All the registered classes will be stored in here
9
     * @var array
10
     */
11
    private static $registry = array();
12
13
    /**
14
     * Registering the class in the $data array
15
     * @param array $registries
16
     *
17
     * @return void
18
     */
19
    public static function register(array $registries): void
20
    {
21
        self::$registry = array_merge(self::$registry, $registries);
22
    }
23
24
    /**
25
     * Collecting the registry based upon the given target
26
     * @param string $index
27
     *
28
     * @return string|void
29
     */
30
    public static function getRegistry(string $index): string
31
    {
32
        $targetIndex = null;
33
34
        // Whether the $data array has the given index
35
        if (self::hasIndex($index)) {
36
            $targetIndex = $index;
37
        }
38
39
        // In this case the $data array has no index of $index, now we'll try to reverse search by value
40
        if (self::hasValue($index)) {
41
            $targetIndex = self::getIndexByValue($index);
42
        }
43
44
        return self::getValuesByIndex($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

44
        return self::getValuesByIndex(/** @scrutinizer ignore-type */ $targetIndex);
Loading history...
45
    }
46
47
    /**
48
     * Validating whether the data contains the given index
49
     * @param string $index
50
     *
51
     * @return bool
52
     */
53
    public static function hasIndex(string $index): bool
54
    {
55
        return (bool) array_key_exists($index, self::$registry) ? true : false;
56
    }
57
58
    /**
59
     * Validating whether the data contains the given value
60
     * @param string $value
61
     *
62
     * @return bool
63
     */
64
    public static function hasValue(string $value): bool
65
    {
66
        // Reversing the data array; ['key' => 'value'] will now be ['value' => 'key']
67
        $reverseData = array_flip(self::$registry);
68
69
        return (bool) (isset($reverseData[$value])) ? true : false;
70
    }
71
72
    /**
73
     * Validating whether the given index has a value
74
     * @param string $index
75
     *
76
     * @return bool
77
     */
78
    public static function indexHasValue(string $index): bool
79
    {
80
        if (self::hasIndex($index)) {
81
            return (bool) !empty(self::$registry[$index]) ? true : false;
82
        }
83
84
        return (bool) false;
85
    }
86
87
    /**
88
     * Collecting the index by the given value
89
     * @param string $value
90
     *
91
     * @return string|void
92
     */
93
    public static function getIndexByValue(string $value)
94
    {
95
        if (self::hasValue($value)) {
96
            // Reversing the data array; ['key' => 'value'] will now be ['value' => 'key']
97
            $reverseData = array_flip(self::$registry);
98
99
            return $reverseData[$value];
100
        }
101
    }
102
103
    /**
104
     * Collecting multiple values based on the given index
105
     * @param string $index
106
     *
107
     * @return array|void
108
     */
109
    public static function getValuesByIndex(string $index)
110
    {
111
        if (self::hasIndex($index) && self::indexHasValue($index)) {
112
            return self::$registry[$index];
113
        }
114
    }
115
}
116