Skip to content

Commit 9d8764f

Browse files
committed
allows modifying Query
1 parent 20a2867 commit 9d8764f

2 files changed

Lines changed: 55 additions & 9 deletions

File tree

src/Components/Query.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
final class Query {
1414

1515
/**
16-
* @var QueryParameter[]
16+
* @var array<string, QueryParameter>
1717
*/
1818
private array $queryParameters;
1919

@@ -30,15 +30,17 @@ static function createFromString (string $query) : Query {
3030
* @param QueryParameter ...$queryParameters
3131
*/
3232
function __construct (QueryParameter ...$queryParameters) {
33-
$this->queryParameters = $queryParameters;
33+
foreach ($queryParameters as $queryParameter) {
34+
$this->queryParameters[$queryParameter->getKey()] = $queryParameter;
35+
}
3436
}
3537

3638
/**
3739
* @param string $key
3840
* @return bool
3941
*/
4042
function hasParameter (string $key) : bool {
41-
return $this->getParameter($key) !== null; // todo: we could build an index and read from that instead
43+
return isset($this->queryParameters[$key]);
4244
}
4345

4446
/**
@@ -47,13 +49,32 @@ function hasParameter (string $key) : bool {
4749
* @return QueryParameter|null
4850
*/
4951
function getParameter (string $key) : ?QueryParameter {
50-
foreach ($this->queryParameters as $parameter) {
51-
if ($parameter->getKey() === $key) {
52-
return $parameter;
53-
}
54-
}
52+
return $this->queryParameters[$key] ?? null;
53+
}
54+
55+
function withParameter (QueryParameter $queryParameter) : self {
56+
$instance = clone $this;
57+
$instance->queryParameters[$queryParameter->getKey()] = $queryParameter;
58+
59+
return $instance;
60+
}
61+
62+
function withParameterKeyRemoved (string $queryParameterKey) : self {
63+
$instance = clone $this;
64+
unset($instance->queryParameters[$queryParameterKey]);
5565

56-
return null;
66+
return $instance;
67+
}
68+
69+
/**
70+
* @throws InvalidArgumentException
71+
* @param string $key
72+
* @param mixed $value
73+
*
74+
* @return self
75+
*/
76+
function withParameterKeyAndValue (string $key, mixed $value) : self {
77+
return $this->withParameter(QueryParameter::create($key, $value));
5778
}
5879

5980
/**

tests/QueryComponentTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPUnit\Framework\TestCase;
66
use UriManage\Components\Query;
77
use UriManage\Components\QueryParameters\QueryParameter;
8+
use UriManage\Components\QueryParameters\StringQueryParameter;
89

910
class QueryComponentTest extends TestCase {
1011

@@ -69,4 +70,28 @@ function provideParseableQueryStrings () : \Generator {
6970
];
7071
}
7172

73+
function testExpectsQueryParameterToBeAdded () {
74+
$query = new Query(new StringQueryParameter('baz', 'boo'));
75+
$query = $query->withParameter(new StringQueryParameter('foo', 'bar'));
76+
77+
$this->assertTrue($query->hasParameter('foo'));
78+
$this->assertSame('baz=boo&foo=bar', (string) $query);
79+
}
80+
81+
function testExpectsQueryParameterKeyAndvalueToBeAdded () {
82+
$query = new Query(new StringQueryParameter('baz', 'boo'));
83+
$query = $query->withParameterKeyAndValue('foo', 'bar');
84+
85+
$this->assertTrue($query->hasParameter('foo'));
86+
$this->assertSame('baz=boo&foo=bar', (string) $query);
87+
}
88+
89+
function testExpectsQueryParameterToBeRemoved () {
90+
$query = new Query(new StringQueryParameter('foo', 'bar'), new StringQueryParameter('baz', 'boo'));
91+
$query = $query->withParameterKeyRemoved('foo');
92+
93+
$this->assertFalse($query->hasParameter('foo'));
94+
$this->assertSame('baz=boo', (string) $query);
95+
}
96+
7297
}

0 commit comments

Comments
 (0)