Skip to content

Commit 5dbe2fa

Browse files
committed
added putDataInQueryString to Helper
1 parent 712e5c1 commit 5dbe2fa

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

spec/HelperSpec.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ function it_is_initializable()
7474
$this->shouldHaveType('LaraPackage\RandomId\Helper');
7575
}
7676

77+
function it_puts_data_in_a_query_string()
78+
{
79+
$queryString = 'foo={bar}&baz={sugar}';
80+
$data = ['bar' => 2, 'useless' => 8, 'sugar' => 9];
81+
$expected = 'foo=2&baz=9';
82+
83+
$this->putDataInQueryString($queryString, $data)->shouldReturn($expected);
84+
}
85+
7786
function it_puts_random_ids_into_the_payload()
7887
{
7988
$ids = [1, 2];

src/Contracts/Helper.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ public function getRandomIdsForPayload(array $payload, $idPlaceholder = '{random
4444
*/
4545
public function getRandomIdsForUri($uri, \Closure $idOverride = null, $idPlaceholder = '{random_id}');
4646

47+
/**
48+
* Replaces placeholders in query string with values from data
49+
*
50+
* @param string $queryString
51+
* @param array $data assoc array; the placeholders are used to pull values from here
52+
*
53+
* @return string the new query string
54+
*/
55+
public function putDataInQueryString($queryString, array $data);
56+
4757
/**
4858
* Takes $ids and replaces $randomIdString sequentially with them in the $payload
4959
*

src/Helper.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,29 @@ public function getRandomIdsForUri($uri, \Closure $idOverride = null, $idPlaceho
101101
);
102102
}
103103

104+
/**
105+
* @inheritdoc
106+
*/
107+
public function putDataInQueryString($queryString, array $data)
108+
{
109+
$returnQueries = [];
110+
111+
$queries = explode('&', $queryString);
112+
113+
foreach ($queries as $query) {
114+
$queryArray = explode('=', $query);
115+
$key = $queryArray[0];
116+
$placeHolder = $queryArray[1];
117+
118+
$valueKey = str_replace(['{', '}'], ['', ''], $placeHolder);
119+
$value = $data[$valueKey];
120+
121+
$returnQueries[] = $key.'='.$value;
122+
}
123+
124+
return implode('&', $returnQueries);
125+
}
126+
104127
/**
105128
* @inheritdoc
106129
*/

0 commit comments

Comments
 (0)