Filtering and sorting - 2Parale/2Performant-php GitHub Wiki

For requests that return a set of items (e.g. $advertiser->getCommissions()), you can add filters and sorting options to your requests (as per the documentation) by using specialized classes.

The modifier class name is given by

  • the requesting User's role (e.g. Affiliate)
  • the returned type of objects - in singular form (e.g. Banner)
  • the modifier type (Filter or Sort)

Filters

Therefore, to apply a filter to the Advertiser::getCommissions() method, you need to provide a first parameter constructed like this:

use TPerformant\API\Filters\AdvertiserCommissionFilter;

...

$filter = new AdvertiserCommissionFilter();
$result = $advertiser->getCommissions($filter->status('pending'));

The methods for each filter class depend on the capabilities of the API. Please consult the API documentation in order to see what filters can be applied to each object type.

If a filter uses underscore notation in the HTTP request, then it must be triggered using equivalent camelCase notation in the PHP code. I.e. if an affiliate wants to filter product feeds by program_id, this is the code to do it:

$feeds = $affiliate->getProductFeeds((new \TPerformant\API\Filters\AffiliateProductFeedFilter)->programId(134));

Sorting

To apply sorting, a second parameter must be provided, constructed like so:

$sorting = new AdvertiserCommissionSort();
$result = $advertiser->getCommissions(null, $sorting->dateDesc());

Note that:

  • if no filters should be applied, then the first parameter must be null
  • sorting methods have to end in Asc or Desc depending on the desired sort order
  • filter classes must either be loaded using a use directive or have their full namespaced name specified

The sorting method name is constructed the same way as filters, by converting underscore notation to camelCase notation on the sortable fields in the API docs.