/** * `create()` function should add Contact to end of stack. If array is given * it should be internally coonverted to Contact object */ public function testCreateFromArray() { $contact1 = array('FirstName' => 'FirstName1', 'Email' => '*****@*****.**'); $contact2 = array('FirstName' => 'FirstName2', 'Email' => '*****@*****.**'); $c = new ContactCollection(array($contact1, $contact2)); $expected = $c->count(); $this->assertEquals($expected, 2); }
public function push(ContactCollection $collection) { $isdk = $this->getIsdk(); $count = $collection->count(); $result = array('skip' => new ContactCollection(), 'create' => new ContactCollection(), 'update' => new ContactCollection(), 'fail' => new ContactCollection()); $contacts = $collection->read(); switch ($count) { case 0: return $result; break; /** * If Collection has 1 contact we don't want to pull all contacts from * remote API. */ /** * If Collection has 1 contact we don't want to pull all contacts from * remote API. */ case 1: $contact = reset($contacts); $id = $contact->getId(); if ($id) { // Contact Id is set. Most likely this is update $c = $this->updateContact($contact); } else { // Contact ID not set. Most likely this is new contact $c = $this->createContact($contact); } if ($c->isCreated()) { $result['create']->create($c); } elseif ($c->isUpdated()) { $result['update']->create($c); } elseif ($c->isSkipped()) { $result['skip']->create($c); } elseif ($c->isFailed()) { $result['fail']->create($c); } break; default: foreach ($contacts as $contact) { $id = $contact->getId(); if ($id) { // Contact Id is set. Most likely this is update $c = $this->updateContact($contact); } else { // Contact ID not set. Most likely this is new contact $c = $this->createContact($contact); } if ($c->isCreated()) { $result['create']->create($c); } elseif ($c->isUpdated()) { $result['update']->create($c); } elseif ($c->isSkipped()) { $result['skip']->create($c); } elseif ($c->isFailed()) { $result['fail']->create($c); } } } return $result; }