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

196 lines 5.1 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 case Form::FIELD_WIDTH:
133 $orderSize->setWidth( $attrValue );
134
135 break;
136 case Form::FIELD_LENGTH:
137 $orderSize->setLength( $attrValue );
138
139 break;
140 case Form::FIELD_HEIGHT:
141 $orderSize->setHeight( $attrValue );
142
143 break;
144 }
145 }
146
147 return $orderSize;
148 }
149
150 /**
151 * From post data to validated address both in frontend and backend.
152 *
153 * @param array $values Data from form.
154 *
155 * @return Entity\Address
156 */
157 public function toValidatedAddress( array $values ): Entity\Address {
158 $address = $this->createAddress( $values );
159 $address->setHouseNumber( $values[ Attribute::ADDRESS_HOUSE_NUMBER ] );
160 $address->setCounty( $values[ Attribute::ADDRESS_COUNTY ] );
161 $address->setLatitude( $values[ Attribute::ADDRESS_LATITUDE ] );
162 $address->setLongitude( $values[ Attribute::ADDRESS_LONGITUDE ] );
163
164 return $address;
165 }
166
167 /**
168 * From post data to validated address both in frontend and backend.
169 *
170 * @param array $values Data from form.
171 *
172 * @return Entity\Address
173 */
174 public function toCarDeliveryAddress( array $values ): Entity\Address {
175 $address = $this->createAddress( $values );
176 $address->setHouseNumber( $values[ Attribute::ADDRESS_HOUSE_NUMBER ] );
177
178 return $address;
179 }
180
181 /**
182 * Creates new Address
183 *
184 * @param array $values Data from form.
185 *
186 * @return Entity\Address
187 */
188 private function createAddress( array $values ): Entity\Address {
189 return new Entity\Address(
190 $values[ Attribute::ADDRESS_STREET ],
191 $values[ Attribute::ADDRESS_CITY ],
192 $values[ Attribute::ADDRESS_POST_CODE ]
193 );
194 }
195 }
196