Skip to content

Commit

Permalink
Chec 3259 shipping zone api (#1) (#6)
Browse files Browse the repository at this point in the history
* Create the Shipping Zone Api including service and models for shipping zone, province and country

* Convert countries and provinces to collections not arrays

* Add id property to ShippingZone
Add getter methods to Country model
Put id as the first property in the models
  • Loading branch information
sandersjessica authored and rickywiens committed Aug 24, 2017
1 parent 5fa531d commit bdeda51
Show file tree
Hide file tree
Showing 4 changed files with 375 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/Models/Country.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace BoldApps\ShopifyToolkit\Models;

use BoldApps\ShopifyToolkit\Contracts\Serializeable;

class Country implements Serializeable
{
/**
* @var int
*/
protected $id;

/**
* @var string
*/
protected $code;

/**
* @var string
*/
protected $name;

/**
* @var array
*/
protected $provinces;

/**
* @var float
*/
protected $tax;


/**
* @return int
*/
public function getId()
{
return $this->id;
}


/**
* @return string
*/
public function getCode()
{
return $this->code;
}


/**
* @return string
*/
public function getName()
{
return $this->name;
}


/**
* @return array
*/
public function getProvinces()
{
return $this->provinces;
}


/**
* @return float
*/
public function getTax()
{
return $this->tax;
}

}
134 changes: 134 additions & 0 deletions src/Models/Province.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

namespace BoldApps\ShopifyToolkit\Models;

use BoldApps\ShopifyToolkit\Contracts\Serializeable;

class Province implements Serializeable
{
/**
* @var int
*/
protected $id;

/**
* @var string
*/
protected $code;

/**
* @var int
*/
protected $countryId;

/**
* @var string
*/
protected $name;

/**
* @var int
*/
protected $shippingZoneId;

/**
* @var float
*/
protected $tax;

/**
* @var string
*/
protected $taxName;

/**
* @var string
*/
protected $taxType;

/**
* @var float
*/
protected $taxPercentage;

/**
* @return string
*/
public function getCode()
{
return $this->code;
}


/**
* @return int
*/
public function getCountryId()
{
return $this->countryId;
}


/**
* @return int
*/
public function getId()
{
return $this->id;
}


/**
* @return string
*/
public function getName()
{
return $this->name;
}


/**
* @return int
*/
public function getShippingZoneId()
{
return $this->shippingZoneId;
}


/**
* @return float
*/
public function getTax()
{
return $this->tax;
}


/**
* @return string
*/
public function getTaxName()
{
return $this->taxName;
}


/**
* @return string
*/
public function getTaxType()
{
return $this->taxType;
}


/**
* @return float
*/
public function getTaxPercentage()
{
return $this->taxPercentage;
}

}
90 changes: 90 additions & 0 deletions src/Models/ShippingZone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace BoldApps\ShopifyToolkit\Models;

use BoldApps\ShopifyToolkit\Contracts\Serializeable;

class ShippingZone implements Serializeable
{

/**
* @var int
*/
protected $id;

/**
* @var string
*/
protected $name;

/**
* @var array
*/
protected $countries;

/**
* @var array
*/
protected $carrierShippingRateProviders;

/**
* @var array
*/
protected $priceBasedShippingRates;

/**
* @var array
*/
protected $weightBasedShippingRates;

/**
* @return int
*/
public function getId()
{
return $this->id;
}

/**
* @return string
*/
public function getName()
{
return $this->name;
}


/**
* @return array
*/
public function getCountries()
{
return $this->countries;
}

/**
* @return array
*/
public function getCarrierShippingRateProviders()
{
return $this->carrierShippingRateProviders;
}


/**
* @return array
*/
public function getPriceBasedShippingRates()
{
return $this->priceBasedShippingRates;
}


/**
* @return array
*/
public function getWeightBasedShippingRates()
{
return $this->weightBasedShippingRates;
}
}
72 changes: 72 additions & 0 deletions src/Services/ShippingZone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace BoldApps\ShopifyToolkit\Services;

use BoldApps\ShopifyToolkit\Models\Country;
use BoldApps\ShopifyToolkit\Models\Province;
use BoldApps\ShopifyToolkit\Models\ShippingZone as ShippingZoneModel;

class ShippingZone extends Base
{

/**
* @param int $page
* @param int $limit
* @param array $filter
*
* @return Collection
*/
public function getAll($page = 1, $limit = 50, $filter = [ ])
{
$raw = $this->client->get('admin/shipping_zones.json', array_merge([
'page' => $page,
'limit' => $limit
], $filter));

$shippingZones = array_map(function ($zone) {
$zone['countries'] = $this->unserializeCountries($zone['countries']);

return $this->unserializeModel($zone, ShippingZoneModel::class);
}, $raw['shipping_zones']);

return collect($shippingZones);
}


/**
* @param $data
*
* @return Collection
*/
public function unserializeCountries($data)
{
if (null === $data) {
return;
}
$countries = array_map(function ($country) {
$country['provinces'] = $this->unserializeProvinces($country['provinces']);

return $this->unserializeModel($country, Country::class);
}, $data);

return collect($countries);
}


/**
* @param $data
*
* @return Collection
*/
public function unserializeProvinces($data)
{
if (null === $data) {
return;
}
$provinces = array_map(function ($province) {
return $this->unserializeModel($province, Province::class);
}, $data);

return collect($provinces);
}
}

0 comments on commit bdeda51

Please sign in to comment.