-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSearchManager.php
More file actions
91 lines (85 loc) · 3.46 KB
/
Copy pathSearchManager.php
File metadata and controls
91 lines (85 loc) · 3.46 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace SimpleSquid\Vend\Actions;
use SimpleSquid\Vend\Resources\TwoDotZero\CustomerCollection;
use SimpleSquid\Vend\Resources\TwoDotZero\ProductCollection;
use SimpleSquid\Vend\Resources\TwoDotZero\SalesCollection;
use SimpleSquid\Vend\Resources\TwoDotZero\Search;
use Spatie\DataTransferObject\DataTransferObjectCollection;
class SearchManager
{
use ManagesResources;
/**
* Search for resources.
* This endpoint allows integrators to search all of the most commonly used resources, **sales**, **products** and **customers**. Each type allowing search by a number of different parameters.
*
* ### Supported resource types and attributes
*
* **Sales**
* - date_from
* - date_to
* - status
* - invoice_number
* - customer_id
* - user_id
* - outlet_id
*
* **Products**
* - sku
* - supplier_id
* - brand_id
* - tag_id
* - product_type_id
* - variant_parent_id
*
* **Customers**
* - customer_code
* - first_name
* - last_name
* - company_name
*
* ### Sorting and pagination
*
* Unlike other endpoints in the API 2.0, search results from this endpoint can be sorted by any of the attributes above. Because of that, the default pagination mechanism is not appropriate for this endpoint. Instead, this endpoint uses `offset` and `page_size` attributes to handle search results spanning multiple pages.
*
* @param string $type The entity type to search for. One of: `sales`, `products`, `customers`.
* @param Search|array $query The search query.
* @param int|null $page_size The maximum number of objects to be included in the response, currently limited to 10000.
* @param int|null $offset The number of objects to be "skipped" for the response. Used for pagination.
* @param string|null $order_by The attribute used to sort items returned in the response.
* @param string|null $order_direction Sorting direction. One of: `asc`, `desc`.
*
* @return DataTransferObjectCollection
*
* @throws \SimpleSquid\Vend\Exceptions\AuthorisationException
* @throws \SimpleSquid\Vend\Exceptions\BadRequestException
* @throws \SimpleSquid\Vend\Exceptions\NotFoundException
* @throws \SimpleSquid\Vend\Exceptions\RateLimitException
* @throws \SimpleSquid\Vend\Exceptions\RequestException
* @throws \SimpleSquid\Vend\Exceptions\TokenExpiredException
* @throws \SimpleSquid\Vend\Exceptions\UnauthorisedException
* @throws \SimpleSquid\Vend\Exceptions\UnknownException
*/
public function find(
string $type,
$query,
int $page_size = null,
int $offset = null,
string $order_by = null,
string $order_direction = null
): DataTransferObjectCollection {
if ($type === 'sales') {
$collection = SalesCollection::class;
} elseif ($type === 'products') {
$collection = ProductCollection::class;
} elseif ($type === 'customers') {
$collection = CustomerCollection::class;
} else {
return null;
}
if ($query instanceof Search) {
$query = $query->toArray();
}
return $this->collection($collection, '2.0/search',
array_merge($query, compact('type', 'page_size', 'offset', 'order_by', 'order_direction')));
}
}