Skip to content

Commit

Permalink
Contact retrieval from Google Contacts API, basic Contact class and t…
Browse files Browse the repository at this point in the history
…est page for contact retrieval
  • Loading branch information
Jordan Hall committed Jul 27, 2015
1 parent 395f80c commit 411e2fe
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
43 changes: 43 additions & 0 deletions factories/ContactFactory.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,51 @@
<?php
namespace rapidweb\googlecontacts\factories;

use rapidweb\googlecontacts\helpers\GoogleHelper;
use rapidweb\googlecontacts\objects\Contact;

abstract class ContactFactory
{
public static function getAll()
{
$client = GoogleHelper::getClient();

$req = new \Google_Http_Request("https://www.google.com/m8/feeds/contacts/default/full?max-results=10000&updated-min=2007-03-16T00:00:00");

$val = $client->getAuth()->authenticatedRequest($req);

$response = $val->getResponseBody();

$xmlContacts = simplexml_load_string($response);
$xmlContacts->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');

foreach ($xmlContacts->entry as $xmlContactsEntry)
{
$contactDetails = array();

$contactDetails['id'] = (string) $xmlContactsEntry->id;
$contactDetails['name'] = (string) $xmlContactsEntry->title;

$contactGDNodes = $xmlContactsEntry->children('http://schemas.google.com/g/2005');

foreach($contactGDNodes as $key => $value) {

$attributes = $value->attributes();

if ($key=='email') {
$contactDetails[$key] = (string) $attributes['address'];
}
else {
$contactDetails[$key] = (string) $value;
}
}

$contactsArray[] = new Contact($contactDetails);
}

return $contactsArray;
}

public static function get()
{

Expand Down
6 changes: 4 additions & 2 deletions objects/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

class Contact
{
public function __construct()
public function __construct($contactDetails)
{

foreach($contactDetails as $key => $value) {
$this->$key = $value;
}
}
}
15 changes: 15 additions & 0 deletions test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

require_once 'vendor/autoload.php';

use rapidweb\googlecontacts\helpers\GoogleHelper;
use rapidweb\googlecontacts\factories\ContactFactory;

$contacts = ContactFactory::getAll();

if (count($contacts)) {
echo "Test retrieved ".count($contacts)." contacts.";
}
else {
echo "No contacts retrieved!";
}

0 comments on commit 411e2fe

Please sign in to comment.