Skip to content

Commit

Permalink
Merge pull request #180 from jbrooksuk/magic-api
Browse files Browse the repository at this point in the history
Closes #177 - use the magic __call method for IDE completion
  • Loading branch information
cursedcoder committed Aug 21, 2014
2 parents 4c5fe77 + b84ee93 commit 414457e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lib/Github/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,41 @@

use Github\Api\ApiInterface;
use Github\Exception\InvalidArgumentException;
use Github\Exception\BadMethodCallException;
use Github\HttpClient\HttpClient;
use Github\HttpClient\HttpClientInterface;

/**
* Simple yet very cool PHP GitHub client
*
* @method Api\CurrentUser currentUser()
* @method Api\CurrentUser me()
* @method Api\Enterprise ent()
* @method Api\Enterprise enterprise()
* @method Api\GitData git()
* @method Api\GitData gitData()
* @method Api\Gists gist()
* @method Api\Gists gists()
* @method Api\Issue issue()
* @method Api\Issue issues()
* @method Api\Markdown markdown()
* @method Api\Organization organization()
* @method Api\Organization organizations()
* @method Api\PullRequest pr()
* @method Api\PullRequest pullRequest()
* @method Api\PullRequest pullRequests()
* @method Api\Repo repo()
* @method Api\Repo repos()
* @method Api\Repo repository()
* @method Api\Repo repositories()
* @method Api\Organization team()
* @method Api\Organization teams()
* @method Api\User user()
* @method Api\User users()
* @method Api\Authorizations authorization()
* @method Api\Authorizations authorizations()
* @method Api\Meta meta()
*
* @author Joseph Bielawski <stloyd@gmail.com>
*
* Website: http://github.com/KnpLabs/php-github-api
Expand Down Expand Up @@ -84,6 +113,7 @@ public function api($name)
switch ($name) {
case 'me':
case 'current_user':
case 'currentUser':
$api = new Api\CurrentUser($this);
break;

Expand All @@ -94,6 +124,7 @@ public function api($name)

case 'git':
case 'git_data':
case 'gitData':
$api = new Api\GitData($this);
break;

Expand All @@ -117,7 +148,9 @@ public function api($name)
break;

case 'pr':
case 'pullRequest':
case 'pull_request':
case 'pullRequests':
case 'pull_requests':
$api = new Api\PullRequest($this);
break;
Expand Down Expand Up @@ -274,4 +307,19 @@ public function getSupportedApiVersions()
{
return array('v3', 'beta');
}

/**
* @param string $name
*
* @return ApiInterface
*
* @throws InvalidArgumentException
*/
public function __call($name, $args) {
try {
return $this->api($name);
} catch (InvalidArgumentException $e) {
throw new BadMethodCallException(sprintf('Undefined method called: "%s"', $name));
}
}
}
13 changes: 13 additions & 0 deletions lib/Github/Exception/BadMethodCallException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Github\Exception;

/**
* BadMethodCallException
*
* @author James Brooks <jbrooksuk@me.com>
*/
class BadMethodCallException extends \BadMethodCallException implements ExceptionInterface
{

}
26 changes: 26 additions & 0 deletions test/Github/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Github\Client;
use Github\Exception\InvalidArgumentException;
use Github\Exception\BadMethodCallException;

class ClientTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -124,6 +125,17 @@ public function shouldGetApiInstance($apiName, $class)
$this->assertInstanceOf($class, $client->api($apiName));
}

/**
* @test
* @dataProvider getApiClassesProvider
*/
public function shouldGetMagicApiInstance($apiName, $class)
{
$client = new Client();

$this->assertInstanceOf($class, $client->$apiName());
}

/**
* @test
* @expectedException InvalidArgumentException
Expand All @@ -134,6 +146,16 @@ public function shouldNotGetApiInstance()
$client->api('do_not_exist');
}

/**
* @test
* @expectedException BadMethodCallException
*/
public function shouldNotGetMagicApiInstance()
{
$client = new Client();
$client->doNotExist();
}

public function getApiClassesProvider()
{
return array(
Expand All @@ -142,9 +164,11 @@ public function getApiClassesProvider()

array('me', 'Github\Api\CurrentUser'),
array('current_user', 'Github\Api\CurrentUser'),
array('currentUser', 'Github\Api\CurrentUser'),

array('git', 'Github\Api\GitData'),
array('git_data', 'Github\Api\GitData'),
array('gitData', 'Github\Api\GitData'),

array('gist', 'Github\Api\Gists'),
array('gists', 'Github\Api\Gists'),
Expand All @@ -163,7 +187,9 @@ public function getApiClassesProvider()
array('repositories', 'Github\Api\Repo'),

array('pr', 'Github\Api\PullRequest'),
array('pullRequest', 'Github\Api\PullRequest'),
array('pull_request', 'Github\Api\PullRequest'),
array('pullRequests', 'Github\Api\PullRequest'),
array('pull_requests', 'Github\Api\PullRequest'),

array('authorization', 'Github\Api\Authorizations'),
Expand Down

0 comments on commit 414457e

Please sign in to comment.