diff --git a/src/Carrier.php b/src/Carrier.php index 29b2479..3631791 100644 --- a/src/Carrier.php +++ b/src/Carrier.php @@ -1,49 +1,74 @@ - + * @copyright 2016 Meanbee Limited (http://www.meanbee.com) + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library + */ +namespace Meanbee\Royalmail; + +/** + * Class Carrier + * Provides methods to get rates from the csv files, interacts with the data class + * to return arrays of methods. + * + * @category Meanbee + * @package Meanbee\Royalmail + * @author Meanbee Limited + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library + */ class Carrier implements CarrierInterface { /** * CSV file location for CountryCodes + * * @var string */ - protected $_csvCountryCode; + protected $csvCountryCodeDef; /** * CSV file location for zone to methods * * @var string */ - protected $_csvZoneToDeliveryMethod; + protected $csvZoneToDeliveryMethodDef; /** * CSV file location for method meta info * * @var string */ - protected $_csvDeliveryMethodMeta; + protected $csvDeliveryMethodMetaDef; /** * CSV file location for method to price * * @var string */ - protected $_csvDeliveryToPrice; + protected $csvDeliveryToPriceDef; /** * CSV file location for method codes to user-friendly label. * * @var string */ - protected $_csvCleanNameToMethod; + protected $csvCleanNameToMethodDef; /** * CSV file location for mapping of method to method group * * @var string */ - protected $_csvCleanNameMethodGroup; - + protected $csvCleanNameMethodGroupDef; /** * Data resource class @@ -55,39 +80,61 @@ class Carrier implements CarrierInterface /** * Carrier constructor. * - * @param string|null $csvCountryCode - * @param string|null $csvZoneToDeliveryMethod - * @param string|null $csvDeliveryMethodMeta - * @param string|null $csvDeliveryToPrice - * @param string|null $csvCleanNameToMethod - * @param string|null $csvCleanNameMethodGroup + * @param string|null $csvCountryCode - csv for Country Code + * @param string|null $csvZoneToDeliveryMethod - csv for ZoneToDeliveyMethod + * @param string|null $csvDeliveryMethodMeta - csv for DeliveryMethodMeta + * @param string|null $csvDeliveryToPrice - csv for DeliveryToPrice + * @param string|null $csvCleanNameToMethod - csv for CleanNameToMethod + * @param string|null $csvCleanNameMethodGroup - csv for $csvCleanNameMethodGroup */ - public function __construct($csvCountryCode = null, $csvZoneToDeliveryMethod = null, $csvDeliveryMethodMeta = null, - $csvDeliveryToPrice = null, $csvCleanNameToMethod = null, - $csvCleanNameMethodGroup = null) - { + public function __construct( + $csvCountryCode = null, + $csvZoneToDeliveryMethod = null, + $csvDeliveryMethodMeta = null, + $csvDeliveryToPrice = null, + $csvCleanNameToMethod = null, + $csvCleanNameMethodGroup = null + ) { $dir = dirname(realpath(__FILE__)) . '/'; // Set the default csv values - $this->_csvCountryCode = isset($csvCountryCode) ? $csvCountryCode : $dir . '../data/1_countryToZone.csv'; - $this->_csvZoneToDeliveryMethod = isset($csvZoneToDeliveryMethod) ? $csvZoneToDeliveryMethod : - $dir . '../data/2_zoneToDeliveryMethod.csv'; - $this->_csvDeliveryMethodMeta = isset($csvDeliveryMethodMeta) ? $csvDeliveryMethodMeta : - $dir . '../data/3_deliveryMethodMeta.csv'; - $this->_csvDeliveryToPrice = isset($csvDeliveryToPrice) ? $csvDeliveryToPrice : - $dir . '../data/4_deliveryToPrice.csv'; - $this->_csvCleanNameToMethod = isset($csvCleanNameToMethod) ? $csvCleanNameToMethod : - $dir . '../data/5_cleanNameToMethod.csv'; - $this->_csvCleanNameMethodGroup = isset($csvCleanNameMethodGroup) ? $csvCleanNameMethodGroup : - $dir . '../data/6_cleanNameMethodGroup.csv'; + $this->csvCountryCodeDef = "$dir../data/1_countryToZone.csv"; + if ($csvCountryCode) { + $this->csvCountryCodeDef = $csvCountryCode; + } + + $this->csvZoneToDeliveryMethodDef = "$dir../data/2_zoneToDeliveryMethod.csv"; + if ($csvZoneToDeliveryMethod) { + $this->csvZoneToDeliveryMethodDef = $csvZoneToDeliveryMethod; + } + + $this->csvDeliveryMethodMetaDef = "$dir../data/3_deliveryMethodMeta.csv"; + if ($csvDeliveryMethodMeta) { + $this->csvDeliveryMethodMetaDef = $csvDeliveryMethodMeta; + } + + $this->csvDeliveryToPriceDef = "$dir../data/4_deliveryToPrice.csv"; + if ($csvDeliveryToPrice) { + $this->csvDeliveryToPriceDef = $csvDeliveryToPrice; + } + + $this->csvCleanNameToMethodDef = "$dir../data/5_cleanNameToMethod.csv"; + if ($csvCleanNameToMethod) { + $this->csvCleanNameToMethodDef = $csvCleanNameToMethod; + } + + $this->csvCleanNameMethodGroupDef = "$dir../data/6_cleanNameMethodGroup.csv"; + if ($csvCleanNameMethodGroup) { + $this->csvCleanNameMethodGroupDef = $csvCleanNameMethodGroup; + } $this->data = isset($data) ? $data : new Data( - $this->_csvCountryCode, - $this->_csvZoneToDeliveryMethod, - $this->_csvDeliveryMethodMeta, - $this->_csvDeliveryToPrice, - $this->_csvCleanNameToMethod, - $this->_csvCleanNameMethodGroup + $this->csvCountryCodeDef, + $this->csvZoneToDeliveryMethodDef, + $this->csvDeliveryMethodMetaDef, + $this->csvDeliveryToPriceDef, + $this->csvCleanNameToMethodDef, + $this->csvCleanNameMethodGroupDef ); } @@ -104,17 +151,28 @@ public function __construct($csvCountryCode = null, $csvZoneToDeliveryMethod = n * wants to get all available methods for the country code * and weight, ignoring the price of the package. * - * @param $country_code - * @param $package_value - * @param $package_weight - * @param $ignore_package_value + * @param string $country_code - The country code being shipped to + * @param int $package_value - The total package value + * @param int $package_weight - The total package weight + * @param bool $ignore_package_value - Flag to allow ignoring the + * package weight * - * @return array + * @return array - Array of all methods returned */ - public function getRates($country_code, $package_value, $package_weight, $ignore_package_value = false) - { - - $sortedDeliveryMethods = [$this->data->calculateMethods($country_code, $package_value, $package_weight, $ignore_package_value)]; + public function getRates( + $country_code, + $package_value, + $package_weight, + $ignore_package_value = false + ) { + $sortedDeliveryMethods = [ + $this->data->calculateMethods( + $country_code, + $package_value, + $package_weight, + $ignore_package_value + ) + ]; $results = []; @@ -155,60 +213,60 @@ public function getAllMethods() /** * CSV file location for CountryCodes * - * @return string + * @return string - default csv */ public function getCsvCountryCode() { - return $this->_csvCountryCode; + return $this->csvCountryCodeDef; } /** * CSV file location for zone to methods * - * @return string + * @return string - default csv */ public function getCsvZoneToDeliveryMethod() { - return $this->_csvZoneToDeliveryMethod; + return $this->csvZoneToDeliveryMethodDef; } /** * CSV file location for method meta info * - * @return string + * @return string - default csv */ public function getCsvDeliveryMethodMeta() { - return $this->_csvDeliveryMethodMeta; + return $this->csvDeliveryMethodMetaDef; } /** * CSV file location for method to price * - * @return string + * @return string - default csv */ public function getCsvDeliveryToPrice() { - return $this->_csvDeliveryToPrice; + return $this->csvDeliveryToPriceDef; } /** * CSV file location for method codes to user-friendly label. * - * @return string + * @return string - default csv */ public function getCsvCleanNameToMethod() { - return $this->_csvCleanNameToMethod; + return $this->csvCleanNameToMethodDef; } /** * CSV file location for mapping of method to method group * - * @return string + * @return string - default csv */ public function getCsvCleanNameMethodGroup() { - return $this->_csvCleanNameMethodGroup; + return $this->csvCleanNameMethodGroupDef; } } diff --git a/src/CarrierInterface.php b/src/CarrierInterface.php index 2059e1f..4f2e838 100644 --- a/src/CarrierInterface.php +++ b/src/CarrierInterface.php @@ -1,19 +1,47 @@ + * @copyright 2016 Meanbee Limited (http://www.meanbee.com) + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library + */ namespace Meanbee\Royalmail; +/** + * Interface CarrierInterface + * Allows for a generic interface of carrier to account for any future carriers + * to be added. + * + * @category Meanbee + * @package Meanbee\Royalmail + * @author Meanbee Limited + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library + */ interface CarrierInterface { /** * Get methods and rates based on package conditions * - * @param $country_code - * @param $package_value - * @param $package_weight - * @param $ignore_package_value + * @param string $country_code - Country code package is going to + * @param int $package_value - Package value + * @param int $package_weight - Package weight + * @param bool $ignore_package_value - Whether to ignore package value or not * * @return array */ - public function getRates($country_code, $package_value, $package_weight, $ignore_package_value = false); + public function getRates( + $country_code, + $package_value, + $package_weight, + $ignore_package_value = false + ); } diff --git a/src/Data.php b/src/Data.php index 26be146..348d03f 100644 --- a/src/Data.php +++ b/src/Data.php @@ -1,5 +1,31 @@ - + * @copyright 2016 Meanbee Limited (http://www.meanbee.com) + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library + */ + +namespace Meanbee\Royalmail; + +/** + * Class Data + * Data layer class. Interacts with the csv files and creates sorted mapped arrays + * out of them. Provides methods to interact with the csv files and return sorted + * method rates of several formats. + * + * @category Meanbee + * @package Meanbee\Royalmail + * @author Meanbee Limited + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library + */ class Data { @@ -24,7 +50,8 @@ class Data protected $mappingCleanNameToMethod = []; /** - * Maps the method group name to the clean name, to allow for printing just the clean names to the user + * Maps the method group name to the clean name, to allow for printing + * just the clean names to the user * * @var array */ @@ -45,7 +72,8 @@ class Data protected $mappingZoneToMethod = []; /** - * Map methods to meta information. This includes the insurance amount, and the corresponding price levels + * Map methods to meta information. This includes the insurance + * amount, and the corresponding price levels * * @var array */ @@ -58,6 +86,16 @@ class Data */ protected $mappingDeliveryToPrice = []; + /** + * Data constructor. + * + * @param string $_csvCountryCode - country code csv path + * @param string $_csvZoneToDeliveryMethod - zone to method csv path + * @param string $_csvDeliveryMethodMeta - delivery method meta csv path + * @param string $_csvDeliveryToPrice - delivery to price csv path + * @param string $_csvCleanNameToMethod - clean name to method csv path + * @param string $_csvCleanNameMethodGroup - clean name method group csv path + */ public function __construct( $_csvCountryCode, $_csvZoneToDeliveryMethod, @@ -66,12 +104,14 @@ public function __construct( $_csvCleanNameToMethod, $_csvCleanNameMethodGroup ) { - $this->mappingCountryToZone = $this->csvToArray($_csvCountryCode); - $this->mappingZoneToMethod = $this->csvToArray($_csvZoneToDeliveryMethod); - $this->mappingMethodToMeta = $this->csvToArray($_csvDeliveryMethodMeta); - $this->mappingDeliveryToPrice = $this->csvToArray($_csvDeliveryToPrice); - $this->mappingCleanNameToMethod = $this->csvToArray($_csvCleanNameToMethod); - $this->mappingCleanNameMethodGroup = $this->csvToArray($_csvCleanNameMethodGroup); + $this->mappingCountryToZone = $this->_csvToArray($_csvCountryCode); + $this->mappingZoneToMethod = $this->_csvToArray($_csvZoneToDeliveryMethod); + $this->mappingMethodToMeta = $this->_csvToArray($_csvDeliveryMethodMeta); + $this->mappingDeliveryToPrice = $this->_csvToArray($_csvDeliveryToPrice); + $this->mappingCleanNameToMethod = $this->_csvToArray($_csvCleanNameToMethod); + $this->mappingCleanNameMethodGroup = $this->_csvToArray( + $_csvCleanNameMethodGroup + ); } /** @@ -85,70 +125,70 @@ public function __construct( * value of the packages to be ignored in the calculation * at the users discretion. * - * @param $country_code - * @param $package_value - * @param $package_weight - * @param $ignore_package_value + * @param string $country_code - Country code being shipped to + * @param int $package_value - Package value + * @param int $package_weight - Package weight + * @param bool $ignore_package_value - ignore weight bool * * @return array */ - public function calculateMethods($country_code, $package_value, $package_weight, $ignore_package_value = false) - { + public function calculateMethods( + $country_code, + $package_value, + $package_weight, + $ignore_package_value = false + ) { $sortedCountryCodeMethods = [ - $this->getCountryCodeData( + $this->_getCountryCodeData( $country_code, $this->mappingCountryToZone ) ]; $sortedZoneToMethods = [ - $this->getZoneToMethod( + $this->_getZoneToMethod( $sortedCountryCodeMethods, $this->mappingZoneToMethod ) ]; if ($ignore_package_value) { - $sortedMethodToMeta = [ - $this->getMethodToMetaAll( - $sortedZoneToMethods, - $this->mappingMethodToMeta - ) - ]; + $sortedMethodToMeta = $this->_getMethodToMetaAll( + $sortedZoneToMethods, + $this->mappingMethodToMeta + ); } else { - $sortedMethodToMeta = [ - $this->getMethodToMeta( - $package_value, - $sortedZoneToMethods, - $this->mappingMethodToMeta - ) - ]; + $sortedMethodToMeta = $this->_getMethodToMeta( + $package_value, + $sortedZoneToMethods, + $this->mappingMethodToMeta + ); } - return $this->getMethodToPrice( + return $this->_getMethodToPrice( $package_weight, $sortedMethodToMeta, $this->mappingDeliveryToPrice - ); } /** - * * Method to return a 2d array of world zones a country * (by its country code) is located in. * - * @param $country_code - * @param $mappingCountryToZone + * @param string $country_code - Country code to filter on + * @param array $mappingCountryToZone - Array for mapped countries to zones * * @return array */ - private function getCountryCodeData($country_code, $mappingCountryToZone) + private function _getCountryCodeData($country_code, $mappingCountryToZone) { // Get All array items that match the country code $countryCodeData = []; foreach ($mappingCountryToZone as $item) { - if (isset($item[self::COUNTRY_CODE]) && $item[self::COUNTRY_CODE] == $country_code) { + if (isset($item[self::COUNTRY_CODE]) + && $item[self::COUNTRY_CODE] == $country_code + ) { foreach ($item as $keys) { $countryCodeData[] = $keys; } @@ -161,7 +201,6 @@ private function getCountryCodeData($country_code, $mappingCountryToZone) unset($countryCodeData[$key]); } } - $countryCodeData = array_values($countryCodeData); return $countryCodeData; @@ -171,22 +210,25 @@ private function getCountryCodeData($country_code, $mappingCountryToZone) * Method to return a 2d array of possible delivery methods based * on the given world zones a country is in. * - * @param $sortedCountryCodeMethods - * @param $mappingZoneToMethod + * @param array $sortedCountryCodeMethods - Methods to filter on + * @param array $mappingZoneToMethod - ZonesToMethods to filter on * * @return array */ - private function getZoneToMethod($sortedCountryCodeMethods, $mappingZoneToMethod) - { + private function _getZoneToMethod( + $sortedCountryCodeMethods, + $mappingZoneToMethod + ) { $mappingZoneData = []; foreach ($sortedCountryCodeMethods as $key => $value) { foreach ($value as $zone) { foreach ($mappingZoneToMethod as $item) { - if (isset($item[self::WORLD_ZONE]) && $item[self::WORLD_ZONE] == $zone) { + if (isset($item[self::WORLD_ZONE]) + && $item[self::WORLD_ZONE] == $zone + ) { foreach ($item as $keys) { $mappingZoneData[] = $keys; } - } } } @@ -201,7 +243,6 @@ private function getZoneToMethod($sortedCountryCodeMethods, $mappingZoneToMethod } } } - } $mappingZoneData = array_values($mappingZoneData); @@ -210,87 +251,88 @@ private function getZoneToMethod($sortedCountryCodeMethods, $mappingZoneToMethod } /** - * Method to return a 2d array of the meta data for each - * given allowed shipping method and the given package - * value. + * Method to return a 2d array of sorted shipping methods based on + * the weight of the item and the allowed shipping methods. Returns + * a 2d array to be converting into objects by the RoyalMailMethod + * class. Also adds the pretty text from the meta table to the + * correct shipping method, to allow for less text in the delivery + * to price csv. * - * @param $packageValue - * @param $sortedZoneToMethods - * @param $mappingMethodToMeta + * @param int $package_weight - The weight of the package + * @param array $sortedMethodToMeta - Sorted methods to meta + * @param array $mappingDeliveryToPrice - Sorted delivery to price * * @return array */ - private function getMethodToMeta($packageValue, $sortedZoneToMethods, $mappingMethodToMeta) - { - $mappingZoneMethodData = []; - foreach ($sortedZoneToMethods as $key => $value) { - foreach ($value as $method) { - foreach ($mappingMethodToMeta as $item) { - if (isset($item[self::SHIPPING_METHOD]) && $item[self::SHIPPING_METHOD] == $method) { - if ($packageValue >= $item[self::METHOD_MIN_VALUE] && $packageValue <= $item[self::METHOD_MAX_VALUE]) { - $mappingZoneMethodData[] = [$item]; - } - - } + private function _getMethodToPrice( + $package_weight, + $sortedMethodToMeta, + $mappingDeliveryToPrice + ) { + $mappingDeliveryToPriceData = []; + foreach ($mappingDeliveryToPrice as $item) { + if (isset($item[self::SHIPPING_METHOD]) + && isset($sortedMethodToMeta[$item[self::SHIPPING_METHOD]]) + && $package_weight >= $item[self::METHOD_MIN_WEIGHT] + && $package_weight <= $item[self::METHOD_MAX_WEIGHT] + ) { + $data = $sortedMethodToMeta[$item[self::SHIPPING_METHOD]]; + $resultArray = [ + 'shippingMethodName' => $item[self::SHIPPING_METHOD], + 'minimumWeight' => (double) $item[self::METHOD_MIN_WEIGHT], + 'maximumWeight' => (double) $item[self::METHOD_MAX_WEIGHT], + 'methodPrice' => (double) $item[self::METHOD_PRICE], + 'insuranceValue' => (int) $item[self::METHOD_INSURANCE_VALUE], + 'shippingMethodNameClean' => $data[self::METHOD_NAME_CLEAN] + ]; + + if (isset($item[self::METHOD_SIZE])) { + $resultArray['size'] = $item[self::METHOD_SIZE]; } + + $mappingDeliveryToPriceData[] = $resultArray; } } - $mappingZoneMethodData = array_values($mappingZoneMethodData); + $mappingDeliveryToPriceData = array_values($mappingDeliveryToPriceData); - return $mappingZoneMethodData; + return $mappingDeliveryToPriceData; } /** - * Method to return a 2d array of sorted shipping methods based on - * the weight of the item and the allowed shipping methods. Returns - * a 2d array to be converting into objects by the RoyalMailMethod - * class. Also adds the pretty text from the meta table to the - * correct shipping method, to allow for less text in the delivery - * to price csv. + * Method to return a 2d array of the meta data for each + * given allowed shipping method and the given package + * value. * - * @param $package_weight - * @param $sortedMethodToMeta - * @param $mappingDeliveryToPrice + * @param int $packageValue - Package value to filter methods on + * @param array $sortedZoneToMethods - SortedZoneToMethods to filter with + * @param array $mappingMethodToMeta - MethodToMeta to filter on * * @return array */ - private function getMethodToPrice($package_weight, $sortedMethodToMeta, $mappingDeliveryToPrice) - { - $mappingDeliveryToPriceData = []; - foreach ($sortedMethodToMeta as $method) { - foreach ($method as $meta) { - foreach ($meta as $key => $value) { - foreach ($value as $methodData) { - foreach ($mappingDeliveryToPrice as $item) { - if (isset($item[self::SHIPPING_METHOD]) && $item[self::SHIPPING_METHOD] == $methodData) { - if ($package_weight >= $item[self::METHOD_MIN_WEIGHT] && $package_weight <= $item[self::METHOD_MAX_WEIGHT]) { - $resultArray = [ - 'shippingMethodName' => $item[self::SHIPPING_METHOD], - 'minimumWeight' => (double)$item[self::METHOD_MIN_WEIGHT], - 'maximumWeight' => (double)$item[self::METHOD_MAX_WEIGHT], - 'methodPrice' => (double)$item[self::METHOD_PRICE], - 'insuranceValue' => (int)$item[self::METHOD_INSURANCE_VALUE], - 'shippingMethodNameClean' => $value[self::METHOD_NAME_CLEAN] - ]; - - if (isset($item[self::METHOD_SIZE])) { - $resultArray['size'] = $item[self::METHOD_SIZE]; - } - - $mappingDeliveryToPriceData[] = $resultArray; - - } - } + private function _getMethodToMeta( + $packageValue, + $sortedZoneToMethods, + $mappingMethodToMeta + ) { + $mappingZoneMethodData = []; + foreach ($sortedZoneToMethods as $key => $value) { + foreach ($value as $method) { + foreach ($mappingMethodToMeta as $item) { + if (isset($item[self::SHIPPING_METHOD]) + && $item[self::SHIPPING_METHOD] == $method + ) { + if ($packageValue >= $item[self::METHOD_MIN_VALUE] + && $packageValue <= $item[self::METHOD_MAX_VALUE] + ) { + $mappingZoneMethodData[$item[0]] = $item; } } } } } - $mappingDeliveryToPriceData = array_values($mappingDeliveryToPriceData); - - return $mappingDeliveryToPriceData; + return $mappingZoneMethodData; } /** @@ -299,43 +341,46 @@ private function getMethodToPrice($package_weight, $sortedMethodToMeta, $mapping * of the item. Returns all possible available methods * that are available. * - * @param $sortedZoneToMethods - * @param $mappingMethodToMeta + * @param array $sortedZoneToMethods - Sorted array of zone to methods + * @param array $mappingMethodToMeta - Sorted array of methods to the meta * * @return array */ - private function getMethodToMetaAll($sortedZoneToMethods, $mappingMethodToMeta) + private function _getMethodToMetaAll($sortedZoneToMethods, $mappingMethodToMeta) { $mappingZoneMethodData = []; foreach ($sortedZoneToMethods as $key => $value) { foreach ($value as $method) { foreach ($mappingMethodToMeta as $item) { - if (isset($item[self::SHIPPING_METHOD]) && $item[self::SHIPPING_METHOD] == $method) { - $mappingZoneMethodData[] = [$item]; + if (isset($item[self::SHIPPING_METHOD]) + && $item[self::SHIPPING_METHOD] == $method + ) { + $mappingZoneMethodData[$item[0]] = $item; } } } } - $mappingZoneMethodData = array_values($mappingZoneMethodData); - return $mappingZoneMethodData; } /** * Reads the given csv in to a 2d array * - * @param string $filename - * @param string $delimiter + * @param string $filename - The filename of the csv + * @param string $delimiter - The delimiter * * @return array * @throws \Exception */ - private function csvToArray($filename = '', $delimiter = ',') + private function _csvToArray($filename = '', $delimiter = ',') { if (!file_exists($filename) || !is_readable($filename)) { - throw new \Exception("Unable to load the Royal Mail price data csv for '$filename'. - Ensure that the data folder contains all the necessary csvs."); + throw new \Exception( + "Unable to load the Royal Mail price data csv for + '$filename'. Ensure that the data folder contains all the necessary + csvs." + ); } $header = null; @@ -360,7 +405,8 @@ public function getMappingCleanNameToMethod() } /** - * Maps the method group name to the clean name, to allow for printing just the clean names to the user + * Maps the method group name to the clean name, to allow + * for printing just the clean names to the user * * @return array */ @@ -390,7 +436,8 @@ public function getMappingZoneToMethod() } /** - * Map methods to meta information. This includes the insurance amount, and the corresponding price levels + * Map methods to meta information. This includes the insurance + * amount, and the corresponding price levels * * @return array */ @@ -408,4 +455,4 @@ public function getMappingDeliveryToPrice() { return $this->mappingDeliveryToPrice; } -} \ No newline at end of file +} diff --git a/src/Method.php b/src/Method.php index c954b34..c56fda4 100644 --- a/src/Method.php +++ b/src/Method.php @@ -1,5 +1,30 @@ - + * @copyright 2016 Meanbee Limited (http://www.meanbee.com) + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library + */ + +namespace Meanbee\Royalmail; + +/** + * Class Method + * Object class to represent the method object and allow for + * creation of method objects by the other classes. + * + * @category Meanbee + * @package Meanbee\Royalmail + * @author Meanbee Limited + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library + */ class Method { /** @@ -58,9 +83,28 @@ class Method */ protected $size; - public function __construct($code, $name, $countryCode, $price, - $insuranceValue, $minimumWeight, $maximumWeight, $size = null) - { + /** + * Method constructor. + * + * @param string $code - Country code of method + * @param string $name - Clean shipping code of method + * @param string $countryCode - Country code of method + * @param string $price - Price of method + * @param string $insuranceValue - Insurance value of method + * @param string $minimumWeight - Minimum weight the method can have + * @param string $maximumWeight - Maximum weight the method can have + * @param null $size - Parcel size, only applies to sm and md parcels + */ + public function __construct( + $code, + $name, + $countryCode, + $price, + $insuranceValue, + $minimumWeight, + $maximumWeight, + $size = null + ) { $this->code = $code; $this->name = $name; $this->countryCode = $countryCode; diff --git a/tests/CarrierTest.php b/tests/CarrierTest.php index 962afa0..edfd037 100644 --- a/tests/CarrierTest.php +++ b/tests/CarrierTest.php @@ -1,5 +1,29 @@ - + * @copyright 2016 Meanbee Limited (http://www.meanbee.com) + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library + */ +namespace Meanbee\Royalmail; + +/** + * Class CarrierTest + * Test class to run phpunit tests on the library. + * + * @category Meanbee + * @package Meanbee\Royalmail + * @author Meanbee Limited + * @license OSL v. 3.0 + * @link http://github.com/meanbee/royalmail-php-library + */ class CarrierTest extends \PHPUnit_Framework_TestCase { @@ -15,32 +39,47 @@ class CarrierTest extends \PHPUnit_Framework_TestCase const INSURANCE_ROW_PRICE_CSV = 4; const INSURANCE_ROW_CLEANNAME_CSV = 5; - /** @var Carrier $carrier */ - private $carrier; + /** + * Variable for carrier class + * + * @var Carrier $_carrier + */ + private $_carrier; - /** @var Data $dataClass */ - private $dataClass; - private $emptyArray; - private $testDataClassArray; + /** + * Variable for data class + * + * @var Data $_dataClass + */ + private $_dataClass; + + private $_emptyArray; + private $_testDataClassArray; /** * Setup the necessary classes and data + * + * @return null */ public function setUp() { - /** @var Carrier */ - $this->carrier = new Carrier(); - $this->dataClass = new Data( - $this->carrier->getCsvCountryCode(), - $this->carrier->getCsvZoneToDeliveryMethod(), - $this->carrier->getCsvDeliveryMethodMeta(), - $this->carrier->getCsvDeliveryToPrice(), - $this->carrier->getCsvCleanNameToMethod(), - $this->carrier->getCsvCleanNameMethodGroup() + /** + * Set the carrier class + * + * @var Carrier + */ + $this->_carrier = new Carrier(); + $this->_dataClass = new Data( + $this->_carrier->getCsvCountryCode(), + $this->_carrier->getCsvZoneToDeliveryMethod(), + $this->_carrier->getCsvDeliveryMethodMeta(), + $this->_carrier->getCsvDeliveryToPrice(), + $this->_carrier->getCsvCleanNameToMethod(), + $this->_carrier->getCsvCleanNameMethodGroup() ); - $this->emptyArray = []; - $this->testDataClassArray = array( + $this->_emptyArray = []; + $this->_testDataClassArray = array( 'shippingMethodName' => 'test', 'minimumWeight' => 1.00, 'maximumWeight' => 5.00, @@ -53,31 +92,37 @@ public function setUp() /** * Cleans up the used classes + * + * @return null */ public function tearDown() { - $this->carrier = null; - $this->dataClass = null; + $this->_carrier = null; + $this->_dataClass = null; } /** * Test to ensure that the calculate method class is returning + * + * @return null */ public function testRoyalmailClassRealValues() { - $this->assertNotEmpty($this->carrier->getRates('GB', 20, 0.050)); + $this->assertNotEmpty($this->_carrier->getRates('GB', 20, 0.050)); } /** * Test to ensure that the calculate method class is returning rates with * the ignore insurance flag set to true. 37 methods are expected to be * returned. + * + * @return null */ public function testRoyalmailClassRealValuesAll() { $this->assertCount( 37, - $this->carrier->getRates('GB', 20, 0.050, true), + $this->_carrier->getRates('GB', 20, 0.050, true), "Array size from getRates did not match on the expected size of 37 methods returned." ); @@ -87,182 +132,379 @@ public function testRoyalmailClassRealValuesAll() * Test to ensure that the calculate method class is returning rates with * the ignore insurance flag set to false. 30 methods are expected to be * returned. + * + * @return null */ public function testRoyalmailClassRealValuesAllFalse() { $this->assertCount( 30, - $this->carrier->getRates('GB', 20, 0.050, false), + $this->_carrier->getRates('GB', 20, 0.050, false), "Array size from getRates did not match on - the expected size of 37 methods returned."); + the expected size of 37 methods returned." + ); } /** * Test to compare the returned data from the Data class to expected values + * + * @return null */ public function testRoyalmailMethodRealValues() { - $calculatedMethods = $this->dataClass->calculateMethods('GB', 19.99, 0.050); + $calculatedMethods = $this->_dataClass->calculateMethods('GB', 19.99, 0.050); foreach ($calculatedMethods as $calculatedMethod => $arrayContents) { - $this->assertEquals(gettype($this->testDataClassArray['shippingMethodName']), - gettype($arrayContents['shippingMethodName']), "shippingMethodName array value not equal to correct type."); - $this->assertEquals(gettype($this->testDataClassArray['minimumWeight']), - gettype($arrayContents['minimumWeight']), "minimumWeight array value not equal to correct type."); - $this->assertEquals(gettype($this->testDataClassArray['maximumWeight']), - gettype($arrayContents['maximumWeight']), "maximumWeight array value not equal to correct type."); - $this->assertEquals(gettype($this->testDataClassArray['methodPrice']), - gettype($arrayContents['methodPrice']), "methodPrice array value not equal to correct type."); - $this->assertEquals(gettype($this->testDataClassArray['insuranceValue']), - gettype($arrayContents['insuranceValue']), "insuranceValue array value not equal to correct type."); - $this->assertEquals(gettype($this->testDataClassArray['shippingMethodNameClean']), - gettype($arrayContents['shippingMethodNameClean']), "shippingMethodNameClean array value not equal to correct type."); - $this->assertEquals(gettype($this->testDataClassArray['size']), gettype($arrayContents['size']), - "size array value not equal to correct type."); - + $this->assertEquals( + gettype( + $this->_testDataClassArray['shippingMethodName'] + ), + gettype($arrayContents['shippingMethodName']), + "shippingMethodName array value not equal to correct type." + ); + $this->assertEquals( + gettype( + $this->_testDataClassArray['minimumWeight'] + ), + gettype($arrayContents['minimumWeight']), + "minimumWeight array value not equal to correct type." + ); + $this->assertEquals( + gettype( + $this->_testDataClassArray['maximumWeight'] + ), + gettype($arrayContents['maximumWeight']), + "maximumWeight array value not equal to correct type." + ); + $this->assertEquals( + gettype( + $this->_testDataClassArray['methodPrice'] + ), + gettype($arrayContents['methodPrice']), + "methodPrice array value not equal to correct type." + ); + $this->assertEquals( + gettype( + $this->_testDataClassArray['insuranceValue'] + ), + gettype($arrayContents['insuranceValue']), + "insuranceValue array value not equal to correct type." + ); + $this->assertEquals( + gettype( + $this->_testDataClassArray['shippingMethodNameClean'] + ), + gettype($arrayContents['shippingMethodNameClean']), + "shippingMethodNameClean array value not equal to correct type." + ); + $this->assertEquals( + gettype( + $this->_testDataClassArray['size'] + ), + gettype($arrayContents['size']), + "size array value not equal to correct type." + ); } } /** - * Test to ensure the only the expected empty array is returned from incorrect data to the data class + * Test to ensure the only the expected empty array + * is returned from incorrect data to the data class + * + * @return null */ public function testRoyalmailMethodFake() { - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods('GASD', "aSDASD", "ASDASD")); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods(123123123, "asdasd", "asdadasd")); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods(123123, 123123, "ASDASD")); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods(123123123, 123123123, 123123123)); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods('GB', "aSD!!ASD", 0.100)); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods('GB', 123123123123, 0.100)); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + 'GASD', "aSDASD", "ASDASD" + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + 123123123, "asdasd", "asdadasd" + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + 123123, 123123, "ASDASD" + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + 123123123, 123123123, 123123123 + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + 'GB', "aSD!!ASD", 0.100 + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + 'GB', 123123123123, 0.100 + ) + ); } /** - * Test to ensure that only the expected empty array is returned from null and incorrect data + * Test to ensure that only the expected empty array + * is returned from null and incorrect data * from the Data class + * + * @return null */ public function testRoyalmailMethodNull() { - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods(null, 123123123123, 0.100)); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods(null, null, 0.100)); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods('GB', null, 0.100)); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods('GB', null, null)); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods('GB', 123123123123, null)); - $this->assertEquals($this->emptyArray, $this->dataClass->calculateMethods(null, null, null)); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + null, 123123123123, 0.100 + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + null, null, 0.100 + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + 'GB', null, 0.100 + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + 'GB', null, null + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + 'GB', 123123123123, null + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_dataClass->calculateMethods( + null, null, null + ) + ); } /** * Test to ensure that only the expected empty array is returned from incorrect * data from the CalculateMethod class + * + * @return null */ public function testRoyalmailClassFake() { $this->assertEquals( - $this->emptyArray, - $this->carrier->getRates('GASD', "aSDASD", "ASDASD")); + $this->_emptyArray, + $this->_carrier->getRates( + 'GASD', "aSDASD", "ASDASD" + ) + ); $this->assertEquals( - $this->emptyArray, - $this->carrier->getRates(123123123, "asdasd", "asdadasd")); + $this->_emptyArray, + $this->_carrier->getRates( + 123123123, "asdasd", "asdadasd" + ) + ); $this->assertEquals( - $this->emptyArray, - $this->carrier->getRates(123123, 123123, "ASDASD")); + $this->_emptyArray, + $this->_carrier->getRates( + 123123, 123123, "ASDASD" + ) + ); $this->assertEquals( - $this->emptyArray, - $this->carrier->getRates(123123123, 123123123, 123123123)); + $this->_emptyArray, + $this->_carrier->getRates( + 123123123, 123123123, 123123123 + ) + ); $this->assertEquals( - $this->emptyArray, - $this->carrier->getRates('GB', "aSD!!ASD", 0.100)); + $this->_emptyArray, + $this->_carrier->getRates( + 'GB', "aSD!!ASD", 0.100 + ) + ); $this->assertEquals( - $this->emptyArray, - $this->carrier->getRates('GB', 123123123123, 0.100)); + $this->_emptyArray, + $this->_carrier->getRates( + 'GB', 123123123123, 0.100 + ) + ); } /** * Test to ensure that only the expected empty array is returned from null * and incorrect data from the CalculateMethod class + * + * @return null */ public function testRoyalmailClassNull() { - $this->assertEquals($this->emptyArray, $this->carrier->getRates(null, 123123123123, 0.100)); - $this->assertEquals($this->emptyArray, $this->carrier->getRates(null, null, 0.100)); - $this->assertEquals($this->emptyArray, $this->carrier->getRates('GB', null, 0.100)); - $this->assertEquals($this->emptyArray, $this->carrier->getRates('GB', null, null)); - $this->assertEquals($this->emptyArray, $this->carrier->getRates('GB', 123123123123, null)); - $this->assertEquals($this->emptyArray, $this->carrier->getRates(null, null, null)); + $this->assertEquals( + $this->_emptyArray, + $this->_carrier->getRates( + null, 123123123123, 0.100 + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_carrier->getRates( + null, null, 0.100 + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_carrier->getRates( + 'GB', null, 0.100 + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_carrier->getRates( + 'GB', null, null + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_carrier->getRates( + 'GB', 123123123123, null + ) + ); + $this->assertEquals( + $this->_emptyArray, + $this->_carrier->getRates( + null, null, null + ) + ); } /** - * Test co compare the meta names vs the clean name to check that the correct value - * exists and is being used. + * Test co compare the meta names vs the clean name to check that + * the correct value exists and is being used. + * + * @return null */ public function testMethodToMetaVsCleanName() { - foreach ($this->dataClass->getMappingMethodToMeta() as $array => $data) { + foreach ($this->_dataClass->getMappingMethodToMeta() + as $array => $data) { $methodNotExist = false; - foreach ($this->dataClass->getMappingCleanNameToMethod() as $method => $methodData) { + foreach ($this->_dataClass->getMappingCleanNameToMethod() + as $method => $methodData) { // Check the the names are equal - if ($data[self::METHOD_NAME_ROW_META_CSV] == $methodData[self::METHOD_NAME_ROW_CLEANNAME_CSV]) { + if ($data[self::METHOD_NAME_ROW_META_CSV] == $methodData[self::METHOD_NAME_ROW_CLEANNAME_CSV] + ) { $methodNotExist = true; - $this->assertEquals($data[self::METHOD_CLEAN_NAME_ROW_META_CSV], + $this->assertEquals( + $data[self::METHOD_CLEAN_NAME_ROW_META_CSV], $methodData[self::METHOD_CLEAN_NAME_ROW_CLEANNAME_CSV], - sprintf("Clean names %s and %s were not equal", $data[self::METHOD_CLEAN_NAME_ROW_META_CSV], - $methodData[self::METHOD_CLEAN_NAME_ROW_CLEANNAME_CSV])); + sprintf( + "Clean names %s and %s were not equal", + $data[self::METHOD_CLEAN_NAME_ROW_META_CSV], + $methodData[self::METHOD_CLEAN_NAME_ROW_CLEANNAME_CSV] + ) + ); } - } - $this->assertTrue($methodNotExist, - sprintf("%s was not found in CleanNameToMethod csv$.", $data[self::METHOD_NAME_ROW_META_CSV])); + + $this->assertTrue( + $methodNotExist, + sprintf( + "%s was not found in CleanNameToMethod csv$.", + $data[self::METHOD_NAME_ROW_META_CSV] + ) + ); } } /** * Test for insurance value checking that the correct insurance value is being * used in the CSV files + * + * @return null */ public function testInsuranceValue() { - foreach ($this->dataClass->getMappingMethodToMeta() as $array => $data) { - foreach ($this->dataClass->getMappingDeliveryToPrice() as $method => $methodData) { - if ($data[self::METHOD_NAME_ROW_META_CSV] == $methodData[self::METHOD_NAME_ROW_PRICE_CSV]) { + foreach ($this->_dataClass->getMappingMethodToMeta() as $array => $data) { + foreach ($this->_dataClass->getMappingDeliveryToPrice() + as $method => $methodData) { + if ($data[self::METHOD_NAME_ROW_META_CSV] == $methodData[self::METHOD_NAME_ROW_PRICE_CSV] + ) { if ($methodData[self::INSURANCE_ROW_PRICE_CSV] != "") { - $this->assertEquals($data[self::INSURANCE_ROW_META_CSV], + $this->assertEquals( + $data[self::INSURANCE_ROW_META_CSV], $methodData[self::INSURANCE_ROW_PRICE_CSV], - sprintf("Insurance values %s from mappingMethodToMeta and %s from mappingDeliveryToPrice were not equal.", + sprintf( + "Insurance values %s from mappingMethodToMeta and + %s from mappingDeliveryToPrice were not equal.", $data[self::INSURANCE_ROW_META_CSV], - $methodData[self::INSURANCE_ROW_PRICE_CSV])); + $methodData[self::INSURANCE_ROW_PRICE_CSV] + ) + ); } } } } - foreach ($this->dataClass->getMappingMethodToMeta() as $array => $data) { - foreach ($this->dataClass->getMappingCleanNameToMethod() as $method => $methodData) { - if ($data[self::METHOD_NAME_ROW_META_CSV] == $methodData[self::METHOD_NAME_ROW_CLEANNAME_CSV]) { + foreach ($this->_dataClass->getMappingMethodToMeta() as $array => $data) { + foreach ($this->_dataClass->getMappingCleanNameToMethod() + as $method => $methodData) { + if ($data[self::METHOD_NAME_ROW_META_CSV] == $methodData[self::METHOD_NAME_ROW_CLEANNAME_CSV] + ) { if ($methodData[self::INSURANCE_ROW_CLEANNAME_CSV] != "") { - $this->assertEquals($data[self::INSURANCE_ROW_META_CSV], + $this->assertEquals( + $data[self::INSURANCE_ROW_META_CSV], $methodData[self::INSURANCE_ROW_CLEANNAME_CSV], - sprintf("Insurance values %s from mappingMethodToMeta and %s from mappingCleanNameToMethod were not equal.", + sprintf( + "Insurance values %s from mappingMethodToMeta and + %s from mappingCleanNameToMethod were not equal.", $data[self::INSURANCE_ROW_META_CSV], - $methodData[self::INSURANCE_ROW_CLEANNAME_CSV])); + $methodData[self::INSURANCE_ROW_CLEANNAME_CSV] + ) + ); } } } - } } /** * Test to compare the method clean name vs the method group, ensuring that * the clean names are correct and exists. + * + * @return null */ public function testCleanNameVsMethodGroup() { - foreach ($this->dataClass->getMappingCleanNameToMethod() as $array => $data) { - foreach ($this->dataClass->getMappingCleanNameMethodGroup() as $method => $methodData) { - if ($data[self::METHOD_CLEAN_NAME_ROW_CLEANNAME_CSV] == $methodData[self::METHOD_CLEAN_NAME_ROW_CLEANNAMEGROUP_CSV]) { - $this->assertEquals($data[self::METHOD_CLEAN_NAME_GROUP_CLEANNAME_CSV], + foreach ($this->_dataClass->getMappingCleanNameToMethod() + as $array => $data) { + foreach ($this->_dataClass->getMappingCleanNameMethodGroup() + as $method => $methodData) { + if ($data[self::METHOD_CLEAN_NAME_ROW_CLEANNAME_CSV] == $methodData[self::METHOD_CLEAN_NAME_ROW_CLEANNAMEGROUP_CSV] + ) { + $this->assertEquals( + $data[self::METHOD_CLEAN_NAME_GROUP_CLEANNAME_CSV], $methodData[self::METHOD_CLEAN_NAME_GROUP_CLEANNAMEGROUP_CSV], - sprintf("Clean names %s from mappingCleanNameToMethod and %s from mappingCleanNameToMethodGroup were not equal.", + sprintf( + "Clean names %s from mappingCleanNameToMethod and + %s from mappingCleanNameToMethodGroup were not equal.", $data[self::METHOD_CLEAN_NAME_GROUP_CLEANNAME_CSV], - $methodData[self::METHOD_CLEAN_NAME_GROUP_CLEANNAMEGROUP_CSV])); + $methodData[self::METHOD_CLEAN_NAME_GROUP_CLEANNAMEGROUP_CSV] + ) + ); } } } @@ -270,11 +512,17 @@ public function testCleanNameVsMethodGroup() /** * Test get a full list of royal mail methods + * + * @return null */ public function testGetAllMethods() { - /** @var array $methods */ - $methods = $this->carrier->getAllMethods(); + /** + * All methods from the carrier class + * + * @var array $methods + */ + $methods = $this->_carrier->getAllMethods(); $this->assertInternalType('array', $methods); $this->assertTrue(count($methods) > 0);