Skip to content

Commit fb56997

Browse files
committed
routes: allow including searchable fields
1 parent 255562d commit fb56997

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

src/Jarischaefer/HalApi/Helpers/ResourceRoute.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php namespace Jarischaefer\HalApi\Helpers;
1+
<?php
2+
3+
namespace Jarischaefer\HalApi\Helpers;
24

35
/**
46
* Class ResourceRoute
@@ -207,17 +209,46 @@ public function rawDelete(string $uri, string $method): ResourceRoute
207209
}
208210

209211
/**
212+
* @param iterable $searchableFields
210213
* @return ResourceRoute
211214
*/
212-
public function searchable(): ResourceRoute
215+
public function searchable(iterable $searchableFields = null): ResourceRoute
213216
{
214217
$this->routeHelper
215218
->get($this->name . '/search', $this->controller, 'search')
216219
->get($this->name . '/search?' . self::PAGINATION_QUERY_STRING, $this->controller, 'search');
217220

221+
$searchableQueryString = $this->createSearchableQueryString($searchableFields);
222+
223+
if (!empty($searchableQueryString)) {
224+
$uri = $this->name . '/search?' . self::PAGINATION_QUERY_STRING . '&' . $searchableQueryString;
225+
$this->routeHelper->get($uri, $this->controller, 'search');
226+
}
227+
218228
return $this;
219229
}
220230

231+
/**
232+
* @param iterable $searchableFields
233+
* @return string
234+
*/
235+
private function createSearchableQueryString(iterable $searchableFields = null): string
236+
{
237+
if ($searchableFields === null) {
238+
return '';
239+
}
240+
241+
$string = '';
242+
$delimiter = '';
243+
244+
foreach ($searchableFields as $field) {
245+
$string .= $delimiter . $field . '={' . $field . '}';
246+
$delimiter = '&';
247+
}
248+
249+
return $string;
250+
}
251+
221252
/**
222253
* Call this method once you're done registering additional routes to the current resource.
223254
* The original RouteHelper instance is returned, allowing infinite chaining of ordinary routes and resources.

0 commit comments

Comments
 (0)