Skip to content

Commit

Permalink
Update to support Balikobot API v1.873 - Add country parameter to bra…
Browse files Browse the repository at this point in the history
…nches request
  • Loading branch information
tomas-novotny committed Nov 17, 2019
1 parent a0afa40 commit cb17aca
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 24 deletions.
24 changes: 20 additions & 4 deletions docs/balikobot.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,21 @@ Several methods can be used to obtain branches for supported shippers.
These methods return a large amount of data, therefore, these methods use [Generator](http://php.net/manual/en/class.generator.php) via [`yield`](http://php.net/manual/en/language.generators.syntax.php) keyword for returned data.
It saves a lot of memory and allows you to iterate all branches at one time in one cycle.

All methods allow filter branches by country.

```php
$shippers = $balikobot->getBranches():
$shippers = $balikobot->getBranchesForShipper(Shipper::CP);
$shippers = $balikobot->getBranchesForShipperService(Shipper::CP, ServiceType::CP_NP);

$shippers = $balikobot->getBranches(Country::CZECH_REPUBLIC):
$shippers = $balikobot->getBranchesForShipper(Shipper::ZASILKOVNA, Country::CZECH_REPUBLIC);
$shippers = $balikobot->getBranchesForShipperService(Shipper::ZASILKOVNA, null, Country::CZECH_REPUBLIC);

$shippers = $balikobot->getBranchesForCountries([Country::CZECH_REPUBLIC, Country::SLOVAKIA]):
$shippers = $balikobot->getBranchesForShipperForCountries(Shipper::ZASILKOVNA, [Country::CZECH_REPUBLIC, Country::SLOVAKIA]);
$shippers = $balikobot->getBranchesForShipperServiceForCountries(Shipper::ZASILKOVNA, null, [Country::CZECH_REPUBLIC, Country::SLOVAKIA]);

/*
var_dump($shippers);
Generator {}
Expand All @@ -175,7 +185,7 @@ Different shippers use different attribute as **branch_id** in [**ADD**](./clien

```php
$shippers = $balikobot->getBranchesForShipper(Shipper::CP):
í

foreach($shippers as $shipper) {

/*
Expand Down Expand Up @@ -234,11 +244,17 @@ getServices(string $shipper): array

getManipulationUnits(string $shipper): array

getBranches(): iterable
getBranches(string $country = null): iterable

getBranchesForCountries(array $countries): iterable

getBranchesForShipper(string $shipper, string $country = null): iterable

getBranchesForShipperForCountries(string $shipper, array $countries): iterable

getBranchesForShipper(string $shipper): iterable
getBranchesForShipperService(string $shipper, ?string $service, string $country = null): iterable

getBranchesForShipperService(string $shipper, ?string $service): iterable
getBranchesForShipperServiceForCountries(string $shipper, ?string $service, array $countries): iterable

getBranchesForLocation(string $shipper, string $country, string $city, string $postcode = null, string $street = null, int $maxResults = null, float $radius = null, string $type = null): iterable

Expand Down
2 changes: 1 addition & 1 deletion docs/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ getServices(string $shipper): array

getManipulationUnits(string $shipper): array

getBranches(string $shipper, string $service = null, bool $fullData = false): array
getBranches(string $shipper, string $service = null, bool $fullData = false, string $country = null): array

getBranchesForLocation(string $shipper, string $country, string $city, string $postcode = null, string $street = null, int $maxResults = null, float $radius = null, string $type = null): array

Expand Down
69 changes: 62 additions & 7 deletions src/Services/Balikobot.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,30 +344,49 @@ public function getManipulationUnits(string $shipper): array
/**
* Get all available branches
*
* @param string|null $country
*
* @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
*
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
*/
public function getBranches(): iterable
public function getBranches(string $country = null): iterable
{
// get all shipper service codes
$shippers = $this->getShippers();

foreach ($shippers as $shipper) {
yield from $this->getBranchesForShipper($shipper);
yield from $this->getBranchesForShipper($shipper, $country);
}
}

/**
* Get all available branches for countries
*
* @param array $countries
*
* @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
*
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
*/
public function getBranchesForCountries(array $countries): iterable
{
foreach ($countries as $country) {
yield from $this->getBranches($country);
}
}

/**
* Get all available branches for given shipper
*
* @param string $shipper
* @param string $shipper
* @param string|null $country
*
* @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
*
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
*/
public function getBranchesForShipper(string $shipper): iterable
public function getBranchesForShipper(string $shipper, string $country = null): iterable
{
// get all services for shipper service
$services = array_keys($this->getServices($shipper));
Expand All @@ -379,7 +398,24 @@ public function getBranchesForShipper(string $shipper): iterable

// get branches for all services
foreach ($services as $service) {
yield from $this->getBranchesForShipperService($shipper, $service);
yield from $this->getBranchesForShipperService($shipper, $service, $country);
}
}

/**
* Get all available branches for given shipper for countries
*
* @param string $shipper
* @param array $countries
*
* @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
*
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
*/
public function getBranchesForShipperForCountries(string $shipper, array $countries): iterable
{
foreach ($countries as $country) {
yield from $this->getBranchesForShipper($shipper, $country);
}
}

Expand All @@ -388,21 +424,40 @@ public function getBranchesForShipper(string $shipper): iterable
*
* @param string $shipper
* @param string|null $service
* @param string|null $country
*
* @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
*
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
*/
public function getBranchesForShipperService(string $shipper, ?string $service): iterable
public function getBranchesForShipperService(string $shipper, ?string $service, string $country = null): iterable
{
$fullData = Shipper::hasFullBranchesSupport($shipper, $service);
$branches = $this->client->getBranches($shipper, $service, $fullData);
$branches = $this->client->getBranches($shipper, $service, $fullData, $country);

foreach ($branches as $branch) {
yield Branch::newInstanceFromData($shipper, $service, $branch);
}
}

/**
* Get all available branches for given shipper and service type for countries
*
* @param string $shipper
* @param string|null $service
* @param array $countries
*
* @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
*
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
*/
public function getBranchesForShipperServiceForCountries(string $shipper, ?string $service, array $countries): iterable
{
foreach ($countries as $country) {
yield from $this->getBranchesForShipperService($shipper, $service, $country);
}
}

/**
* Get all available branches for given shipper
*
Expand Down
5 changes: 3 additions & 2 deletions src/Services/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,16 +382,17 @@ public function getManipulationUnits(string $shipper): array
* @param string $shipper
* @param string $service
* @param bool $fullData
* @param string $country
*
* @return array[]
*
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
*/
public function getBranches(string $shipper, string $service = null, bool $fullData = false): array
public function getBranches(string $shipper, string $service = null, bool $fullData = false, string $country = null): array
{
$request = $fullData ? Request::FULL_BRANCHES : Request::BRANCHES;

$response = $this->requester->call(API::V1, $shipper, $request . '/' . $service);
$response = $this->requester->call(API::V1, $shipper, $request . '/' . $service . '/' . $country);

if ($response['branches'] === null) {
return [];
Expand Down
1 change: 1 addition & 0 deletions src/Services/Requester.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function call(
): array {
// resolve url
$path = trim($shipper . '/' . $request, '/');
$path = str_replace('//', '/', $path);
$host = $this->resolveHostName($version);

// call API server and get response
Expand Down
Loading

0 comments on commit cb17aca

Please sign in to comment.