PluginProbe
Packeta / 1.5.0
Packeta v1.5.0
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Order / AttributeMapper.php

AttributeMapper.php in Packeta 1.5.0, at src/Packetery/Module/Order/AttributeMapper.php

151 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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::CARRIER_ID:
42 // TODO: Remove in carrier refactor.
43 $orderEntity->setCarrierId( $attrValue );
44 break;
45 case Attribute::POINT_ID:
46 $pickupPoint->setId( $attrValue );
47 break;
48 case Attribute::POINT_NAME:
49 $pickupPoint->setName( $attrValue );
50 break;
51 case Attribute::POINT_URL:
52 $pickupPoint->setUrl( $attrValue );
53 break;
54 case Attribute::POINT_STREET:
55 $pickupPoint->setStreet( $attrValue );
56 break;
57 case Attribute::POINT_ZIP:
58 $pickupPoint->setZip( $attrValue );
59 break;
60 case Attribute::POINT_CITY:
61 $pickupPoint->setCity( $attrValue );
62 break;
63 }
64 }
65
66 return $pickupPoint;
67 }
68
69 /**
70 * Update order shipping.
71 *
72 * @param WC_Order $wcOrder WC Order.
73 * @param string $attributeName Attribute name.
74 * @param string $value Value.
75 *
76 * @return void
77 * @throws WC_Data_Exception When shipping input is invalid.
78 */
79 public function toWcOrderShippingAddress( WC_Order $wcOrder, string $attributeName, string $value ): void {
80 if ( Attribute::POINT_STREET === $attributeName ) {
81 $wcOrder->set_shipping_address_1( $value );
82 $wcOrder->set_shipping_address_2( '' );
83 }
84 if ( Attribute::POINT_PLACE === $attributeName ) {
85 $wcOrder->set_shipping_company( $value );
86 }
87 if ( Attribute::POINT_CITY === $attributeName ) {
88 $wcOrder->set_shipping_city( $value );
89 }
90 if ( Attribute::POINT_ZIP === $attributeName ) {
91 $wcOrder->set_shipping_postcode( $value );
92 }
93 }
94
95 /**
96 * From prepared properties to order Size.
97 *
98 * @param Entity\Order $order Order.
99 * @param array $propsToSave Prepared properties.
100 *
101 * @return Entity\Size
102 */
103 public function toOrderSize( Entity\Order $order, array $propsToSave ): Entity\Size {
104 $orderSize = $order->getSize();
105 if ( null === $orderSize ) {
106 $orderSize = new Entity\Size();
107 }
108
109 foreach ( $propsToSave as $attrName => $attrValue ) {
110 switch ( $attrName ) {
111 case Metabox::FIELD_WEIGHT:
112 $order->setWeight( $attrValue );
113 break;
114 case Metabox::FIELD_WIDTH:
115 $orderSize->setWidth( $attrValue );
116 break;
117 case Metabox::FIELD_LENGTH:
118 $orderSize->setLength( $attrValue );
119 break;
120 case Metabox::FIELD_HEIGHT:
121 $orderSize->setHeight( $attrValue );
122 break;
123 }
124 }
125
126 return $orderSize;
127 }
128
129 /**
130 * From post data to validated address both in frontend and backend.
131 *
132 * @param array $values Data from form.
133 *
134 * @return Entity\Address
135 */
136 public function toValidatedAddress( array $values ): Entity\Address {
137 $address = new Entity\Address(
138 $values[ Attribute::ADDRESS_STREET ],
139 $values[ Attribute::ADDRESS_CITY ],
140 $values[ Attribute::ADDRESS_POST_CODE ]
141 );
142 $address->setHouseNumber( $values[ Attribute::ADDRESS_HOUSE_NUMBER ] );
143 $address->setCounty( $values[ Attribute::ADDRESS_COUNTY ] );
144 $address->setLatitude( $values[ Attribute::ADDRESS_LATITUDE ] );
145 $address->setLongitude( $values[ Attribute::ADDRESS_LONGITUDE ] );
146
147 return $address;
148 }
149
150 }
151