| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class AttributeMapper. |
| 4 |
* |
| 5 |
* @package Packetery |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Order; |
| 11 |
|
| 12 |
use Packetery\Core\Entity; |
| 13 |
use WC_Data_Exception; |
| 14 |
use WC_Order; |
| 15 |
|
| 16 |
/** |
| 17 |
* Class AttributeMapper. |
| 18 |
* |
| 19 |
* @package Packetery |
| 20 |
*/ |
| 21 |
class AttributeMapper { |
| 22 |
|
| 23 |
// Business name of pickup point. |
| 24 |
|
| 25 |
/** |
| 26 |
* Updates order entity from props to save. |
| 27 |
* |
| 28 |
* @param Entity\Order $orderEntity Order entity. |
| 29 |
* @param array $propsToSave Props to save. |
| 30 |
* |
| 31 |
* @return Entity\PickupPoint |
| 32 |
*/ |
| 33 |
public function toOrderEntityPickupPoint( Entity\Order $orderEntity, array $propsToSave ): Entity\PickupPoint { |
| 34 |
$pickupPoint = $orderEntity->getPickupPoint(); |
| 35 |
if ( null === $pickupPoint ) { |
| 36 |
$pickupPoint = new Entity\PickupPoint(); |
| 37 |
} |
| 38 |
|
| 39 |
foreach ( $propsToSave as $attrName => $attrValue ) { |
| 40 |
switch ( $attrName ) { |
| 41 |
case Attribute::POINT_ID: |
| 42 |
$pickupPoint->setId( $attrValue ); |
| 43 |
break; |
| 44 |
case Attribute::POINT_NAME: |
| 45 |
$pickupPoint->setName( $attrValue ); |
| 46 |
break; |
| 47 |
case Attribute::POINT_URL: |
| 48 |
$pickupPoint->setUrl( $attrValue ); |
| 49 |
break; |
| 50 |
case Attribute::POINT_STREET: |
| 51 |
$pickupPoint->setStreet( $attrValue ); |
| 52 |
break; |
| 53 |
case Attribute::POINT_ZIP: |
| 54 |
$pickupPoint->setZip( $attrValue ); |
| 55 |
break; |
| 56 |
case Attribute::POINT_CITY: |
| 57 |
$pickupPoint->setCity( $attrValue ); |
| 58 |
break; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
return $pickupPoint; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Update order shipping. |
| 67 |
* |
| 68 |
* @param WC_Order $wcOrder WC Order. |
| 69 |
* @param string $attributeName Attribute name. |
| 70 |
* @param string $value Value. |
| 71 |
* |
| 72 |
* @return void |
| 73 |
* @throws WC_Data_Exception When shipping input is invalid. |
| 74 |
*/ |
| 75 |
public function toWcOrderShippingAddress( WC_Order $wcOrder, string $attributeName, string $value ): void { |
| 76 |
if ( Attribute::POINT_STREET === $attributeName ) { |
| 77 |
$wcOrder->set_shipping_address_1( $value ); |
| 78 |
$wcOrder->set_shipping_address_2( '' ); |
| 79 |
} |
| 80 |
if ( Attribute::POINT_PLACE === $attributeName ) { |
| 81 |
$wcOrder->set_shipping_company( $value ); |
| 82 |
} |
| 83 |
if ( Attribute::POINT_CITY === $attributeName ) { |
| 84 |
$wcOrder->set_shipping_city( $value ); |
| 85 |
} |
| 86 |
if ( Attribute::POINT_ZIP === $attributeName ) { |
| 87 |
$wcOrder->set_shipping_postcode( $value ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* From prepared properties to order Size. |
| 93 |
* |
| 94 |
* @param Entity\Order $order Order. |
| 95 |
* @param array $propsToSave Prepared properties. |
| 96 |
* |
| 97 |
* @return Entity\Size |
| 98 |
*/ |
| 99 |
public function toOrderSize( Entity\Order $order, array $propsToSave ): Entity\Size { |
| 100 |
$orderSize = $order->getSize(); |
| 101 |
if ( null === $orderSize ) { |
| 102 |
$orderSize = new Entity\Size(); |
| 103 |
} |
| 104 |
|
| 105 |
foreach ( $propsToSave as $attrName => $attrValue ) { |
| 106 |
switch ( $attrName ) { |
| 107 |
case Metabox::FIELD_WEIGHT: |
| 108 |
$order->setWeight( $attrValue ); |
| 109 |
break; |
| 110 |
case Metabox::FIELD_WIDTH: |
| 111 |
$orderSize->setWidth( $attrValue ); |
| 112 |
break; |
| 113 |
case Metabox::FIELD_LENGTH: |
| 114 |
$orderSize->setLength( $attrValue ); |
| 115 |
break; |
| 116 |
case Metabox::FIELD_HEIGHT: |
| 117 |
$orderSize->setHeight( $attrValue ); |
| 118 |
break; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return $orderSize; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* From post data to validated address both in frontend and backend. |
| 127 |
* |
| 128 |
* @param array $values Data from form. |
| 129 |
* |
| 130 |
* @return Entity\Address |
| 131 |
*/ |
| 132 |
public function toValidatedAddress( array $values ): Entity\Address { |
| 133 |
$address = new Entity\Address( |
| 134 |
$values[ Attribute::ADDRESS_STREET ], |
| 135 |
$values[ Attribute::ADDRESS_CITY ], |
| 136 |
$values[ Attribute::ADDRESS_POST_CODE ] |
| 137 |
); |
| 138 |
$address->setHouseNumber( $values[ Attribute::ADDRESS_HOUSE_NUMBER ] ); |
| 139 |
$address->setCounty( $values[ Attribute::ADDRESS_COUNTY ] ); |
| 140 |
$address->setLatitude( $values[ Attribute::ADDRESS_LATITUDE ] ); |
| 141 |
$address->setLongitude( $values[ Attribute::ADDRESS_LONGITUDE ] ); |
| 142 |
|
| 143 |
return $address; |
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
|