Completed
Push — master ( f56ee5...b16611 )
by ignace nyamagana
06:11
created

Query   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 248
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 20
Bugs 1 Features 0
Metric Value
wmc 26
c 20
b 1
f 0
lcom 1
cbo 3
dl 0
loc 248
ccs 59
cts 59
cp 1
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromArray() 0 4 1
A createFromPairs() 0 10 2
A __construct() 0 5 1
A validate() 0 9 2
A __debugInfo() 0 4 1
A __set_state() 0 7 1
A getContent() 0 8 3
A __toString() 0 4 1
A getUriComponent() 0 9 2
A modify() 0 8 2
A getValue() 0 8 2
A merge() 0 12 3
A ksort() 0 11 3
A newCollectionInstance() 0 8 2
1
<?php
2
/**
3
 * League.Uri (http://uri.thephpleague.com)
4
 *
5
 * @package   League.uri
6
 * @author    Ignace Nyamagana Butera <[email protected]>
7
 * @copyright 2013-2015 Ignace Nyamagana Butera
8
 * @license   https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License)
9
 * @version   4.2.0
10
 * @link      https://github.com/thephpleague/uri/
11
 */
12
namespace League\Uri\Components;
13
14
use League\Uri\Interfaces\Query as QueryInterface;
15
use League\Uri\QueryParser;
16
use League\Uri\Types\ImmutableCollectionTrait;
17
use League\Uri\Types\ImmutableComponentTrait;
18
19
/**
20
 * Value object representing a URI query component.
21
 *
22
 * @package League.uri
23
 * @author  Ignace Nyamagana Butera <[email protected]>
24
 * @since   1.0.0
25
 */
26
class Query implements QueryInterface
27
{
28
    use ImmutableComponentTrait;
29
30
    use ImmutableCollectionTrait;
31
32
    /**
33
     * Key/pair separator character
34
     *
35
     * @var string
36
     */
37
    protected static $separator = '&';
38
39
    /**
40
     * Preserve the delimiter
41
     *
42
     * @var bool
43
     */
44
    protected $preserveDelimiter = false;
45
46
    /**
47
     * DEPRECATION WARNING! This method will be removed in the next major point release
48
     *
49
     * @deprecated deprecated since version 4.2
50
     *
51
     * return a new instance from an array or a traversable object
52
     *
53
     * @param \Traversable|array $data
54
     *
55
     * @return static
56
     */
57
    public static function createFromArray($data)
58
    {
59
        return self::createFromPairs($data);
60
    }
61
62
    /**
63
     * return a new Query instance from an Array or a traversable object
64
     *
65
     * @param \Traversable|array $data
66
     *
67
     * @return static
68
     */
69 108
    public static function createFromPairs($data)
70
    {
71 108
        $query = null;
72 108
        $data = static::validateIterator($data);
73 102
        if (!empty($data)) {
74 81
            $query = (new QueryParser())->build($data, static::$separator, PHP_QUERY_RFC3986);
75 54
        }
76
77 102
        return new static($query);
78
    }
79
80
    /**
81
     * a new instance
82
     *
83
     * @param string $data
84
     */
85 898
    public function __construct($data = null)
86
    {
87 898
        $this->data = $this->validate($data);
88 898
        $this->preserveDelimiter = null !== $data;
89 898
    }
90
91
    /**
92
     * sanitize the submitted data
93
     *
94
     * @param string $str
95
     *
96
     * @return array
97
     */
98 898
    protected function validate($str)
99
    {
100 898
        if (null === $str) {
101 414
            return [];
102
        }
103
104 742
        return (new QueryParser())
105 742
            ->parse($this->validateString($str), static::$separator, PHP_QUERY_RFC3986);
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111 2
    public function __debugInfo()
112
    {
113 2
        return ['query' => $this->getContent()];
114
    }
115
116
    /**
117
     * @inheritdoc
118
     */
119 12
    public static function __set_state(array $properties)
120
    {
121 12
        $component = static::createFromArray($properties['data']);
0 ignored issues
show
Deprecated Code introduced by
The method League\Uri\Components\Query::createFromArray() has been deprecated with message: deprecated since version 4.2 return a new instance from an array or a traversable object

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
122 12
        $component->preserveDelimiter = $properties['preserveDelimiter'];
123
124 12
        return $component;
125
    }
126
127
    /**
128
     * Returns the component literal value.
129
     *
130
     * @return null|string
131
     */
132 805
    public function getContent()
133
    {
134 805
        if ([] === $this->data && false === $this->preserveDelimiter) {
135 429
            return null;
136
        }
137
138 652
        return (new QueryParser())->build($this->data, static::$separator, PHP_QUERY_RFC3986);
139
    }
140
141
    /**
142
     * Returns the instance string representation; If the
143
     * instance is not defined an empty string is returned
144
     *
145
     * @return string
146
     */
147 793
    public function __toString()
148
    {
149 793
        return (string) $this->getContent();
150
    }
151
152
    /**
153
     * Returns the instance string representation
154
     * with its optional URI delimiters
155
     *
156
     * @return string
157
     */
158 758
    public function getUriComponent()
159
    {
160 758
        $query = $this->__toString();
161 758
        if ($this->preserveDelimiter) {
162 599
            return QueryInterface::DELIMITER.$query;
163
        }
164
165 405
        return $query;
166
    }
167
168
    /**
169
     * Returns an instance with the specified string
170
     *
171
     * This method MUST retain the state of the current instance, and return
172
     * an instance that contains the modified data
173
     *
174
     * @param string $value
175
     *
176
     * @return static
177
     */
178 198
    public function modify($value)
179
    {
180 198
        if ($value === $this->getContent()) {
181 171
            return $this;
182
        }
183
184 27
        return new static($value);
185
    }
186
187
    /**
188
     * Retrieves a single query parameter.
189
     *
190
     * Retrieves a single query parameter. If the parameter has not been set,
191
     * returns the default value provided.
192
     *
193
     * @param string $offset  the parameter name
194
     * @param mixed  $default Default value to return if the parameter does not exist.
195
     *
196
     * @return mixed
197
     */
198 9
    public function getValue($offset, $default = null)
199
    {
200 9
        if (isset($this->data[$offset])) {
201 3
            return $this->data[$offset];
202
        }
203
204 6
        return $default;
205
    }
206
207
    /**
208
     * Returns an instance merge with the specified query
209
     *
210
     * This method MUST retain the state of the current instance, and return
211
     * an instance that contains the modified query
212
     *
213
     * @param Query|string $query the data to be merged query can be
214
     *                            - another Interfaces\Query object
215
     *                            - a string or a Stringable object
216
     *
217
     * @return static
218
     */
219 24
    public function merge($query)
220
    {
221 24
        if (!$query instanceof QueryInterface) {
222 6
            $query = static::createFromArray($this->validate($query));
0 ignored issues
show
Deprecated Code introduced by
The method League\Uri\Components\Query::createFromArray() has been deprecated with message: deprecated since version 4.2 return a new instance from an array or a traversable object

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
223 4
        }
224
225 24
        if ($this->sameValueAs($query)) {
226 3
            return $this;
227
        }
228
229 21
        return static::createFromArray(array_merge($this->toArray(), $query->toArray()));
0 ignored issues
show
Deprecated Code introduced by
The method League\Uri\Components\Query::createFromArray() has been deprecated with message: deprecated since version 4.2 return a new instance from an array or a traversable object

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
230
    }
231
232
    /**
233
     * Sort the query string by offset, maintaining offset to data correlations.
234
     *
235
     * This method MUST retain the state of the current instance, and return
236
     * an instance that contains the modified query
237
     *
238
     * @param callable|int $sort a PHP sort flag constant or a comparaison function
239
     *                           which must return an integer less than, equal to,
240
     *                           or greater than zero if the first argument is
241
     *                           considered to be respectively less than, equal to,
242
     *                           or greater than the second.
243
     *
244
     * @return static
245
     */
246 45
    public function ksort($sort = SORT_REGULAR)
247
    {
248 45
        $func = is_callable($sort) ? 'uksort' : 'ksort';
249 45
        $data = $this->data;
250 45
        $func($data, $sort);
251 45
        if ($data === $this->data) {
252 33
            return $this;
253
        }
254
255 12
        return static::createFromArray($data);
0 ignored issues
show
Deprecated Code introduced by
The method League\Uri\Components\Query::createFromArray() has been deprecated with message: deprecated since version 4.2 return a new instance from an array or a traversable object

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
256
    }
257
258
    /**
259
     * Return a new instance when needed
260
     *
261
     * @param array $data
262
     *
263
     * @return static
264
     */
265 42
    protected function newCollectionInstance(array $data)
266
    {
267 42
        if ($data == $this->data) {
268 15
            return $this;
269
        }
270
271 27
        return static::createFromArray($data);
0 ignored issues
show
Deprecated Code introduced by
The method League\Uri\Components\Query::createFromArray() has been deprecated with message: deprecated since version 4.2 return a new instance from an array or a traversable object

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
272
    }
273
}
274