Skip to content

Added: order_by in search function #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/Resource/AbstractResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ private function allWithoutPagination()
* @return mixed Returns array of ZammadAPIClient\Resource\... objects
* or this object on failure.
*/
public function search( $search_term, $page = null, $objects_per_page = null )
public function search( $search_term, $page = null, $objects_per_page = null, $sort_by = null, $order_by = null )
{
if ( !empty( $this->getValues() ) ) {
throw new AlreadyFetchedObjectException('Object already contains values, search() not possible, use a new object');
Expand All @@ -406,7 +406,7 @@ public function search( $search_term, $page = null, $objects_per_page = null )
}

if ( !isset($page) || !isset($objects_per_page) ) {
return $this->searchWithoutPagination($search_term);
return $this->searchWithoutPagination($search_term, $sort_by, $order_by);
}

$url_parameters = [
Expand All @@ -419,6 +419,13 @@ public function search( $search_term, $page = null, $objects_per_page = null )
$url_parameters['per_page'] = $objects_per_page;
}

if ( isset($sort_by) ) {
$url_parameters['sort_by'] = $sort_by;
}
if ( isset($order_by) ) {
$url_parameters['order_by'] = $order_by;
}

$url = $this->getURL('search');
$response = $this->getClient()->get(
$url,
Expand Down Expand Up @@ -452,15 +459,15 @@ public function search( $search_term, $page = null, $objects_per_page = null )
* @return mixed Returns array of ZammadAPIClient\Resource\... objects
* or this object on failure.
*/
private function searchWithoutPagination($search_term)
private function searchWithoutPagination($search_term, $sort_by = null, $order_by = null)
{
$page = 1;
$objects_per_page = 100;
$objects = [];
$objects_of_page = [];

do {
$objects_of_page = $this->search( $search_term, $page, $objects_per_page );
$objects_of_page = $this->search( $search_term, $page, $objects_per_page, $sort_by, $order_by );
if ( !is_array($objects_of_page) ) {
return $this;
}
Expand Down