/** * There can be only one primary entity * * @param Contact $entity */ public function updatePrimaryEntities(Contact $entity) { // update addresses $addresses = $entity->getAddresses(); $primaryAddress = $this->getPrimaryEntity($addresses); if ($primaryAddress) { $entity->setPrimaryAddress($primaryAddress); } elseif ($addresses->count() > 0) { $entity->setPrimaryAddress($addresses->first()); } // update emails $emails = $entity->getEmails(); $primaryEmail = $this->getPrimaryEntity($emails); if ($primaryEmail) { $entity->setPrimaryEmail($primaryEmail); } elseif ($emails->count() > 0) { $entity->setPrimaryEmail($emails->first()); } // update phones $phones = $entity->getPhones(); $primaryPhone = $this->getPrimaryEntity($phones); if ($primaryPhone) { $entity->setPrimaryPhone($primaryPhone); } elseif ($phones->count() > 0) { $entity->setPrimaryPhone($phones->first()); } }
public function testGetPrimaryPhone() { $contact = new Contact(); $this->assertNull($contact->getPrimaryPhone()); $phone = new ContactPhone('06001122334455'); $contact->addPhone($phone); $this->assertNull($contact->getPrimaryPhone()); $contact->setPrimaryPhone($phone); $this->assertSame($phone, $contact->getPrimaryPhone()); $phone2 = new ContactPhone('22001122334455'); $contact->addPhone($phone2); $contact->setPrimaryPhone($phone2); $this->assertSame($phone2, $contact->getPrimaryPhone()); $this->assertFalse($phone->isPrimary()); }
/** * @param Contact $entity * @return Contact */ protected function afterProcessEntity($entity) { // there can be only one primary entity $addresses = $entity->getAddresses(); $primaryAddress = $this->getPrimaryEntity($addresses); if ($primaryAddress) { /** @var ContactAddress $primaryAddress */ $entity->setPrimaryAddress($primaryAddress); } elseif ($addresses->count() > 0) { $entity->setPrimaryAddress($addresses->first()); } $emails = $entity->getEmails(); $primaryEmail = $this->getPrimaryEntity($emails); if ($primaryEmail) { /** @var ContactEmail $primaryEmail */ $entity->setPrimaryEmail($primaryEmail); } elseif ($emails->count() > 0) { $entity->setPrimaryEmail($emails->first()); } $phones = $entity->getPhones(); $primaryPhone = $this->getPrimaryEntity($phones); if ($primaryPhone) { /** @var ContactPhone $primaryPhone */ $entity->setPrimaryPhone($primaryPhone); } elseif ($phones->count() > 0) { $entity->setPrimaryPhone($phones->first()); } return $entity; }