first commit

This commit is contained in:
2026-03-05 13:07:40 +01:00
commit 64ba0721ee
25709 changed files with 4691006 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
{
"name": "wpdesk\/abstract-shipping",
"description": "Abstract Shipping",
"license": "MIT",
"keywords": [
"wordpress",
"woocommerce",
"shipping"
],
"homepage": "https:\/\/gitlab.com\/wpdesk\/abstract-shipping",
"authors": [
{
"name": "grola",
"email": "grola@wpdesk.net"
}
],
"require": {
"php": ">=5.6",
"ext-json": "*",
"psr\/log": "^1.1"
},
"require-dev": {
"phpunit\/phpunit": "<7",
"wp-coding-standards\/wpcs": "^0.14.1",
"squizlabs\/php_codesniffer": "^3.0.2",
"mockery\/mockery": "*",
"10up\/wp_mock": "*",
"phpcompatibility\/php-compatibility": "^9.1"
},
"autoload": {
"psr-4": {
"DPDVendor\\WPDesk\\AbstractShipping\\": "src\/AbstractShipping"
}
},
"autoload-dev": {
"classmap": [
"tests\/unit"
]
},
"extra": {
"text-domain": "abstract-shipping",
"translations-folder": "lang",
"po-files": {
"pl_PL": "pl_PL.po",
"en_AU": "en_AU.po",
"en_CA": "en_CA.po",
"en_GB": "en_GB.po",
"de_DE": "de_DE.po"
}
},
"scripts": {
"phpcs": "phpcs",
"phpunit-unit": "phpunit --configuration phpunit-unit.xml --coverage-text --colors=never",
"phpunit-unit-fast": "phpunit --configuration phpunit-unit.xml --no-coverage",
"phpunit-integration": "phpunit --configuration phpunit-integration.xml --coverage-text --colors=never",
"phpunit-integration-fast": "phpunit --configuration phpunit-integration.xml --no-coverage"
}
}

View File

@@ -0,0 +1,34 @@
<?php
/**
* Simple DTO: SingleCollectionPoint class.
*
* @package WPDesk\AbstractShipping\CollectionPoint
*/
namespace DPDVendor\WPDesk\AbstractShipping\CollectionPoints;
use DPDVendor\WPDesk\AbstractShipping\Shipment\Address;
/**
* Define Single Collection Point.
*/
final class CollectionPoint
{
/**
* Collection point ID.
*
* @var string
*/
public $collection_point_id;
/**
* Collection point name.
*
* @var string
*/
public $collection_point_name;
/**
* Address.
*
* @var Address
*/
public $collection_point_address;
}

View File

@@ -0,0 +1,46 @@
<?php
/**
* Capability: CollectionPointsProvider class
*
* @package WPDesk\AbstractShipping\CollectionPointCapability
*/
namespace DPDVendor\WPDesk\AbstractShipping\CollectionPointCapability;
use DPDVendor\WPDesk\AbstractShipping\CollectionPoints\CollectionPoint;
use DPDVendor\WPDesk\AbstractShipping\Exception\CollectionPointNotFoundException;
use DPDVendor\WPDesk\AbstractShipping\Shipment\Address;
/**
* Interface for classes that provides collections points.
*/
interface CollectionPointsProvider
{
/**
* Get nearest collection points to given address.
*
* @param Address $address .
*
* @return CollectionPoint[]
* @throws CollectionPointNotFoundException
*/
public function get_nearest_collection_points(\DPDVendor\WPDesk\AbstractShipping\Shipment\Address $address);
/**
* Get single nearest collection point to given address.
*
* @param Address $address .
*
* @return CollectionPoint
* @throws CollectionPointNotFoundException
*/
public function get_single_nearest_collection_point(\DPDVendor\WPDesk\AbstractShipping\Shipment\Address $address);
/**
* Get get collection point by given id.
*
* @param string $collection_point_id .
* @param string $country_code .
*
* @return CollectionPoint
* @throws CollectionPointNotFoundException .
*/
public function get_point_by_id($collection_point_id, $country_code);
}

View File

@@ -0,0 +1,12 @@
<?php
/**
* Exception thrown when connection checker fails.
*
* @package WPDesk\AbstractShipping\Exception
*/
namespace DPDVendor\WPDesk\AbstractShipping\Exception;
class ApiConnectionCheckerException extends \RuntimeException
{
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* Custom Exception for CollectionPointNotFoundException.
*
* @package WPDesk\AbstractShipping\Exception
*/
namespace DPDVendor\WPDesk\AbstractShipping\Exception;
/**
* Exception thrown when collection point not found.
*
* @package WPDesk\AbstractShipping\Exception
*/
class CollectionPointNotFoundException extends \RuntimeException
{
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* Custom Exception for InvalidSettingsException.
*
* @package WPDesk\AbstractShipping\Exception
*/
namespace DPDVendor\WPDesk\AbstractShipping\Exception;
/**
* Exception thrown by service in case the settings do not pass the validation.
*
* @package WPDesk\AbstractShipping\Exception
*/
class InvalidSettingsException extends \RuntimeException implements \DPDVendor\WPDesk\AbstractShipping\Exception\ShippingException
{
}

View File

@@ -0,0 +1,45 @@
<?php
/**
* Custom Exception for Rates.
*
* @package WPDesk\AbstractShipping\Exception
*/
namespace DPDVendor\WPDesk\AbstractShipping\Exception;
/**
* Exception thrown when rates is empty.
*
* @package WPDesk\AbstractShipping\Exception
*/
class RateException extends \RuntimeException implements \DPDVendor\WPDesk\AbstractShipping\Exception\ShippingException
{
/**
* Context.
*
* @var array
*/
private $context;
/**
* RateException constructor.
*
* @param string $message Message.
* @param array $context Context.
* @param int $code Code.
* @param null $previous Previous.
*/
public function __construct($message = '', $context = [], $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->context = $context;
}
/**
* Get context.
*
* @return array
*/
public function get_context()
{
return $this->context;
}
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* Custom Exception when settings field not exists.
*
* @package WPDesk\AbstractShipping\Exception
*/
namespace DPDVendor\WPDesk\AbstractShipping\Exception;
/**
* Exception thrown when settings field not exists.
*
* @package WPDesk\AbstractShipping\Exception
*/
class SettingsFieldNotExistsException extends \Exception
{
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* Shipping exception interface.
*
* @package WPDesk\AbstractShipping\Exception
*/
namespace DPDVendor\WPDesk\AbstractShipping\Exception;
/**
* Shipping exception interface.
*
* @package WPDesk\AbstractShipping\Exception
*/
interface ShippingException
{
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* Custom Exception for unit conversion.
*
* @package WPDesk\AbstractShipping\Exception
*/
namespace DPDVendor\WPDesk\AbstractShipping\Exception;
/**
* Exception thrown when can't convert to other unit.
*
* @package WPDesk\AbstractShipping\Exception
*/
class UnitConversionException extends \Exception
{
}

View File

@@ -0,0 +1,29 @@
<?php
/**
* Simple DTO: Money class.
*
* @package WPDesk\AbstractShipping
*/
namespace DPDVendor\WPDesk\AbstractShipping\Rate;
/**
* Class for prepare data returned by plugin.
*
* @package WPDesk\AbstractShipping
*/
final class Money
{
/**
* Value in string field but usually is INT
*
* @var string|float
*/
public $amount;
/**
* Currency.
*
* @var string In ISO 4217
*/
public $currency;
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* Simple DTO: ShipmentRating interface.
*
* @package WPDesk\AbstractShipping\Rate
*/
namespace DPDVendor\WPDesk\AbstractShipping\Rate;
/**
* Interface for classes that store shipment prices. It will be returned by CanRate::get_rates.
*
* @package WPDesk\AbstractShipping\Rate
*/
interface ShipmentRating
{
/**
* Get ratings.
*
* @return SingleRate[]
*/
public function get_ratings();
}

View File

@@ -0,0 +1,41 @@
<?php
/**
* Simple DTO: ShipmentRatingImplementation class.
*
* @package WPDesk\AbstractShipping
*/
namespace DPDVendor\WPDesk\AbstractShipping\Rate;
/**
* Part of AbstractShipping package which provides uniform interface between WC and Shipment API.
*
* @package WPDesk\AbstractShipping
*/
class ShipmentRatingImplementation implements \DPDVendor\WPDesk\AbstractShipping\Rate\ShipmentRating
{
/**
* Rates.
*
* @var SingleRate[]
*/
private $rates;
/**
* ShipmentRatingImplementation constructor.
*
* @param array $rates SingleRate[].
*/
public function __construct(array $rates)
{
$this->rates = $rates;
}
/**
* Get ratings.
*
* @return SingleRate[]
*/
public function get_ratings()
{
return $this->rates;
}
}

View File

@@ -0,0 +1,53 @@
<?php
/**
* Simple DTO: SingleRate class.
*
* @package WPDesk\AbstractShipping\Rate
*/
namespace DPDVendor\WPDesk\AbstractShipping\Rate;
/**
* Define Single Rate.
*
* @package WPDesk\AbstractShipping\Rate
*/
final class SingleRate
{
/**
* Service type.
*
* @var string
*/
public $service_type;
/**
* Service name.
*
* @var string
*/
public $service_name;
/**
* Total charge.
*
* @var Money
*/
public $total_charge;
/**
* Is collection point rate?
*
* @var bool
*/
public $is_collection_point_rate = \false;
/**
* Estimated delivery date. Should be here if service implements CanReturnDeliveryDate interface.
*
* @var \DateTimeInterface|null
*/
public $delivery_date;
/**
* Estimated number business days in transit. Should be here if service implements CanReturnDeliveryDate interface.
*
* @var int|null
*/
public $business_days_in_transit;
}

View File

@@ -0,0 +1,68 @@
<?php
/**
* Class BlackoutLeadDays
*
* @package WPDesk\AbstractShipping\Settings
*/
namespace DPDVendor\WPDesk\AbstractShipping\Settings;
/**
* Can calculate date according to blackout days.
*/
class BlackoutLeadDays
{
/**
* @var array
*/
private $blackout_days;
/**
* @var int
*/
private $lead_days;
/**
* BlackoutLeadDays constructor.
*
* @param array $blackout_days .
* @param int $lead_days .
*/
public function __construct(array $blackout_days, $lead_days)
{
$this->blackout_days = $blackout_days;
$this->lead_days = $lead_days;
}
/**
* @param \DateTime $calculated_date .
*
* @return \DateTime
*/
public function calculate_date(\DateTime $calculated_date)
{
$lead_days = $this->lead_days;
$one_day_date_interval = new \DateInterval('P1D');
$calculated_date = $this->increase_date_while_in_blackout_days($calculated_date, $one_day_date_interval);
while ($lead_days) {
$calculated_date = $calculated_date->add($one_day_date_interval);
$calculated_date->setTime(0, 0);
$calculated_date = $this->increase_date_while_in_blackout_days($calculated_date, $one_day_date_interval);
$lead_days--;
}
return $calculated_date;
}
/**
* @param \DateTime $calculated_date .
* @param \DateInterval $one_day_date_interval .
*
* @return \DateTime
*/
private function increase_date_while_in_blackout_days(\DateTime $calculated_date, \DateInterval $one_day_date_interval)
{
$calculated_date_week_day = $calculated_date->format('N');
while (\in_array($calculated_date_week_day, $this->blackout_days, \true)) {
$calculated_date->add($one_day_date_interval);
$calculated_date->setTime(0, 0);
$calculated_date_week_day = $calculated_date->format('N');
}
return $calculated_date;
}
}

View File

@@ -0,0 +1,92 @@
<?php
/**
* Settings container: SettingsDefinitionModifierAfter.
*
* @package WPDesk\AbstractShipping\Settings
*/
namespace DPDVendor\WPDesk\AbstractShipping\Settings\DefinitionModifier;
use DPDVendor\WPDesk\AbstractShipping\Exception\SettingsFieldNotExistsException;
use DPDVendor\WPDesk\AbstractShipping\Settings\SettingsDefinition;
use DPDVendor\WPDesk\AbstractShipping\Settings\SettingsValues;
/**
* Can decorate settings by adding settings field after given field.
*/
class SettingsDefinitionModifierAfter extends \DPDVendor\WPDesk\AbstractShipping\Settings\SettingsDefinition
{
/**
* Decorated settings definition.
*
* @var SettingsDefinition
*/
private $decorated_settings_definition;
/**
* Field id.
*
* @var string
*/
private $field_id_after;
/**
* New field id.
*
* @var string
*/
private $new_field_id;
/**
* New field.
*
* @var array
*/
private $new_field;
/**
* SettingsDefinitionModifierBefore constructor.
*
* @param SettingsDefinition $decorated_settings_definition Decorated settings definition,
* @param string $field_id_after Field id before which should be settings added.
* @param string $new_field_id New field id.
* @param array $new_field New field.
*/
public function __construct(\DPDVendor\WPDesk\AbstractShipping\Settings\SettingsDefinition $decorated_settings_definition, $field_id_after, $new_field_id, array $new_field)
{
$this->decorated_settings_definition = $decorated_settings_definition;
$this->field_id_after = $field_id_after;
$this->new_field_id = $new_field_id;
$this->new_field = $new_field;
}
/**
* Returns modified form fields.
*
* @param array $form_fields
*
* @return array
*
* @throws SettingsFieldNotExistsException
*/
public function get_form_fields()
{
$form_fields = $this->decorated_settings_definition->get_form_fields();
if (isset($form_fields[$this->field_id_after])) {
$modified_form_fields = [];
foreach ($form_fields as $field_id => $field) {
$modified_form_fields[$field_id] = $field;
if ($field_id === $this->field_id_after) {
$modified_form_fields[$this->new_field_id] = $this->new_field;
}
}
return $modified_form_fields;
}
throw new \DPDVendor\WPDesk\AbstractShipping\Exception\SettingsFieldNotExistsException(\sprintf('Field %1$s not found in settings!', $this->field_id_after));
}
/**
* Validate settings.
*
* @param SettingsValues $settings Settings values.
*
* @return bool
*/
public function validate_settings(\DPDVendor\WPDesk\AbstractShipping\Settings\SettingsValues $settings)
{
return $this->decorated_settings_definition->validate_settings($settings);
}
}

View File

@@ -0,0 +1,92 @@
<?php
/**
* Settings container: SettingsDefinitionModifierBefore.
*
* @package WPDesk\AbstractShipping\Settings
*/
namespace DPDVendor\WPDesk\AbstractShipping\Settings\DefinitionModifier;
use DPDVendor\WPDesk\AbstractShipping\Exception\SettingsFieldNotExistsException;
use DPDVendor\WPDesk\AbstractShipping\Settings\SettingsDefinition;
use DPDVendor\WPDesk\AbstractShipping\Settings\SettingsValues;
/**
* Can modify settings by adding settings field before given field.
*/
class SettingsDefinitionModifierBefore extends \DPDVendor\WPDesk\AbstractShipping\Settings\SettingsDefinition
{
/**
* Decorated settings definition.
*
* @var SettingsDefinition
*/
private $decorated_settings_definition;
/**
* Field id.
*
* @var string
*/
private $field_id_before;
/**
* New field id.
*
* @var string
*/
private $new_field_id;
/**
* New field.
*
* @var array
*/
private $new_field;
/**
* SettingsDefinitionModifierBefore constructor.
*
* @param SettingsDefinition $decorated_settings_definition Decorated settings definition,
* @param string $field_id_before Field id before which should be settings added.
* @param string $new_field_id New field id.
* @param array $new_field New field.
*/
public function __construct(\DPDVendor\WPDesk\AbstractShipping\Settings\SettingsDefinition $decorated_settings_definition, $field_id_before, $new_field_id, array $new_field)
{
$this->decorated_settings_definition = $decorated_settings_definition;
$this->field_id_before = $field_id_before;
$this->new_field_id = $new_field_id;
$this->new_field = $new_field;
}
/**
* Returns modified form fields.
*
* @param array $form_fields
*
* @return array
*
* @throws SettingsFieldNotExistsException
*/
public function get_form_fields()
{
$form_fields = $this->decorated_settings_definition->get_form_fields();
if (isset($form_fields[$this->field_id_before])) {
$modified_form_fields = [];
foreach ($form_fields as $field_id => $field) {
if ($field_id === $this->field_id_before) {
$modified_form_fields[$this->new_field_id] = $this->new_field;
}
$modified_form_fields[$field_id] = $field;
}
return $modified_form_fields;
}
throw new \DPDVendor\WPDesk\AbstractShipping\Exception\SettingsFieldNotExistsException(\sprintf('Field %1$s not found in settings!', $this->field_id_before));
}
/**
* Validate settings.
*
* @param SettingsValues $settings Settings values.
*
* @return bool
*/
public function validate_settings(\DPDVendor\WPDesk\AbstractShipping\Settings\SettingsValues $settings)
{
return $this->decorated_settings_definition->validate_settings($settings);
}
}

View File

@@ -0,0 +1,47 @@
<?php
/**
* Class AbstractDecoratorFactory
* @package WPDesk\AbstractShipping\Settings\SettingsDecorators
*/
namespace DPDVendor\WPDesk\AbstractShipping\Settings\SettingsDecorators;
use DPDVendor\WPDesk\AbstractShipping\Settings\DefinitionModifier\SettingsDefinitionModifierAfter;
use DPDVendor\WPDesk\AbstractShipping\Settings\DefinitionModifier\SettingsDefinitionModifierBefore;
use DPDVendor\WPDesk\AbstractShipping\Settings\SettingsDefinition;
/**
* Abstract factory.
*/
abstract class AbstractDecoratorFactory
{
/**
* @param SettingsDefinition $settings_definition .
* @param string $related_field_id .
* @param bool $before .
* @param string $field_id .
*
* @return SettingsDefinition
*/
public function create_decorator(\DPDVendor\WPDesk\AbstractShipping\Settings\SettingsDefinition $settings_definition, $related_field_id, $before = \true, $field_id = null)
{
$decorator_class = $this->get_settings_definition_modifier_class($before);
return new $decorator_class($settings_definition, $related_field_id, empty($field_id) ? $this->get_field_id() : $field_id, $this->get_field_settings());
}
/**
* @return string
*/
protected abstract function get_field_settings();
/**
* @return array
*/
public abstract function get_field_id();
/**
* @param bool $before .
*
* @return string
*/
protected function get_settings_definition_modifier_class($before = \true)
{
return $before ? \DPDVendor\WPDesk\AbstractShipping\Settings\DefinitionModifier\SettingsDefinitionModifierBefore::class : \DPDVendor\WPDesk\AbstractShipping\Settings\DefinitionModifier\SettingsDefinitionModifierAfter::class;
}
}

View File

@@ -0,0 +1,30 @@
<?php
/**
* Class BlackoutLeadDaysSettingsDefinitionDecoratorFactory
*
* @package WPDesk\AbstractShipping\Settings\SettingsDecorators
*/
namespace DPDVendor\WPDesk\AbstractShipping\Settings\SettingsDecorators;
/**
* Can create Blackout Lead Days settings decorator.
*/
class BlackoutLeadDaysSettingsDefinitionDecoratorFactory extends \DPDVendor\WPDesk\AbstractShipping\Settings\SettingsDecorators\AbstractDecoratorFactory
{
const OPTION_ID = 'blackout_lead_days';
/**
* @return string
*/
public function get_field_id()
{
return self::OPTION_ID;
}
/**
* @return array
*/
protected function get_field_settings()
{
return array('title' => \__('Blackout Lead Days', 'woocommerce-dpd'), 'type' => 'multiselect', 'description' => \__('Blackout Lead Days are used to define days of the week when shop is not processing orders.', 'woocommerce-dpd'), 'options' => array('1' => \__('Monday', 'woocommerce-dpd'), '2' => \__('Tuesday', 'woocommerce-dpd'), '3' => \__('Wednesday', 'woocommerce-dpd'), '4' => \__('Thursday', 'woocommerce-dpd'), '5' => \__('Friday', 'woocommerce-dpd'), '6' => \__('Saturday', 'woocommerce-dpd'), '7' => \__('Sunday', 'woocommerce-dpd')), 'custom_attributes' => array('size' => 7), 'class' => 'wc-enhanced-select', 'desc_tip' => \true, 'default' => '');
}
}

View File

@@ -0,0 +1,31 @@
<?php
/**
* Settings container: SettingsDefinition.
*
* @package WPDesk\AbstractShipping\Settings
*/
namespace DPDVendor\WPDesk\AbstractShipping\Settings;
/**
* Abstract class for create default settings data.
*
* @package WPDesk\AbstractShipping\Settings
*/
abstract class SettingsDefinition
{
/**
* Validate settings.
*
* @param SettingsValues $settings Settings values.
*
* @return bool
*/
public abstract function validate_settings(\DPDVendor\WPDesk\AbstractShipping\Settings\SettingsValues $settings);
/**
* Get settings.
*
* @return array
*/
public abstract function get_form_fields();
}

View File

@@ -0,0 +1,34 @@
<?php
/**
* Settings container: SettingsDefinition.
*
* @package WPDesk\AbstractShipping\Settings
*/
namespace DPDVendor\WPDesk\AbstractShipping\Settings;
/**
* Interface for SettingsValuesAsArray class.
*
* @package WPDesk\AbstractShipping\Settings
*/
interface SettingsValues
{
/**
* Get value.
*
* @param string $name Setting name.
* @param string|null $default Default value.
*
* @return mixed
*/
public function get_value($name, $default = null);
/**
* Has value.
*
* @param string $name Setting name.
*
* @return bool
*/
public function has_value($name);
}

View File

@@ -0,0 +1,64 @@
<?php
/**
* Settings container: SettingsValuesAsArray.
*
* @package WPDesk\AbstractShipping\Settings
*/
namespace DPDVendor\WPDesk\AbstractShipping\Settings;
/**
* Container class for settings data.
*
* @package WPDesk\AbstractShipping\Settings
*/
class SettingsValuesAsArray implements \DPDVendor\WPDesk\AbstractShipping\Settings\SettingsValues
{
/**
* Values.
*
* @var array
*/
private $values;
/**
* SettingsValuesAsArray constructor.
*
* @param array $values Array values.
*/
public function __construct(array $values)
{
$this->values = $values;
}
/**
* Get value.
*
* @param string $name Setting name.
* @param string|null $default Default value if no value found.
*
* @return mixed
*/
public function get_value($name, $default = null)
{
return $this->has_value($name) ? $this->values[$name] : $default;
}
/**
* Has value.
*
* @param string $name Setting name.
*
* @return bool
*/
public function has_value($name)
{
return isset($this->values[$name]);
}
/**
* Get settings md5 hash.
*
* @return string
*/
public function get_settings_md5_hash()
{
return \md5(\json_encode($this->values));
}
}

View File

@@ -0,0 +1,59 @@
<?php
/**
* Simple DTO: Address class.
*
* @package WPDesk\AbstractShipping\Shipment
*/
namespace DPDVendor\WPDesk\AbstractShipping\Shipment;
/**
* Class that stores the customer's address data.
*
* @package WPDesk\AbstractShipping\Shipment
*/
final class Address
{
/**
* Adress line 2.
*
* @var string
*/
public $address_line1;
/**
* Adress line 2.
*
* @var string
*/
public $address_line2;
/**
* Postal code.
*
* @var string
*/
public $postal_code;
/**
* City.
*
* @var string
*/
public $city;
/**
* State code.
*
* @var string
*/
public $state_code;
/**
* Country code.
*
* @var string
*/
public $country_code;
/**
* Street lines
*
* @var array
*/
public $street_lines = [];
}

View File

@@ -0,0 +1,47 @@
<?php
/**
* Simple DTO: Client class.
*
* @package WPDesk\AbstractShipping\Shipment
*/
namespace DPDVendor\WPDesk\AbstractShipping\Shipment;
/**
* Class that stores the client data
*
* @package WPDesk\AbstractShipping\Shipment
*/
final class Client
{
/**
* Address.
*
* @var Address
*/
public $address;
/**
* E-mail.
*
* @var string
*/
public $email;
/**
* Phone number.
*
* @var string|null
*/
public $phone_number;
/**
* Name.
*
* @var string
*/
public $name;
/**
* Company name.
*
* @var string|null
*/
public $company_name;
}

View File

@@ -0,0 +1,45 @@
<?php
/**
* Simple DTO: Dimensions class.
*
* @package WPDesk\AbstractShipping\Shipment
*/
namespace DPDVendor\WPDesk\AbstractShipping\Shipment;
/**
* Class that stores the package dimensions.
*
* @package WPDesk\AbstractShipping\Shipment
*/
final class Dimensions
{
const DIMENSION_UNIT_MM = 'MM';
const DIMENSION_UNIT_CM = 'CM';
const DIMENSION_UNIT_M = 'M';
const DIMENSION_UNIT_IN = 'IN';
/**
* Height.
*
* @var int
*/
public $height;
/**
* Width.
*
* @var int
*/
public $width;
/**
* Length.
*
* @var int
*/
public $length;
/**
* Dimension unit.
*
* @var string
*/
public $dimensions_unit;
}

View File

@@ -0,0 +1,42 @@
<?php
/**
* Simple DTO: Item class.
*
* @package WPDesk\AbstractShipping\Shipment
*/
namespace DPDVendor\WPDesk\AbstractShipping\Shipment;
use DPDVendor\WPDesk\AbstractShipping\Rate\Money;
/**
* Class that stores items for package.
*
* @package WPDesk\AbstractShipping\Shipment
*/
final class Item
{
/**
* Item name.
*
* @var string
*/
public $name;
/**
* Item weight.
*
* @var Weight
*/
public $weight;
/**
* Item dimensions.
*
* @var Dimensions
*/
public $dimensions;
/**
* Declared value of item ie. for Insurance.
*
* @var Money|null
*/
public $declared_value;
}

View File

@@ -0,0 +1,46 @@
<?php
/**
* Simple DTO: Package class.
*
* @package WPDesk\AbstractShipping\Shipment
*/
namespace DPDVendor\WPDesk\AbstractShipping\Shipment;
/**
* Class that stores packages data for shipment.
*
* @package WPDesk\AbstractShipping\Shipment
*/
final class Package
{
/** Items packed in this package.
*
* @var Item[]
*/
public $items;
/**
* Item weight. Can be null if not packed.
*
* @var Weight|null
*/
public $weight;
/**
* Item dimensions. Can be null if not packed.
*
* @var Dimensions|null
*/
public $dimensions;
/**
* Packages can be a special packages with type ie. CUBE_BOX . If not set then shipment should use custom type.
*
* @var string|null
*/
public $package_type;
/**
* Packages can have a descriptive name.
*
* @var string|null
*/
public $description;
}

View File

@@ -0,0 +1,51 @@
<?php
/**
* Simple DTO: Shipment class.
*
* @package WPDesk\AbstractShipping\Shipment
*/
namespace DPDVendor\WPDesk\AbstractShipping\Shipment;
/**
* Class that stores shipment data.
*
* @package WPDesk\AbstractShipping\Shipment
*/
final class Shipment
{
/**
* Ship from
*
* @var Client
*/
public $ship_from;
/**
* Ship to.
*
* @var Client
*/
public $ship_to;
/**
* Packages.
*
* @var Package[]
*/
public $packages;
/**
* Should use insurance if possible. Declared item values will be in Item::declared_value.
*
* @var bool
*/
public $insurance = \false;
/**
* If packed then Package::Weight, Package::Dimension should be not null.
* Package:package_type and Package:description can still be null.
*
* It means that packer tried to pack items into packages and size/weight of these packages is more important for rating
* than the size of items.
*
* @var bool
*/
public $packed = \false;
}

View File

@@ -0,0 +1,34 @@
<?php
/**
* Simple DTO: Shipment class.
*
* @package WPDesk\AbstractShipping\Shipment
*/
namespace DPDVendor\WPDesk\AbstractShipping\Shipment;
/**
* Class that stores weight data for Item.
*
* @package WPDesk\AbstractShipping\Shipment
*/
final class Weight
{
const WEIGHT_UNIT_KG = 'KG';
const WEIGHT_UNIT_G = 'G';
const WEIGHT_UNIT_LB = 'LB';
const WEIGHT_UNIT_LBS = 'LBS';
const WEIGHT_UNIT_OZ = 'OZ';
/**
* Weight KGS by default
*
* @var float
*/
public $weight;
/**
* Weight unit.
*
* @var string
*/
public $weight_unit;
}

View File

@@ -0,0 +1,43 @@
<?php
/**
* Shipping service abstract: ShippingService class.
*
* @package WPDesk\AbstractShipping
*/
namespace DPDVendor\WPDesk\AbstractShipping;
use DPDVendor\Psr\Log\LoggerAwareInterface;
use DPDVendor\WPDesk\AbstractShipping\Settings\SettingsDefinition;
/**
* Basic abstract class for shipping classes.
*
* @package WPDesk\AbstractShipping
*/
abstract class ShippingService implements \DPDVendor\Psr\Log\LoggerAwareInterface
{
/**
* Get unique ID.
*
* @return string
*/
public abstract function get_unique_id();
/**
* Get name.
*
* @return string
*/
public abstract function get_name();
/**
* Get description.
*
* @return string
*/
public abstract function get_description();
/**
* Get settings definitions.
*
* @return SettingsDefinition
*/
public abstract function get_settings_definition();
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* Capability: CanRate class
*
* @package WPDesk\AbstractShipping\Shipment
*/
namespace DPDVendor\WPDesk\AbstractShipping\ShippingServiceCapability;
/**
* Tag interface that informs if Service supports Shipment::insurance flag.
*
* @package WPDesk\AbstractShipping\ShippingServiceCapability
*/
interface CanInsure
{
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* Capability: CanRate class
*
* @package WPDesk\AbstractShipping\Shipment
*/
namespace DPDVendor\WPDesk\AbstractShipping\ShippingServiceCapability;
/**
* Tag interface that informs if Service supports Sghipment::packed flag.
*
* @package WPDesk\AbstractShipping\ShippingServiceCapability
*/
interface CanPack
{
}

View File

@@ -0,0 +1,37 @@
<?php
/**
* Capability: CanRate class
*
* @package WPDesk\AbstractShipping\Shipment
*/
namespace DPDVendor\WPDesk\AbstractShipping\ShippingServiceCapability;
use DPDVendor\WPDesk\AbstractShipping\Rate\ShipmentRating;
use DPDVendor\WPDesk\AbstractShipping\Settings\SettingsValues;
use DPDVendor\WPDesk\AbstractShipping\Shipment\Shipment;
/**
* Interface for rate shipment
*
* @package WPDesk\AbstractShipping\ShippingServiceCapability
*/
interface CanRate
{
/**
* Rate shipment.
*
* @param SettingsValues $settings Settings.
* @param Shipment $shipment Shipment.
*
* @return ShipmentRating
*/
public function rate_shipment(\DPDVendor\WPDesk\AbstractShipping\Settings\SettingsValues $settings, \DPDVendor\WPDesk\AbstractShipping\Shipment\Shipment $shipment);
/**
* Is rate enabled?
*
* @param SettingsValues $settings .
*
* @return bool
*/
public function is_rate_enabled(\DPDVendor\WPDesk\AbstractShipping\Settings\SettingsValues $settings);
}

View File

@@ -0,0 +1,37 @@
<?php
/**
* Capability: CanRateToCollectionPoint class
*
* @package WPDesk\AbstractShipping\ShippingServiceCapability
*/
namespace DPDVendor\WPDesk\AbstractShipping\ShippingServiceCapability;
use DPDVendor\WPDesk\AbstractShipping\CollectionPoints\CollectionPoint;
use DPDVendor\WPDesk\AbstractShipping\Rate\ShipmentRating;
use DPDVendor\WPDesk\AbstractShipping\Settings\SettingsValues;
use DPDVendor\WPDesk\AbstractShipping\Shipment\Shipment;
/**
* Interface for rate shipment to collection point
*/
interface CanRateToCollectionPoint
{
/**
* Rate shipment to collection point.
*
* @param SettingsValues $settings Settings.
* @param Shipment $shipment Shipment.
* @param CollectionPoint $collection_point Collection point.
*
* @return ShipmentRating
*/
public function rate_shipment_to_collection_point(\DPDVendor\WPDesk\AbstractShipping\Settings\SettingsValues $settings, \DPDVendor\WPDesk\AbstractShipping\Shipment\Shipment $shipment, \DPDVendor\WPDesk\AbstractShipping\CollectionPoints\CollectionPoint $collection_point);
/**
* Is rate to collection point enabled?
*
* @param SettingsValues $settings
*
* @return mixed
*/
public function is_rate_to_collection_point_enabled(\DPDVendor\WPDesk\AbstractShipping\Settings\SettingsValues $settings);
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* Capability: CanRate class
*
* @package WPDesk\AbstractShipping\Shipment
*/
namespace DPDVendor\WPDesk\AbstractShipping\ShippingServiceCapability;
/**
* Tag interface that informs if Service supports SingleRate::delivery_date field.
*
* @package WPDesk\AbstractShipping\ShippingServiceCapability
*/
interface CanReturnDeliveryDate
{
}

View File

@@ -0,0 +1,32 @@
<?php
/**
* Capability: CanTestSettings class
*
* @package WPDesk\AbstractShipping\ShippingServiceCapability
*/
namespace DPDVendor\WPDesk\AbstractShipping\ShippingServiceCapability;
use DPDVendor\Psr\Log\LoggerInterface;
use DPDVendor\WPDesk\AbstractShipping\Settings\SettingsValues;
/**
* Interface for checking connection to API.
*/
interface CanTestSettings
{
/**
* Pings API.
* Returns empty string on success or error message on failure.
*
* @param SettingsValues $settings .
* @param LoggerInterface $logger .
* @return string
*/
public function check_connection(\DPDVendor\WPDesk\AbstractShipping\Settings\SettingsValues $settings, \DPDVendor\Psr\Log\LoggerInterface $logger);
/**
* Returns field ID after which API Status field should be added.
*
* @return string
*/
public function get_field_before_api_status_field();
}

View File

@@ -0,0 +1,24 @@
<?php
/**
* Capability: HasSettings class.
*
* @package WPDesk\AbstractShipping\ShippingServiceCapability
*/
namespace DPDVendor\WPDesk\AbstractShipping\ShippingServiceCapability;
use DPDVendor\WPDesk\AbstractShipping\Settings\SettingsDefinition;
/**
* Interface for get settings definition
*
* @package WPDesk\AbstractShipping\ShippingServiceCapability
*/
interface HasSettings
{
/**
* Get settings definition.
*
* @return SettingsDefinition
*/
public function get_settings_definition();
}

View File

@@ -0,0 +1,80 @@
<?php
namespace DPDVendor\WPDesk\AbstractShipping\Shop;
/**
* Define some helper functions.
*
* @TODO: move from AbstractShipping to AbstractWooCommerceShipping ?
*/
interface ShopSettings
{
/**
* Get countries.
*
* @return string[]
*/
public function get_countries();
/**
* Get EU countries.
*
* @return string[]
*/
public function get_eu_countries();
/**
* Get states.
*
* @param null|string $cc Country code.
*
* @return array|false
*/
public function get_states($cc = null);
/**
* Get WooCommerce country.
*
* @return string
*/
public function get_origin_country();
/**
* Get locale.
*
* @return string
*/
public function get_locale();
/**
* Get weight unit.
*
* @return string
*/
public function get_weight_unit();
/**
* Get WooCommerce currency.
*
* @return string
*/
public function get_currency();
/**
* Get default shop currency.
*
* @return string
*/
public function get_default_currency();
/**
* Get price rounding precision.
*
* @return int
*/
public function get_price_rounding_precision();
/**
* Is production?
*
* @return bool
*/
public function is_testing();
/**
* Is tax enabled?
*
* @return bool
*/
public function is_tax_enabled();
}

View File

@@ -0,0 +1,80 @@
<?php
/**
* UnitConversion: Lenght Conversion.
*
* @package WPDesk\AbstractShipping\Shipment
*/
namespace DPDVendor\WPDesk\AbstractShipping\UnitConversion;
use DPDVendor\WPDesk\AbstractShipping\Exception\UnitConversionException;
use DPDVendor\WPDesk\AbstractShipping\Shipment\Dimensions;
use DPDVendor\WPDesk\AbstractShipping\Shipment\Weight;
/**
* Can convert length between different measure types
*/
class UniversalDimension
{
private $unit_calc = [\DPDVendor\WPDesk\AbstractShipping\Shipment\Dimensions::DIMENSION_UNIT_IN => 25.4, \DPDVendor\WPDesk\AbstractShipping\Shipment\Dimensions::DIMENSION_UNIT_MM => 1, \DPDVendor\WPDesk\AbstractShipping\Shipment\Dimensions::DIMENSION_UNIT_CM => 10, \DPDVendor\WPDesk\AbstractShipping\Shipment\Dimensions::DIMENSION_UNIT_M => 1000];
/**
* Length.
*
* @var float
*/
private $length;
/**
* Precision.
*
* @var int
*/
private $precision;
/**
* LengthConverter constructor.
*
* @param float $length Length.
* @param string $from_unit From unit.
* @param int $precision .
*
* @throws UnitConversionException Dimension exception.
*/
public function __construct($length, $from_unit, $precision = 3)
{
$this->precision = $precision;
$this->length = $this->to_mm($length, $from_unit);
}
/**
* @param float $length .
* @param string $unit .
*
* @return false|float
* @throws UnitConversionException
*/
private function to_mm($length, $unit)
{
$unit = \strtoupper($unit);
if (isset($this->unit_calc[$unit])) {
$calc = $this->unit_calc[$unit];
return \round($length * $calc, $this->precision);
}
throw new \DPDVendor\WPDesk\AbstractShipping\Exception\UnitConversionException(\sprintf('Can\'t support "%s" unit', $unit));
}
/**
* Convert to target unit. Returns 0 if confused.
*
* @param string $to_unit Target unit.
* @param int $precision .
*
* @return float
*
* @throws UnitConversionException Dimension exception.
*/
public function as_unit_rounded($to_unit, $precision = 2)
{
$to_unit = \strtoupper($to_unit);
if (isset($this->unit_calc[$to_unit])) {
$calc = $this->unit_calc[$to_unit];
return \round($this->length / $calc, $precision);
}
throw new \DPDVendor\WPDesk\AbstractShipping\Exception\UnitConversionException(\__('Can\'t convert weight to target unit.', 'woocommerce-dpd'));
}
}

View File

@@ -0,0 +1,95 @@
<?php
/**
* UnitConversion: Weight Conversion.
*
* @package WPDesk\AbstractShipping\Shipment
*/
namespace DPDVendor\WPDesk\AbstractShipping\UnitConversion;
use DPDVendor\WPDesk\AbstractShipping\Exception\UnitConversionException;
use DPDVendor\WPDesk\AbstractShipping\Shipment\Weight;
/**
* Can convert weight between different measure types
*/
class UniversalWeight
{
/**
* Weight.
*
* @var float
*/
private $weight;
const UNIT_CALC = [\DPDVendor\WPDesk\AbstractShipping\Shipment\Weight::WEIGHT_UNIT_G => 1, \DPDVendor\WPDesk\AbstractShipping\Shipment\Weight::WEIGHT_UNIT_LB => 453.59237, \DPDVendor\WPDesk\AbstractShipping\Shipment\Weight::WEIGHT_UNIT_KG => 1000, \DPDVendor\WPDesk\AbstractShipping\Shipment\Weight::WEIGHT_UNIT_OZ => 28.34952];
/**
* @var int
*/
private $precision;
/**
* WeightConverter constructor.
*
* @param float $weight Weight.
* @param string $unit Unit.
* @param int $precision .
*
* @throws UnitConversionException Weight exception.
*/
public function __construct($weight, $unit, $precision = 3)
{
$this->precision = $precision;
$this->weight = $this->to_grams($weight, $unit);
}
/**
* Unify all units to grams.
*
* @param float $weight Weight.
* @param string $unit Unit.
*
* @return float
* @throws \WPDesk\AbstractShipping\Exception\UnitConversionException Unit
* Conversion.
*/
private function to_grams($weight, $unit)
{
$unit = \strtoupper($unit);
if (\DPDVendor\WPDesk\AbstractShipping\Shipment\Weight::WEIGHT_UNIT_LBS === $unit) {
$unit = \DPDVendor\WPDesk\AbstractShipping\Shipment\Weight::WEIGHT_UNIT_LB;
}
if (\DPDVendor\WPDesk\AbstractShipping\Shipment\Weight::WEIGHT_UNIT_G === $unit) {
return $weight;
}
$calc = self::UNIT_CALC[$unit];
switch ($unit) {
case \DPDVendor\WPDesk\AbstractShipping\Shipment\Weight::WEIGHT_UNIT_KG:
case \DPDVendor\WPDesk\AbstractShipping\Shipment\Weight::WEIGHT_UNIT_LB:
case \DPDVendor\WPDesk\AbstractShipping\Shipment\Weight::WEIGHT_UNIT_OZ:
return \round($weight * $calc, $this->precision);
}
throw new \DPDVendor\WPDesk\AbstractShipping\Exception\UnitConversionException(\sprintf('Can\'t support "%s" unit', $unit));
}
/**
* Convert to target unit. Returns 0 if confused.
*
* @param string $target_unit Target unit.
* @param int $precision .
*
* @return float
*
* @throws UnitConversionException Weight exception.
*/
public function as_unit_rounded($target_unit, $precision = 2)
{
$target_unit = \strtoupper($target_unit);
$calc = self::UNIT_CALC[$target_unit];
switch ($target_unit) {
case \DPDVendor\WPDesk\AbstractShipping\Shipment\Weight::WEIGHT_UNIT_G:
return $this->weight;
case \DPDVendor\WPDesk\AbstractShipping\Shipment\Weight::WEIGHT_UNIT_KG:
case \DPDVendor\WPDesk\AbstractShipping\Shipment\Weight::WEIGHT_UNIT_LB:
case \DPDVendor\WPDesk\AbstractShipping\Shipment\Weight::WEIGHT_UNIT_OZ:
return \round($this->weight / $calc, $precision);
default:
throw new \DPDVendor\WPDesk\AbstractShipping\Exception\UnitConversionException(\__('Can\'t convert weight to target unit.', 'woocommerce-dpd'));
}
}
}