Skip to content

Commit

Permalink
Merge pull request #29 from webarchitect609/fix/update_contact
Browse files Browse the repository at this point in the history
Fix updateContact method according to API manual
  • Loading branch information
tim-bezhashvyly authored Nov 29, 2016
2 parents 6615e4f + 9198836 commit 5783c78
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/Snowcap/Emarsys/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,7 @@ public function getConditions()
*/
public function createContact(array $data)
{
if (isset($data['contacts']) && is_array($data['contacts'])){
foreach($data['contacts'] as &$contact){
$contact = $this->mapFieldsToIds($contact);
}
}
$data = $this->mapFieldsForMultipleContacts($data);

return $this->send(HttpClient::POST, 'contact', $this->mapFieldsToIds($data));
}
Expand All @@ -254,15 +250,25 @@ public function createContact(array $data)
*/
public function updateContact(array $data)
{
if (isset($data['contacts']) && is_array($data['contacts'])){
foreach($data['contacts'] as &$contact){
$contact = $this->mapFieldsToIds($contact);
}
}
$data = $this->mapFieldsForMultipleContacts($data);

return $this->send(HttpClient::PUT, 'contact', $this->mapFieldsToIds($data));
}

/**
* Updates one or more contacts/recipients, identified by an external ID. If the contact does not exist in the
* database, it is created.
*
* @param array $data
* @return Response
*/
public function updateContactAndCreateIfNotExists(array $data)
{
$data = $this->mapFieldsForMultipleContacts($data);

return $this->send(HttpClient::PUT, 'contact/?create_if_not_exists=1', $this->mapFieldsToIds($data));
}

/**
* Deletes a single contact/recipient, identified by an external ID.
*
Expand Down Expand Up @@ -860,4 +866,18 @@ private function castIniFileValues($data)

return $data;
}

/**
* @param array $data
* @return array
*/
private function mapFieldsForMultipleContacts(array $data)
{
if (!isset($data['contacts']) || !is_array($data['contacts'])) {
return $data;
}

return array_merge($data, ['contacts' => array_map([$this, 'mapFieldsToIds'], $data['contacts'])]);
}

}

0 comments on commit 5783c78

Please sign in to comment.