Swagger API Личный кабинет Центр поддержки

Filtering in queries #

Description #

Basically the filter is passed in requests https://api.apiship.ru/v1/lists/* as a get parameter. По фильтру можно получить определенную запись по условию из фильтра. Пример фильтра. Example of a filter, https://api.apiship.ru/v1/lists/points?filter=city=Москва;providerKey=cdek. With this request, the user will receive the points of the sdek pvz in the city of moscow.

Syntax #

This string filter=city=Москва;providerKey=cdek нis called a filter, what is contained inside the filter is called a condition[s]. For example, this filter contains the conditions city=Moscow and providerKey=cdek.

The condition consists of 3 sections: key, operator, and value. Thus, the condition ’ city=Moscow` can be divided into sections:

  • Ключ - city
  • Оператор - =
  • Значение - Moscow

Уach following condition must be concatenated to the filter via the ‘;’ separator. Such a filter is valid filter=city=Москва;providerKey=cdek, and such is not filter=city=Москва;providerKey=cdek;.

Table of available values:

TypeExampleNote
Array[one,two,three]If the array has more than 1 value, only the ’ = ’ operator can be used.
Inside the array, the separator is ,.
Stringone
'one'
"one"
As a value, String can be enclosed in quotation marks ' or ".
Number0
0.1

Table of available operators:

OperatorAvailable typesDEscription
=Array, String, Number
>Number, String
<Number, String
%Number, StringSearch by parts. The operator works like “%value% " in SQL

Realisation example #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
 
// Array of filter conditions.
$conditions = [
    [
        'key'      => 'city',
        'operator' => '=',
        // Value tupe String
        'value'    => 'Moscow'
    ],
    [
        'key'      => 'providerKey',
        'operator' => '=',
        // Value type Array
        'value'    => ['cdek','boxberry']
    ],
];
//  Array of conditions as a string.
$stringConditions = [];
foreach($conditions as $condition){
    $key = $condition['key'];
    $operator = $condition['operator'];
    $value = $condition['value'];
 
    $stringConditions[] = sprintf(
        // Template for printing 3 sections.
        '%s%s%s',
        // 1 section: key.
        $key,
        // 2 section: operator.
        $operator,
        // section 3: value. If the type is Array, then print as "[<values through , >]", otherwise print "as is".
        is_array($value) ? sprintf('[%s]',implode(',',$value)) : $value
    );
}
// Print the filter, all conditions are concatenated to each other via a separator (;).
$filter = sprintf('filter=%s',implode(';',$stringConditions));
// As a result, we get the filter:  "filter=city=Москва;providerKey=[cdek,boxberry]".
echo $filter;