# packeta/1.6.3/src/Packetery/Module/Order/AttributeMapper.php

Packeta, version 1.6.3. 147 lines.

- Page: https://pluginprobe.com/plugins/packeta/1.6.3/code/src/Packetery/Module/Order/AttributeMapper.php
- Raw: https://pluginprobe.com/plugins/packeta/1.6.3/raw/src/Packetery/Module/Order/AttributeMapper.php
- Modified: 2023-08-30T08:36:18+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/packeta/1.6.3/code/src/Packetery/Module/Order/AttributeMapper.php#L10-L20`.

```php
<?php
/**
 * Class AttributeMapper.
 *
 * @package Packetery
 */

declare( strict_types=1 );

namespace Packetery\Module\Order;

use Packetery\Core\Entity;
use WC_Data_Exception;
use WC_Order;

/**
 * Class AttributeMapper.
 *
 * @package Packetery
 */
class AttributeMapper {

	// Business name of pickup point.

	/**
	 * Updates order entity from props to save.
	 *
	 * @param Entity\Order $orderEntity Order entity.
	 * @param array        $propsToSave Props to save.
	 *
	 * @return Entity\PickupPoint
	 */
	public function toOrderEntityPickupPoint( Entity\Order $orderEntity, array $propsToSave ): Entity\PickupPoint {
		$pickupPoint = $orderEntity->getPickupPoint();
		if ( null === $pickupPoint ) {
			$pickupPoint = new Entity\PickupPoint();
		}

		foreach ( $propsToSave as $attrName => $attrValue ) {
			switch ( $attrName ) {
				case Attribute::POINT_ID:
					$pickupPoint->setId( $attrValue );
					break;
				case Attribute::POINT_NAME:
					$pickupPoint->setName( $attrValue );
					break;
				case Attribute::POINT_URL:
					$pickupPoint->setUrl( $attrValue );
					break;
				case Attribute::POINT_STREET:
					$pickupPoint->setStreet( $attrValue );
					break;
				case Attribute::POINT_ZIP:
					$pickupPoint->setZip( $attrValue );
					break;
				case Attribute::POINT_CITY:
					$pickupPoint->setCity( $attrValue );
					break;
			}
		}

		return $pickupPoint;
	}

	/**
	 * Update order shipping.
	 *
	 * @param WC_Order $wcOrder       WC Order.
	 * @param string   $attributeName Attribute name.
	 * @param string   $value         Value.
	 *
	 * @return void
	 * @throws WC_Data_Exception When shipping input is invalid.
	 */
	public function toWcOrderShippingAddress( WC_Order $wcOrder, string $attributeName, string $value ): void {
		if ( Attribute::POINT_STREET === $attributeName ) {
			$wcOrder->set_shipping_address_1( $value );
			$wcOrder->set_shipping_address_2( '' );
		}
		if ( Attribute::POINT_PLACE === $attributeName ) {
			$wcOrder->set_shipping_company( $value );
		}
		if ( Attribute::POINT_CITY === $attributeName ) {
			$wcOrder->set_shipping_city( $value );
		}
		if ( Attribute::POINT_ZIP === $attributeName ) {
			$wcOrder->set_shipping_postcode( $value );
		}
	}

	/**
	 * From prepared properties to order Size.
	 *
	 * @param Entity\Order $order       Order.
	 * @param array        $propsToSave Prepared properties.
	 *
	 * @return Entity\Size
	 */
	public function toOrderSize( Entity\Order $order, array $propsToSave ): Entity\Size {
		$orderSize = $order->getSize();
		if ( null === $orderSize ) {
			$orderSize = new Entity\Size();
		}

		foreach ( $propsToSave as $attrName => $attrValue ) {
			switch ( $attrName ) {
				case Metabox::FIELD_WEIGHT:
					$order->setWeight( $attrValue );
					break;
				case Metabox::FIELD_WIDTH:
					$orderSize->setWidth( $attrValue );
					break;
				case Metabox::FIELD_LENGTH:
					$orderSize->setLength( $attrValue );
					break;
				case Metabox::FIELD_HEIGHT:
					$orderSize->setHeight( $attrValue );
					break;
			}
		}

		return $orderSize;
	}

	/**
	 * From post data to validated address both in frontend and backend.
	 *
	 * @param array $values Data from form.
	 *
	 * @return Entity\Address
	 */
	public function toValidatedAddress( array $values ): Entity\Address {
		$address = new Entity\Address(
			$values[ Attribute::ADDRESS_STREET ],
			$values[ Attribute::ADDRESS_CITY ],
			$values[ Attribute::ADDRESS_POST_CODE ]
		);
		$address->setHouseNumber( $values[ Attribute::ADDRESS_HOUSE_NUMBER ] );
		$address->setCounty( $values[ Attribute::ADDRESS_COUNTY ] );
		$address->setLatitude( $values[ Attribute::ADDRESS_LATITUDE ] );
		$address->setLongitude( $values[ Attribute::ADDRESS_LONGITUDE ] );

		return $address;
	}

}

```
