Skip to content

Commit

Permalink
Parse full ID and add to render.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwagroves committed Sep 2, 2018
1 parent b4c7397 commit a97e490
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,13 @@ public function render()
$entry = $this->createEntryElement();
$dom->appendChild($entry);

// Add ID.
if (isset($this->id)) {
$id = $dom->createElement('id');
$id->nodeValue = $this->id;
$entry->appendChild($id);
}

// Add name.
if (isset($this->name)) {
$this->name->createAndAppendDomElement($entry);
Expand Down
12 changes: 5 additions & 7 deletions src/ContactFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public function createFromDomElement(\DOMElement $element)
$contact = new Contact();

// ID.
$id = $element->getElementsByTagName('id');
if ($id->length) {
$contact->setId($id[0]->nodeValue);
}

$links = $element->getElementsByTagName('link');
if ($len = $links->length) {
for ($i=0; $i < $len; $i++) {
Expand All @@ -65,13 +70,6 @@ public function createFromDomElement(\DOMElement $element)

if ($rel === 'self') {
$contact->setLinkSelf($href);

// Extract ID.
$pattern = '/\/([a-z0-9]+)$/';
if (preg_match($pattern, $href, $matches)) {
$contact->setId($matches[1]);
}

continue;
}

Expand Down
7 changes: 5 additions & 2 deletions tests/ContactFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testCreateFromXmlString()

$this->assertInstanceOf(Contact::class, $contact);

$this->assertEquals('623c70ec0f21715e', $contact->getId());
$this->assertEquals('http://www.google.com/m8/feeds/contacts/example.com/base/623c70ec0f21715e', $contact->getId());
$this->assertEquals('W/"A0EEQHY6fSt7I2A9XBVWGEs."', $contact->getEtag());
$this->assertEquals('2018-09-01T07:37:20.038Z', $contact->getUpdated());
$this->assertEquals('https://www.google.com/m8/feeds/contacts/example.com/full/623c70ec0f21715e', $contact->getLinkSelf());
Expand Down Expand Up @@ -62,6 +62,9 @@ public function testCreateFromXmlString()
$this->assertEquals('Writes documentation', $contact->organization(0)->getJobDescription());
$this->assertEquals('Software Development', $contact->organization(0)->getDepartment());
$this->assertEquals('GOOG', $contact->organization(0)->getSymbol());

// Test render.
$xml = $contact->render();
}

public function testCreateFromFeedXML()
Expand All @@ -72,6 +75,6 @@ public function testCreateFromFeedXML()

$this->assertEquals('array', gettype($contacts));
$this->assertEquals(1, count($contacts));
$this->assertEquals('623c70ec0f21715e', $contacts[0]->getId());
$this->assertEquals('http://www.google.com/m8/feeds/contacts/example.com/base/623c70ec0f21715e', $contacts[0]->getId());
}
}

0 comments on commit a97e490

Please sign in to comment.