setData() public method

Create a new address if the existing address has changed, or is not yet created.
public setData ( Order $order, array $data )
$order Order order to get addresses from
$data array data to set
 public function setData(Order $order, array $data)
 {
     if (!isset($data[$this->addresstype . 'ToSameAddress'])) {
         parent::setData($order, $data);
     } else {
         $order->{$this->addresstype . "AddressID"} = $order->{$this->useAddressType . "AddressID"};
         if (!$order->BillingAddressID) {
             $order->BillingAddressID = $order->{$this->useAddressType . "AddressID"};
         }
     }
     // FIX for missing name fields
     $fields = array_intersect_key($data, array_flip(['FirstName', 'Surname', 'Name', 'Email']));
     $changed = false;
     foreach ($fields as $field => $value) {
         if (!$order->{$this->addresstype . "Address"}->{$field}) {
             $order->{$this->addresstype . "Address"}->{$field} = $value;
             $changed = true;
         }
     }
     if ($changed) {
         $order->{$this->addresstype . "Address"}->write();
     }
 }
 public function setData(Order $order, array $data)
 {
     if (!isset($data['Use'])) {
         parent::setData($order, $data);
         return;
     }
     $member = Member::currentUser() ?: $order->Member();
     if (strpos($data['Use'], 'address-') === 0) {
         $address = Address::get()->byID(substr($data['Use'], 8));
         if (!$address) {
             throw new ValidationException('That address does not exist');
         }
         if (isset($data[$data['Use']]) && isset($data[$data['Use']]['saveAsNew']) && $data[$data['Use']]['saveAsNew']) {
             if (!$address->canCreate()) {
                 throw new ValidationException('You do not have permission to add a new address');
             }
             $address = $address->duplicate();
         } else {
             if (isset($data[$data['Use']]) && !$address->canEdit() && $address->MemberID != $member->ID) {
                 throw new ValidationException('You do not have permission to use this address');
             }
         }
     } else {
         if (!singleton('Address')->canCreate()) {
             throw new ValidationException('You do not have permission to add a new address');
         }
         $address = Address::create();
     }
     if (isset($data[$data['Use']])) {
         $address->castedUpdate($data[$data['Use']]);
         // FIX for missing name fields
         $fields = array_intersect_key($data, array_flip(['FirstName', 'Surname', 'Name', 'Email']));
         foreach ($fields as $field => $value) {
             if (!$address->{$field}) {
                 $address->{$field} = $value;
             }
         }
         if ($country = ShopConfig::current()->SingleCountry) {
             $address->Country = $country;
         }
         if ($this->addtoaddressbook) {
             $address->MemberID = $member->ID;
         }
         $address->write();
         if (isset($data[$data['Use']]['useAsDefaultShipping']) && $data[$data['Use']]['useAsDefaultShipping']) {
             $member->DefaultShippingAddressID = $address->ID;
         }
         if (isset($data[$data['Use']]['useAsDefaultBilling']) && $data[$data['Use']]['useAsDefaultBilling']) {
             $member->DefaultBillingAddressID = $address->ID;
         }
         if ($member->isChanged()) {
             $member->write();
         }
     }
     if ($address->exists()) {
         if ($this->addresstype === 'Shipping') {
             ShopUserInfo::singleton()->setAddress($address);
             Zone::cache_zone_ids($address);
         }
         $order->{$this->addresstype . 'AddressID'} = $address->ID;
         if (!$order->BillingAddressID) {
             $order->BillingAddressID = $address->ID;
         }
         $order->write();
         $order->extend('onSet' . $this->addresstype . 'Address', $address);
     }
 }
 /**
  * Create a new address if the existing address has changed, or is not yet
  * created.
  *
  * @param Order $order order to get addresses from
  * @param array $data  data to set
  */
 public function setData(Order $order, array $data)
 {
     $existingID = !empty($data[$this->addresstype . "AddressID"]) ? (int) $data[$this->addresstype . "AddressID"] : 0;
     if ($existingID > 0) {
         $order->{$this->addresstype . "AddressID"} = $existingID;
         $order->write();
         $order->extend('onSet' . $this->addresstype . 'Address', $address);
     } else {
         parent::setData($order, $data);
     }
 }