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