Skip to content

Commit 231cf64

Browse files
committed
Fix filter url when using filter attribute multiple times
1 parent 559cfb0 commit 231cf64

File tree

3 files changed

+84
-4
lines changed

3 files changed

+84
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 2.2.2
2+
- Fix filter url when using filter attribute multiple times
3+
- Add unit test for FilterTest->getUrl
4+
15
# 2.2.1
26
- Fix RelationshipsData->append behaviour with to-many relationships, to store data in jsonapi normalized specification form
37

src/Directive/Filter.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ public function __construct(
132132

133133
/**
134134
* @return string
135+
* @throws \Exception when unknown filter attribute class
136+
* @version 2.2.2
135137
*/
136138
public function getURL(): string
137139
{
@@ -160,19 +162,19 @@ public function getURL(): string
160162
switch (get_class($attribute)) {
161163
case FilterAttribute::class:
162164
$parts[] = sprintf(
163-
'filter[%s]=%s%s',
165+
'filter[%s][]=%s%s',
164166
urlencode($attribute->getAttribute()),
165167
urlencode($attribute->getOperator()),
166-
urlencode($attribute->getOperand())
168+
urlencode((string) $attribute->getOperand())
167169
);
168170
break;
169171
case FilterJSONAttribute::class:
170172
$parts[] = sprintf(
171-
'filter[%s.%s]=%s%s',
173+
'filter[%s.%s][]=%s%s',
172174
urlencode($attribute->getAttribute()),
173175
urlencode($attribute->getKey()),
174176
urlencode($attribute->getOperator()),
175-
urlencode($attribute->getOperand())
177+
urlencode((string) $attribute->getOperand())
176178
);
177179
break;
178180
default:

tests/src/Directive/FilterTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Phramework\JSONAPI\Client\Directive;
4+
5+
use Phramework\JSONAPI\Directive\FilterAttribute;
6+
use Phramework\Operator\Operator;
7+
8+
/**
9+
* @since 2.2.2
10+
* @author Xenofon Spafaridis <[email protected]>
11+
* @coversDefaultClass \Phramework\JSONAPI\Client\Directive\Filter(
12+
*/
13+
class FilterTest extends \PHPUnit_Framework_TestCase
14+
{
15+
public function provider() : array
16+
{
17+
return [
18+
[
19+
new Filter(
20+
'user',
21+
['1', '2']
22+
),
23+
'filter[user]=1,2'
24+
],
25+
[
26+
new Filter(
27+
'article',
28+
['1', '2'],
29+
(object) [
30+
'author' => ['1']
31+
]
32+
),
33+
'filter[article]=1,2&filter[author]=1'
34+
],
35+
[
36+
new Filter(
37+
'',
38+
[],
39+
(object) [
40+
'author' => ['1', '2']
41+
],
42+
[
43+
new FilterAttribute(
44+
'created',
45+
Operator::GREATER_EQUAL,
46+
100
47+
),
48+
new FilterAttribute(
49+
'created',
50+
Operator::LESS,
51+
100000
52+
)
53+
]
54+
),
55+
sprintf(
56+
'filter[author]=1,2&filter[created][]=%s100&filter[created][]=%s100000',
57+
urlencode('>='),
58+
urlencode('<')
59+
)
60+
]
61+
];
62+
}
63+
64+
/**
65+
* @dataProvider provider
66+
*/
67+
public function testGetUrl(Filter $filter, string $expected)
68+
{
69+
$this->assertSame(
70+
$filter->getURL(),
71+
$expected
72+
);
73+
}
74+
}

0 commit comments

Comments
 (0)