Accessor   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 99
rs 10
c 0
b 0
f 0
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A hasKeyIn() 0 3 1
A lastInPath() 0 5 2
A firstInQuery() 0 3 2
A getValueIn() 0 9 3
A lastInQuery() 0 5 2
A firstInPath() 0 3 2
A getKeyIn() 0 8 2
A hasValueIn() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Keppler\Url\Traits;
5
6
use Keppler\Url\Exceptions\ComponentNotFoundException;
7
8
/**
9
 * Trait Accessor
10
 * @package Keppler\Url\Scheme\Traits
11
 */
12
trait Accessor
13
{
14
    /**
15
     * @param array $path
16
     * @return string|null
17
     */
18
    protected function firstInPath(array $path)
19
    {
20
        return false !== reset($path) ? reset($path) : null;
21
    }
22
23
    /**
24
     * @param array $query
25
     * @return array|null
26
     */
27
    protected function firstInQuery(array $query)
28
    {
29
        return false !== reset($query) ? [key($query) => reset($query)] : null;
30
    }
31
32
    /**
33
     * Returns the last key => value pair of an array
34
     *
35
     * @param array $array
36
     * @return string
37
     */
38
    protected function lastInPath(array $array): ?string
39
    {
40
        $array_revers = array_reverse($array);
41
42
        return false !== reset($array_revers) ? (string)reset($array_revers) : null;
43
    }
44
45
    /**
46
     * @param array $query
47
     * @return array|null
48
     */
49
    protected function lastInQuery(array $query)
50
    {
51
        $array_revers = array_reverse($query);
52
53
        return false !== reset($array_revers) ? [key($array_revers) => reset($array_revers)] : null;
54
    }
55
56
    /**
57
     * This sort of function could have easily been implemented in
58
     * each class but the single entry point is nice to have
59
     *
60
     * @param $in
61
     * @param $key
62
     * @return mixed
63
     * @throws ComponentNotFoundException
64
     */
65
    protected function getKeyIn($in, $key)
66
    {
67
        if (!array_key_exists($key, $in)) {
68
            throw new ComponentNotFoundException(sprintf('Component with index "%s" does not exist in %s', $key,
69
                __CLASS__));
70
        }
71
72
        return $in[$key];
73
    }
74
75
    /**
76
     * @param array $in
77
     * @param       $value
78
     *
79
     * @return mixed
80
     * @throws ComponentNotFoundException
81
     */
82
    protected function getValueIn(array $in, $value) {
83
        foreach ($in as $element) {
84
            if ($element === $value) {
85
                return $value;
86
            }
87
        }
88
89
        throw new ComponentNotFoundException(sprintf('Component with index "%s" does not exist in %s', $value,
90
            __CLASS__));
91
    }
92
93
    /**
94
     * @param array $in
95
     * @param $value
96
     * @return bool
97
     */
98
    protected function hasValueIn(array $in, $value): bool
99
    {
100
        return array_key_exists($value, array_flip($in));
101
    }
102
103
    /**
104
     * @param array $in
105
     * @param $key
106
     * @return bool
107
     */
108
    protected function hasKeyIn(array $in, $key): bool
109
    {
110
        return array_key_exists($key, $in);
111
    }
112
}