PicnicApi/src/PicnicClient.php

63 lines
1.6 KiB
PHP

<?php
namespace Ardent\PicnicApi;
use Ardent\PicnicApi\Objects\ApiListTransformer;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
class PicnicClient
{
private Client $client;
public function __construct(string $username, string $password)
{
$cb = new ClientBuilder($username, $password);
$this->client = $cb->getClient();
}
public function getUser()
{
return $this->get('user');
}
public function getCart()
{
return ApiListTransformer::getObject($this->get('cart'));
}
/**
* Returns all past and current deliveries of the user.
*
* @param bool $summary Return a summary (less data).
* @param array $filters An array with the statusses of the deliveries. For example; ['COMPLETED'] will only get completed deliveries. Possible options include CURRENT, COMPLETED and CANCELLED.
*/
public function getDeliveries(bool $summary = false, array $filters = []): array
{
$summary = $summary ? '/summary' : '';
return $this->post('deliveries' . $summary, $filters);
}
public function search(string $term)
{
return ApiListTransformer::getObjects($this->get(sprintf('search?search_term=%s', urlencode($term))));
}
private function post(string $url, array $data): array
{
$data = $this->client->post($url, [
RequestOptions::JSON => $data,
]);
return json_decode($data->getBody()->getContents(), true);
}
private function get(string $url): array
{
$data = $this->client->get($url);
return json_decode($data->getBody()->getContents(), true);
}
}