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

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