OEModule\PASAPI\resources\Patient::mapAddresses PHP Method

mapAddresses() private method

It may be useful to abstract this to a helper class or for it to be a static method on the Address resource ... if we wind up dooing more API importing.
private mapAddresses ( Contact $contact )
$contact Contact
    private function mapAddresses(\Contact $contact)
    {
        if (property_exists($this, 'AddressList')) {
            $matched_address_ids = array();
            foreach ($this->AddressList as $idx => $address_resource) {
                $matched_clause = $matched_address_ids ? ' AND id NOT IN (' . implode(',', $matched_address_ids) . ')' : '';
                $address_model = \Address::model()->find(array('condition' => "contact_id = :contact_id AND REPLACE(postcode,' ','') = :postcode" . $matched_clause, 'params' => array(':contact_id' => $contact->id, ':postcode' => str_replace(' ', '', $address_resource->Postcode))));
                if (!$address_model) {
                    $address_model = new \Address();
                    $address_model->contact_id = $contact->id;
                }
                if ($address_resource->saveModel($address_model)) {
                    $matched_address_ids[] = $address_model->id;
                    foreach ($address_resource->warnings as $warn) {
                        $this->addWarning("Address {$idx}: {$warn}");
                    }
                } else {
                    $this->addWarning("Address {$idx} not added");
                    foreach ($address_resource->errors as $err) {
                        $this->addWarning("Address {$idx}: {$err}");
                    }
                }
            }
            // clear out any addresses not matched
            $this->deleteAddresses($contact, $matched_address_ids);
        } else {
            if (!$this->partial_record) {
                $this->deleteAddresses($contact);
            }
        }
    }