| @@ -24,16 +24,16 @@ | ||
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | 26 | * Updates order entity from props to save. |
| 27 | 27 | * |
| 28 | - * @param Entity\Order $orderEntity Order entity. | |
| 29 | - * @param array $propsToSave Props to save. | |
| 28 | + * @param Entity\Order $orderEntity Order entity. | |
| 29 | + * @param array<string, string|float|int|true|null> $propsToSave Props to save. | |
| 30 | 30 | * |
| 31 | 31 | * @return Entity\PickupPoint |
| 32 | 32 | */ |
| 33 | 33 | public function toOrderEntityPickupPoint( Entity\Order $orderEntity, array $propsToSave ): Entity\PickupPoint { |
| 34 | 34 | $pickupPoint = $orderEntity->getPickupPoint(); |
| 35 | - if ( null === $pickupPoint ) { | |
| 35 | + if ( $pickupPoint === null ) { | |
| 36 | 36 | $pickupPoint = new Entity\PickupPoint(); |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | foreach ( $propsToSave as $attrName => $attrValue ) { |
| @@ -39,23 +39,29 @@ | ||
| 39 | 39 | foreach ( $propsToSave as $attrName => $attrValue ) { |
| 40 | 40 | switch ( $attrName ) { |
| 41 | 41 | case Attribute::POINT_ID: |
| 42 | 42 | $pickupPoint->setId( $attrValue ); |
| 43 | + | |
| 43 | 44 | break; |
| 44 | 45 | case Attribute::POINT_NAME: |
| 45 | 46 | $pickupPoint->setName( $attrValue ); |
| 47 | + | |
| 46 | 48 | break; |
| 47 | 49 | case Attribute::POINT_URL: |
| 48 | 50 | $pickupPoint->setUrl( $attrValue ); |
| 51 | + | |
| 49 | 52 | break; |
| 50 | 53 | case Attribute::POINT_STREET: |
| 51 | 54 | $pickupPoint->setStreet( $attrValue ); |
| 55 | + | |
| 52 | 56 | break; |
| 53 | 57 | case Attribute::POINT_ZIP: |
| 54 | 58 | $pickupPoint->setZip( $attrValue ); |
| 59 | + | |
| 55 | 60 | break; |
| 56 | 61 | case Attribute::POINT_CITY: |
| 57 | 62 | $pickupPoint->setCity( $attrValue ); |
| 63 | + | |
| 58 | 64 | break; |
| 59 | 65 | } |
| 60 | 66 | } |
| 61 | 67 | |
| @@ -72,50 +78,69 @@ | ||
| 72 | 78 | * @return void |
| 73 | 79 | * @throws WC_Data_Exception When shipping input is invalid. |
| 74 | 80 | */ |
| 75 | 81 | public function toWcOrderShippingAddress( WC_Order $wcOrder, string $attributeName, string $value ): void { |
| 76 | - if ( Attribute::POINT_STREET === $attributeName ) { | |
| 82 | + if ( $attributeName === Attribute::POINT_STREET ) { | |
| 77 | 83 | $wcOrder->set_shipping_address_1( $value ); |
| 78 | 84 | $wcOrder->set_shipping_address_2( '' ); |
| 79 | 85 | } |
| 80 | - if ( Attribute::POINT_PLACE === $attributeName ) { | |
| 86 | + if ( $attributeName === Attribute::POINT_PLACE ) { | |
| 81 | 87 | $wcOrder->set_shipping_company( $value ); |
| 82 | 88 | } |
| 83 | - if ( Attribute::POINT_CITY === $attributeName ) { | |
| 89 | + if ( $attributeName === Attribute::POINT_CITY ) { | |
| 84 | 90 | $wcOrder->set_shipping_city( $value ); |
| 85 | 91 | } |
| 86 | - if ( Attribute::POINT_ZIP === $attributeName ) { | |
| 92 | + if ( $attributeName === Attribute::POINT_ZIP ) { | |
| 87 | 93 | $wcOrder->set_shipping_postcode( $value ); |
| 88 | 94 | } |
| 89 | 95 | } |
| 90 | 96 | |
| 91 | 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 | + /** | |
| 92 | 117 | * From prepared properties to order Size. |
| 93 | 118 | * |
| 94 | - * @param Entity\Order $order Order. | |
| 95 | - * @param array $propsToSave Prepared properties. | |
| 119 | + * @param Entity\Order $order Order. | |
| 120 | + * @param array<string, string|float|int|true|null> $propsToSave Prepared properties. | |
| 96 | 121 | * |
| 97 | 122 | * @return Entity\Size |
| 98 | 123 | */ |
| 99 | 124 | public function toOrderSize( Entity\Order $order, array $propsToSave ): Entity\Size { |
| 100 | 125 | $orderSize = $order->getSize(); |
| 101 | - if ( null === $orderSize ) { | |
| 126 | + if ( $orderSize === null ) { | |
| 102 | 127 | $orderSize = new Entity\Size(); |
| 103 | 128 | } |
| 104 | 129 | |
| 105 | 130 | foreach ( $propsToSave as $attrName => $attrValue ) { |
| 106 | 131 | switch ( $attrName ) { |
| 107 | - case Metabox::FIELD_WEIGHT: | |
| 108 | - $order->setWeight( $attrValue ); | |
| 109 | - break; | |
| 110 | - case Metabox::FIELD_WIDTH: | |
| 132 | + case Form::FIELD_WIDTH: | |
| 111 | 133 | $orderSize->setWidth( $attrValue ); |
| 134 | + | |
| 112 | 135 | break; |
| 113 | - case Metabox::FIELD_LENGTH: | |
| 136 | + case Form::FIELD_LENGTH: | |
| 114 | 137 | $orderSize->setLength( $attrValue ); |
| 138 | + | |
| 115 | 139 | break; |
| 116 | - case Metabox::FIELD_HEIGHT: | |
| 140 | + case Form::FIELD_HEIGHT: | |
| 117 | 141 | $orderSize->setHeight( $attrValue ); |
| 142 | + | |
| 118 | 143 | break; |
| 119 | 144 | } |
| 120 | 145 | } |
| 121 | 146 | |
| @@ -129,13 +154,9 @@ | ||
| 129 | 154 | * |
| 130 | 155 | * @return Entity\Address |
| 131 | 156 | */ |
| 132 | 157 | 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 | - ); | |
| 158 | + $address = $this->createAddress( $values ); | |
| 138 | 159 | $address->setHouseNumber( $values[ Attribute::ADDRESS_HOUSE_NUMBER ] ); |
| 139 | 160 | $address->setCounty( $values[ Attribute::ADDRESS_COUNTY ] ); |
| 140 | 161 | $address->setLatitude( $values[ Attribute::ADDRESS_LATITUDE ] ); |
| 141 | 162 | $address->setLongitude( $values[ Attribute::ADDRESS_LONGITUDE ] ); |
| @@ -142,5 +163,33 @@ | ||
| 142 | 163 | |
| 143 | 164 | return $address; |
| 144 | 165 | } |
| 145 | 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 | + } | |
| 146 | 195 | } |