-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathQueryParam.php
90 lines (79 loc) · 2.17 KB
/
QueryParam.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
declare(strict_types=1);
namespace Core\Request\Parameters;
use Core\Utils\CoreHelper;
use CoreInterfaces\Core\Request\RequestArraySerialization;
use CoreInterfaces\Core\Request\RequestSetterInterface;
class QueryParam extends EncodedParam
{
/**
* Initializes a query parameter with the key and value provided.
*/
public static function init(string $key, $value): self
{
return new self($key, $value);
}
private function __construct(string $key, $value)
{
parent::__construct($key, $value, 'query');
}
/**
* Sets the parameter format to un-indexed.
*/
public function unIndexed(): self
{
$this->format = RequestArraySerialization::UN_INDEXED;
return $this;
}
/**
* Sets the parameter format to plain.
*/
public function plain(): self
{
$this->format = RequestArraySerialization::PLAIN;
return $this;
}
/**
* Sets the parameter format to comma separated.
*/
public function commaSeparated(): self
{
$this->format = RequestArraySerialization::CSV;
return $this;
}
/**
* Sets the parameter format to tab separated.
*/
public function tabSeparated(): self
{
$this->format = RequestArraySerialization::TSV;
return $this;
}
/**
* Sets the parameter format to pipe separated.
*/
public function pipeSeparated(): self
{
$this->format = RequestArraySerialization::PSV;
return $this;
}
/**
* Adds the parameter to the request provided.
*
* @param RequestSetterInterface $request The request to add the parameter to.
*/
public function apply(RequestSetterInterface $request): void
{
if (!$this->validated) {
return;
}
$value = CoreHelper::prepareValue($this->value);
$query = $this->httpBuildQuery([$this->key => $value], $this->format);
if (empty($query)) {
return;
}
$hasParams = (strrpos($request->getQueryUrl(), '?') > 0);
$separator = (($hasParams) ? '&' : '?');
$request->appendPath($separator . $query);
}
}