Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit f22bd65

Browse files
author
Jens Schulze
committed
fix(Query): fix undesired sort of query parameter sort
Closes #386
1 parent 2d1f6c4 commit f22bd65

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

src/Core/Request/AbstractApiRequest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ public function __construct(JsonEndpoint $endpoint, Context $context = null)
5353
$this->setEndpoint($endpoint);
5454
}
5555

56+
/**
57+
* @return int
58+
*/
59+
public function getParamCount()
60+
{
61+
return count($this->params);
62+
}
63+
5664
/**
5765
* @return string
5866
* @internal
@@ -142,7 +150,7 @@ function ($param) {
142150
},
143151
$params
144152
);
145-
sort($params);
153+
ksort($params);
146154
$params = implode('&', $params);
147155

148156
return $params;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* @author @jenschude <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\Query;
7+
8+
class OrderedMultiParameter extends Parameter
9+
{
10+
private $position;
11+
12+
public function __construct($key, $value = null, $position = 0)
13+
{
14+
$this->position = $position;
15+
parent::__construct($key, $value);
16+
}
17+
18+
public function getId()
19+
{
20+
return sprintf("%s-%010d", $this->key, $this->position);
21+
}
22+
}

src/Core/Request/SortTrait.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Commercetools\Core\Request;
88

99
use Commercetools\Core\Request\Query\MultiParameter;
10+
use Commercetools\Core\Request\Query\OrderedMultiParameter;
1011
use Commercetools\Core\Request\Query\ParameterInterface;
1112

1213
/**
@@ -20,14 +21,15 @@ trait SortTrait
2021
*/
2122
abstract public function addParamObject(ParameterInterface $param);
2223

24+
abstract public function getParamCount();
2325
/**
2426
* @param string $sort
2527
* @return $this
2628
*/
2729
public function sort($sort)
2830
{
2931
if (!is_null($sort)) {
30-
$this->addParamObject(new MultiParameter('sort', $sort));
32+
$this->addParamObject(new OrderedMultiParameter('sort', $sort, $this->getParamCount()));
3133
}
3234

3335
return $this;

tests/unit/Request/AbstractQueryRequestTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ public function testSort()
8484
$this->assertSame('test?sort=test', (string)$httpRequest->getUri());
8585
}
8686

87+
public function testMultiSort()
88+
{
89+
$request = $this->getQueryRequest();
90+
$request->sort('def')->sort('abc')->expand('test');
91+
$httpRequest = $request->httpRequest();
92+
93+
$this->assertSame('test?expand=test&sort=def&sort=abc', (string)$httpRequest->getUri());
94+
}
95+
8796
public function testLimit()
8897
{
8998
$request = $this->getQueryRequest();

0 commit comments

Comments
 (0)