UriParser   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Test Coverage

Coverage 84.29%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 63
c 3
b 1
f 0
dl 0
loc 193
ccs 59
cts 70
cp 0.8429
rs 10
wmc 29

9 Methods

Rating   Name   Duplication   Size   Complexity  
B composeUri() 0 30 7
A __construct() 0 5 1
A parsePost() 0 17 5
A translateKeyOutput() 0 7 2
A setTranslator() 0 3 1
B parseUri() 0 23 7
A translateValueOutput() 0 7 2
A translateKeyInput() 0 7 2
A translateValueInput() 0 7 2
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Bundle\UrlBundle\Url\Params;
8
9
/**
10
 * Parser for key/value pairs in the url.
11
 */
12
class UriParser implements Translator
13
{
14
    private $seperators = array();
15
16
    /**
17
     * @var Translator
18
     */
19
    private $translator = null;
20
21
    /**
22
     * Constructor
23
     *
24
     * @param string $paramSeparator
25
     * @param string $keyValueSeparator
26
     * @param string $valueSeparator
27
     */
28 26
    public function __construct($paramSeparator = '/', $keyValueSeparator = '=', $valueSeparator = ',')
29
    {
30 26
        $this->seperators['param']     = $paramSeparator;
31 26
        $this->seperators['key_value'] = $keyValueSeparator;
32 26
        $this->seperators['value']     = $valueSeparator;
33 26
    }
34
35
36
    /**
37
     * Translator
38
     *
39
     * @param Translator $translator
40
     * @return void
41
     */
42 3
    public function setTranslator(Translator $translator)
43
    {
44 3
        $this->translator = $translator;
45 3
    }
46
47
    /**
48
     * POST keys are not translated, but do translate the values
49
     *
50
     * @param array $post
51
     * @return array
52
     */
53
    public function parsePost(array $post)
54
    {
55
        $ret = array();
56
        foreach ($post as $key => $value) {
57
            if ($key) {
58
                $external  = $this->translateKeyOutput($key);
59
                $ret[$key] = array();
60
                if (strlen($value) > 0) {
61
                    if ($internal = $this->translateValueInput($external, $value)) {
62
                        $value = $internal;
63
                    }
64
                    $ret[$key][] = $value;
65
                }
66
            }
67
        }
68
69
        return $ret;
70
    }
71
72
    /**
73
     * Parse the uri
74
     *
75
     * @param string $uri
76
     * @return array
77
     */
78 22
    public function parseUri($uri)
79
    {
80 22
        $ret = array();
81 22
        foreach (explode($this->seperators['param'], $uri) as $params) {
82 22
            if ($params) {
83 22
                @list($key, $values) = explode($this->seperators['key_value'], $params, 2);
84 22
                $external = $key;
85 22
                if ($internal = $this->translateKeyInput($key)) {
86 2
                    $key = $internal;
87
                }
88 22
                $ret[$key] = array();
89 22
                foreach (explode($this->seperators['value'], $values) as $value) {
90 22
                    if (strlen($value) > 0) {
91 21
                        if ($internal = $this->translateValueInput($external, $value)) {
92 2
                            $value = $internal;
93
                        }
94 22
                        $ret[$key][] = urldecode($value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type true; however, parameter $str of urldecode() 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

94
                        $ret[$key][] = urldecode(/** @scrutinizer ignore-type */ $value);
Loading history...
95
                    }
96
                }
97
            }
98
        }
99
100 22
        return $ret;
101
    }
102
103
104
    /**
105
     * Compose an URI from the passed params with the local separators
106
     *
107
     * @param Params $params
108
     * @return string
109
     */
110 16
    public function composeUri($params)
111
    {
112 16
        $ret   = '';
113 16
        $first = true;
114
115 16
        foreach ($params as $param => $values) {
116 14
            if (!$first) {
117 10
                $ret .= $this->seperators['param'];
118
            }
119 14
            $first    = false;
120 14
            $internal = $param;
121 14
            if ($external = $this->translateKeyOutput($param)) {
122 2
                $param = $external;
123
            }
124 14
            $ret .= $param . $this->seperators['key_value'];
125 14
            $firstValue = true;
126 14
            foreach ($values as $value) {
127 14
                if ($external = $this->translateValueOutput($internal, $value)) {
128 2
                    $value = $external;
129
                }
130 14
                if (!$firstValue) {
131 11
                    $ret .= $this->seperators['value'];
132
                } else {
133 14
                    $firstValue = false;
134
                }
135 14
                $ret .= urlencode($value);
136
            }
137
        }
138
139 16
        return $ret;
140
    }
141
142
143
    /**
144
     * Proxy method for translateKeyInput() of the translator
145
     *
146
     * @param string $keyName
147
     * @return bool
148
     */
149 22
    final public function translateKeyInput($keyName)
150
    {
151 22
        if ($this->translator) {
152 2
            return $this->translator->translateKeyInput($keyName);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->translator...slateKeyInput($keyName) returns the type string which is incompatible with the documented return type boolean.
Loading history...
153
        }
154
155 20
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by Zicht\Bundle\UrlBundle\U...or::translateKeyInput() of string.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
156
    }
157
158
159
    /**
160
     * Proxy method for translateValueInput() of the translator
161
     *
162
     * @param string $keyName
163
     * @param mixed $value
164
     * @return bool
165
     */
166 21
    final public function translateValueInput($keyName, $value)
167
    {
168 21
        if ($this->translator) {
169 2
            return $this->translator->translateValueInput($keyName, $value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->translator...Input($keyName, $value) returns the type string which is incompatible with the documented return type boolean.
Loading history...
170
        }
171
172 19
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by Zicht\Bundle\UrlBundle\U...::translateValueInput() of string.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
173
    }
174
175
176
    /**
177
     * Proxy method for translateKeyOutput() of the translator
178
     *
179
     * @param string $keyName
180
     * @return bool
181
     */
182 14
    final public function translateKeyOutput($keyName)
183
    {
184 14
        if ($this->translator) {
185 2
            return $this->translator->translateKeyOutput($keyName);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->translator...lateKeyOutput($keyName) returns the type string which is incompatible with the documented return type boolean.
Loading history...
186
        }
187
188 12
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by Zicht\Bundle\UrlBundle\U...r::translateKeyOutput() of string.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
189
    }
190
191
    /**
192
     * Proxy method for translateValueOutput() of the translator
193
     *
194
     * @param string $keyName
195
     * @param string $value
196
     * @return bool
197
     */
198 14
    final public function translateValueOutput($keyName, $value)
199
    {
200 14
        if ($this->translator) {
201 2
            return $this->translator->translateValueOutput($keyName, $value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->translator...utput($keyName, $value) returns the type string which is incompatible with the documented return type boolean.
Loading history...
202
        }
203
204 12
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by Zicht\Bundle\UrlBundle\U...:translateValueOutput() of string.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
205
    }
206
}
207