Passed
Pull Request — release/4.x (#44)
by Erik
07:43 queued 03:55
created

Params::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
use Zicht\Bundle\FrameworkExtraBundle\Util\SortedSetMap;
10
11
/**
12
 * Abstracts faceted uri's into a Map construct, with each key in the map corresponding to the name of the
13
 * facet and each facet represented as a sorted set.
14
 *
15
 * The with() method will return a clone of the instance with a value for the given key added
16
 * or removed. It is added if it wasn't yet in the set, it is removed if it was.
17
 *
18
 * Usage:
19
 *
20
 *      $uri = new Zicht_Util_FacetUri('a=1/b=2/c=3,4');
21
 *
22
 *      $uri->with('a', 2); // will return a new instance with "a=1,2/b=2/c=3,4"
23
 *      $uri->with('c', 3); // will return a new instance with "a=1/b=2/c=4"
24
 */
25
class Params extends SortedSetMap
0 ignored issues
show
Deprecated Code introduced by
The class Zicht\Bundle\FrameworkEx...undle\Util\SortedSetMap has been deprecated: Use sorting, unique, and mapBy functionality from `zicht/itertools` instead ( Ignorable by Annotation )

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

25
class Params extends /** @scrutinizer ignore-deprecated */ SortedSetMap
Loading history...
26
{
27
    /**
28
     * @var UriParser
29
     */
30
    protected $parser;
31
32
    /**
33
     * Construct the object and parse the URI.
34
     *
35
     * Each of the separator parameters is used for providing different formats of the URI. These separators
36
     * will be used to generate a string of the object too.
37
     *
38
     * @param null|UriParser $parser
39
     * @return Params
40
     */
41
    public function __construct(UriParser $parser = null)
42
    {
43
        parent::__construct();
44
        if (is_null($parser)) {
45
            $parser = new UriParser();
46
        }
47
        $this->parser = $parser;
48
    }
49
50
51
    /**
52
     * Parses the uri and assigns the parsed values.
53
     *
54
     * @param string $uri
55
     * @return void
56
     */
57
    public function setUri($uri)
58
    {
59
        $this->setValues($this->parser->parseUri($uri));
60
    }
61
62
63
    /**
64
     * Process the POST and merges the (translated) values.
65
     *
66
     * @param array $post
67
     * @return void
68
     */
69
    public function mergePost($post)
70
    {
71
        $this->mergeAll($this->parser->parsePost($post));
72
    }
73
74
75
    /**
76
     * Duplicates the current instance with one value changed.
77
     *
78
     * - If the value already exists, the value is removed;
79
     * - If the value does not exist, it is added;
80
     * - If the value exists, and 'multiple' is false, it is replaced.
81
     * - If the value does not exists, and 'multiple' is false, it is added.
82
     *
83
     * The $multiple parameter is typically useful for paging or other parameters.
84
     *
85
     * @param string $key
86
     * @param string $value
87
     * @param bool $multiple
88
     * @return Params
89
     */
90
    public function with($key, $value, $multiple = true)
91
    {
92
        $ret = clone $this;
93
        $ret = self::doWith($ret, $key, $value, $multiple);
94
95
        return $ret;
96
    }
97
98
99
    /**
100
     * Helper for with()
101
     *
102
     * @param Params $ret
103
     * @param string $key
104
     * @param string $value
105
     * @param bool $multiple
106
     * @return Params
107
     */
108
    private static function doWith(Params $ret, $key, $value, $multiple = true)
109
    {
110
        if (!$multiple) {
111
            if (!is_scalar($value)) {
0 ignored issues
show
introduced by
The condition is_scalar($value) is always true.
Loading history...
112
                throw new \InvalidArgumentException(
113
                    'Invalid argument $value to with(), expected scalar, got ' . gettype($value)
114
                );
115
            }
116
            if ($ret->contains($key, $value)) {
117
                $ret->remove($key, $value);
118
            } else {
119
                $ret->replace($key, [$value]);
120
            }
121
        } else {
122
            if ($ret->contains($key, $value)) {
123
                $ret->remove($key, $value);
124
            } else {
125
                $ret->add($key, $value);
126
            }
127
        }
128
129
        return $ret;
130
    }
131
132
133
    /**
134
     * Returns a single value, and defaults to the given default parameter if not available.
135
     *
136
     * @param string $key
137
     * @param mixed $default
138
     * @return mixed
139
     */
140
    public function getOne($key, $default = null)
141
    {
142
        $ret = $default;
143
        $all = $this->get($key);
144
        if (count($all) > 0) {
145
            $ret = array_shift($all);
146
        }
147
148
        return $ret;
149
    }
150
151
152
    /**
153
     * Duplicates the current instance with one or more sets of values removed
154
     *
155
     * @param mixed $keys
156
     * @return Params
157
     */
158
    public function without($keys)
159
    {
160
        if (is_scalar($keys)) {
161
            $keys = [$keys];
162
        }
163
        $ret = clone $this;
164
        foreach ($keys as $key) {
165
            $ret->removeKey($key);
166
        }
167
168
        return $ret;
169
    }
170
171
172
    /**
173
     * Returns all keys in the current set.
174
     *
175
     * @return array
176
     */
177
    public function getKeys()
178
    {
179
        return array_keys($this->toArray());
180
    }
181
182
    /**
183
     * Renders a string of the URI, joining it using the separators provided at construction time.
184
     *
185
     * @return string
186
     */
187
    public function __toString()
188
    {
189
        return $this->parser->composeUri($this->toArray());
0 ignored issues
show
Bug introduced by
$this->toArray() of type array is incompatible with the type Zicht\Bundle\UrlBundle\Url\Params\Params expected by parameter $params of Zicht\Bundle\UrlBundle\U...UriParser::composeUri(). ( Ignorable by Annotation )

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

189
        return $this->parser->composeUri(/** @scrutinizer ignore-type */ $this->toArray());
Loading history...
190
    }
191
}
192