Skip to content

Commit b209b48

Browse files
author
Antoine Aflalo
committed
perf(Arguments): Refactor the way the arguments works
To be sure we can work with SubKeyedArg that are having array as value.
1 parent 0efa70c commit b209b48

8 files changed

Lines changed: 27 additions & 115 deletions

File tree

src/Request/Api/BaseRequest.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
use ZEROSPAM\Framework\SDK\Request\Arguments\Mergeable\IMergeableArgument;
1515
use ZEROSPAM\Framework\SDK\Request\Arguments\Mergeable\Worker\ArgMerger;
1616
use ZEROSPAM\Framework\SDK\Request\Arguments\Stackable\IStackableArgument;
17-
use ZEROSPAM\Framework\SDK\Request\Arguments\Stackable\IStackableArrayArgument;
18-
use ZEROSPAM\Framework\SDK\Request\Arguments\Stackable\Worker\ArgArrayCollector;
1917
use ZEROSPAM\Framework\SDK\Request\Arguments\Stackable\Worker\ArgCollector;
2018
use ZEROSPAM\Framework\SDK\Request\Type\RequestType;
2119
use ZEROSPAM\Framework\SDK\Response\Api\IResponse;
@@ -44,10 +42,6 @@ abstract class BaseRequest implements IRequest
4442
* @var ArgCollector[]
4543
*/
4644
private $stackableArguments = [];
47-
48-
/** @var ArgArrayCollector[] */
49-
private $stackableArrayArguments = [];
50-
5145
/**
5246
* @var \ZEROSPAM\Framework\SDK\Response\Api\IResponse
5347
*/
@@ -80,15 +74,7 @@ public function addArgument(IArgument $arg): IRequest
8074
return $this;
8175
}
8276

83-
if ($arg instanceof IStackableArrayArgument) {
84-
if (!isset($this->stackableArrayArguments[$arg->getKey()])) {
85-
$this->stackableArrayArguments[$arg->getKey()] = new ArgArrayCollector();
86-
}
87-
88-
$this->stackableArrayArguments[$arg->getKey()]->addArgument($arg);
89-
90-
return $this;
91-
} elseif ($arg instanceof IStackableArgument) {
77+
if ($arg instanceof IStackableArgument) {
9278
if (!isset($this->stackableArguments[$arg->getKey()])) {
9379
$this->stackableArguments[$arg->getKey()] = new ArgCollector();
9480
}
@@ -293,10 +279,6 @@ private function generateUrl(): string
293279
$query[$key] = $value->toArray();
294280
}
295281

296-
foreach ($this->stackableArrayArguments as $key => $value) {
297-
$query[$key] = $value->toArray();
298-
}
299-
300282
$queryArg = http_build_query($query, null, '&', PHP_QUERY_RFC3986);
301283

302284
$fullUrl = $this->routeUrl() . '?' . preg_replace($re, $subst, $queryArg);

src/Request/Arguments/IArgument.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,11 @@ interface IArgument extends PrimalValued
2525
* @return string
2626
*/
2727
public function getKey(): string;
28+
29+
/**
30+
* Possible Subkey of the argument
31+
*
32+
* @return string
33+
*/
34+
public function getSubKey(): string;
2835
}

src/Request/Arguments/RequestArg.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,14 @@ public function toPrimitive()
7272

7373
return $value;
7474
}
75+
76+
/**
77+
* Possible Subkey of the argument
78+
*
79+
* @return string
80+
*/
81+
public function getSubKey(): string
82+
{
83+
return $this->toPrimitive();
84+
}
7585
}

src/Request/Arguments/Stackable/ISubKeyedStackableArgument.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,5 @@
1010

1111
interface ISubKeyedStackableArgument extends IStackableArgument
1212
{
13-
/**
14-
* Possible Subkey of the argument
15-
*
16-
* @return string
17-
*/
18-
public function getSubKey(): string;
13+
1914
}

src/Request/Arguments/Stackable/Worker/ArgArrayCollector.php

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/Request/Arguments/Stackable/Worker/ArgCollector.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,9 @@ protected function stackKey(): string
4141
}
4242

4343
/**
44-
* Add the argument.
45-
*
4644
* @param IStackableArgument $argument
4745
*
48-
* @return $this
46+
* @return $this|ArgCollector
4947
*/
5048
public function addArgument(IStackableArgument $argument)
5149
{
@@ -57,18 +55,16 @@ public function addArgument(IStackableArgument $argument)
5755
} else {
5856
$key = $this->stackKey();
5957
}
60-
$primitive = $argument->toPrimitive();
61-
$this->args[$key][$primitive] = $argument;
58+
59+
$this->args[$key][$argument->getSubKey()] = $argument;
6260

6361
return $this;
6462
}
6563

6664
/**
67-
* Remove the argument.
68-
*
6965
* @param IStackableArgument $argument
7066
*
71-
* @return $this
67+
* @return $this|ArgCollector
7268
*/
7369
public function removeArgument(IStackableArgument $argument)
7470
{
@@ -78,7 +74,7 @@ public function removeArgument(IStackableArgument $argument)
7874
$key = $this->stackKey();
7975
}
8076

81-
unset($this->args[$key][$argument->toPrimitive()]);
77+
unset($this->args[$key][$argument->getSubKey()]);
8278
if (empty($this->args[$key])) {
8379
unset($this->args[$key]);
8480
}

tests/src/Base/Argument/SearchKeyedArg.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
class SearchKeyedArg extends SubKeyedStackableRequestArg
2121
{
2222

23-
2423
public function __construct(string $subKey, string $value)
2524
{
2625
parent::__construct('search', $subKey, $value);

tests/src/Base/Argument/TestMergeableArg.php

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace ZEROSPAM\Framework\SDK\Test\Base\Argument;
1010

1111
use ZEROSPAM\Framework\SDK\Request\Arguments\Mergeable\IMergeableArgument;
12+
use ZEROSPAM\Framework\SDK\Request\Arguments\RequestArg;
1213

1314
/**
1415
* Class TestMergeableArg
@@ -17,7 +18,7 @@
1718
*
1819
* @package ZEROSPAM\Framework\SDK\Test\Base\Argument
1920
*/
20-
class TestMergeableArg implements IMergeableArgument
21+
class TestMergeableArg extends RequestArg implements IMergeableArgument
2122
{
2223
/**
2324
* @var string
@@ -31,17 +32,7 @@ class TestMergeableArg implements IMergeableArgument
3132
*/
3233
public function __construct(string $obj)
3334
{
34-
$this->obj = $obj;
35-
}
36-
37-
/**
38-
* Key for the argument.
39-
*
40-
* @return string
41-
*/
42-
public function getKey(): string
43-
{
44-
return 'mergeArg';
35+
parent::__construct('mergeArg', $obj);
4536
}
4637

4738
/**
@@ -53,14 +44,4 @@ public static function glue()
5344
{
5445
return ';';
5546
}
56-
57-
/**
58-
* Return a primitive value for this object.
59-
*
60-
* @return int|float|string|float
61-
*/
62-
public function toPrimitive()
63-
{
64-
return $this->obj;
65-
}
6647
}

0 commit comments

Comments
 (0)