Skip to content

Commit 2879f87

Browse files
committed
Add searchEmail method in github-api
1 parent 98069be commit 2879f87

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

lib/Github/Api/User.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ public function search($username)
2424
return $response['users'];
2525
}
2626

27+
public function searchEmail($email)
28+
{
29+
$response = $this->get('user/email/'.urlencode($email), array(), array('format'=>'xml'));
30+
31+
return $response;
32+
}
33+
2734
/**
2835
* Get extended information about a user by its username
2936
* http://develop.github.com/p/users.html#getting_user_information

lib/Github/HttpClient.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,27 @@ protected function decodeResponse($response, array $options)
121121
return $response;
122122
} elseif ('json' === $options['format']) {
123123
return json_decode($response, true);
124+
} elseif ('xml' === $options['format']) {
125+
$xml = new SimpleXMLElement($response);
126+
return $this->xmlToArray($xml);
124127
}
125128

126129
throw new Exception(__CLASS__.' only supports json & text format, '.$options['format'].' given.');
127130
}
128131

132+
private function xmlToArray($xml)
133+
{
134+
$array = json_decode(json_encode($xml), TRUE);
135+
foreach (array_slice($array, 0) as $key => $value) {
136+
if (empty($value)) {
137+
$array[$key] = NULL;
138+
} elseif (is_array($value)) {
139+
$array[$key] = $this->xmlToArray($value);
140+
}
141+
}
142+
return $array;
143+
}
144+
129145
/**
130146
* Change an option value.
131147
*

modules/service_github_api.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,22 @@ public function get_repo_info($url) {
144144
);
145145
return $repository;
146146
}
147+
148+
public function get_user($email) {
149+
150+
try {
151+
$user = $this->_client->getUserApi()->searchEmail($email);
152+
} catch (Exception $e) {
153+
$user = null;
154+
}
155+
return $user;
156+
}
157+
158+
public function get_user_link($email) {
159+
160+
$user = $this->get_user($email);
161+
if ($user) {
162+
return $this->_web_root . '/' . $user['login'];
163+
}
164+
}
147165
}

0 commit comments

Comments
 (0)