PluginProbe
Packeta / 2.0.9
Packeta v2.0.9
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 2.0.9, at src/Packetery/Module/Order/AttributeMapper.php

201 lines 5.3 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<string, string|float|int|true|null> $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 ( $pickupPoint === null ) {
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
44 break;
45 case Attribute::POINT_NAME:
46 $pickupPoint->setName( $attrValue );
47
48 break;
49 case Attribute::POINT_URL:
50 $pickupPoint->setUrl( $attrValue );
51
52 break;
53 case Attribute::POINT_STREET:
54 $pickupPoint->setStreet( $attrValue );
55
56 break;
57 case Attribute::POINT_ZIP:
58 $pickupPoint->setZip( $attrValue );
59
60 break;
61 case Attribute::POINT_CITY:
62 $pickupPoint->setCity( $attrValue );
63
64 break;
65 }
66 }
67
68 return $pickupPoint;
69 }
70
71 /**
72 * Update order shipping.
73 *
74 * @param WC_Order $wcOrder WC Order.
75 * @param string $attributeName Attribute name.
76 * @param string $value Value.
77 *
78 * @return void
79 * @throws WC_Data_Exception When shipping input is invalid.
80 */
81 public function toWcOrderShippingAddress( WC_Order $wcOrder, string $attributeName, string $value ): void {
82 if ( $attributeName === Attribute::POINT_STREET ) {
83 $wcOrder->set_shipping_address_1( $value );
84 $wcOrder->set_shipping_address_2( '' );
85 }
86 if ( $attributeName === Attribute::POINT_PLACE ) {
87 $wcOrder->set_shipping_company( $value );
88 }
89 if ( $attributeName === Attribute::POINT_CITY ) {
90 $wcOrder->set_shipping_city( $value );
91 }
92 if ( $attributeName === Attribute::POINT_ZIP ) {
93 $wcOrder->set_shipping_postcode( $value );
94 }
95 }
96
97 /**
98 * Maps validated address from checkout data to WC order shipping address.
99 *
100 * @param WC_Order $wcOrder WC order.
101 * @param array $checkoutData Checkout data.
102 *
103 * @return void
104 */
105 public function validatedAddressToWcOrderShippingAddress( WC_Order $wcOrder, array $checkoutData ): void {
106 // Change all address fields except customer name and country.
107 $houseNumberSuffix = $checkoutData[ Attribute::ADDRESS_HOUSE_NUMBER ] ? ' ' . $checkoutData[ Attribute::ADDRESS_HOUSE_NUMBER ] : '';
108 $wcOrder->set_shipping_company( '' );
109 $wcOrder->set_shipping_address_1( $checkoutData[ Attribute::ADDRESS_STREET ] . $houseNumberSuffix );
110 $wcOrder->set_shipping_address_2( '' );
111 $wcOrder->set_shipping_city( $checkoutData[ Attribute::ADDRESS_CITY ] );
112 $wcOrder->set_shipping_state( '' );
113 $wcOrder->set_shipping_postcode( $checkoutData[ Attribute::ADDRESS_POST_CODE ] );
114 }
115
116 /**
117 * From prepared properties to order Size.
118 *
119 * @param Entity\Order $order Order.
120 * @param array<string, string|float|int|true|null> $propsToSave Prepared properties.
121 *
122 * @return Entity\Size
123 */
124 public function toOrderSize( Entity\Order $order, array $propsToSave ): Entity\Size {
125 $orderSize = $order->getSize();
126 if ( $orderSize === null ) {
127 $orderSize = new Entity\Size();
128 }
129
130 foreach ( $propsToSave as $attrName => $attrValue ) {
131 switch ( $attrName ) {
132 // TODO: Setting these values shouldn't be done here?
133 case Form::FIELD_WEIGHT:
134 $order->setWeight( $attrValue );
135
136 break;
137 case Form::FIELD_WIDTH:
138 $orderSize->setWidth( $attrValue );
139
140 break;
141 case Form::FIELD_LENGTH:
142 $orderSize->setLength( $attrValue );
143
144 break;
145 case Form::FIELD_HEIGHT:
146 $orderSize->setHeight( $attrValue );
147
148 break;
149 }
150 }
151
152 return $orderSize;
153 }
154
155 /**
156 * From post data to validated address both in frontend and backend.
157 *
158 * @param array $values Data from form.
159 *
160 * @return Entity\Address
161 */
162 public function toValidatedAddress( array $values ): Entity\Address {
163 $address = $this->createAddress( $values );
164 $address->setHouseNumber( $values[ Attribute::ADDRESS_HOUSE_NUMBER ] );
165 $address->setCounty( $values[ Attribute::ADDRESS_COUNTY ] );
166 $address->setLatitude( $values[ Attribute::ADDRESS_LATITUDE ] );
167 $address->setLongitude( $values[ Attribute::ADDRESS_LONGITUDE ] );
168
169 return $address;
170 }
171
172 /**
173 * From post data to validated address both in frontend and backend.
174 *
175 * @param array $values Data from form.
176 *
177 * @return Entity\Address
178 */
179 public function toCarDeliveryAddress( array $values ): Entity\Address {
180 $address = $this->createAddress( $values );
181 $address->setHouseNumber( $values[ Attribute::ADDRESS_HOUSE_NUMBER ] );
182
183 return $address;
184 }
185
186 /**
187 * Creates new Address
188 *
189 * @param array $values Data from form.
190 *
191 * @return Entity\Address
192 */
193 private function createAddress( array $values ): Entity\Address {
194 return new Entity\Address(
195 $values[ Attribute::ADDRESS_STREET ],
196 $values[ Attribute::ADDRESS_CITY ],
197 $values[ Attribute::ADDRESS_POST_CODE ]
198 );
199 }
200 }
201