From 1e929f3b2a613764c30b3573ddfea6172e64f635 Mon Sep 17 00:00:00 2001 From: Greg Payne Date: Mon, 8 Sep 2014 13:37:40 -0500 Subject: [PATCH 1/7] KnpLabs/php-github-api#178 - adds a basic search api implementation. --- lib/Github/Api/Search.php | 38 ++++++++++++++++++++++++++++++++++++++ lib/Github/Client.php | 6 +++++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 lib/Github/Api/Search.php diff --git a/lib/Github/Api/Search.php b/lib/Github/Api/Search.php new file mode 100644 index 00000000000..4d3416a73fe --- /dev/null +++ b/lib/Github/Api/Search.php @@ -0,0 +1,38 @@ + + */ +class Search extends AbstractApi +{ + /** + * Search by filter (q) + * @link http://developer.github.com/v3/search/ + * + * @param string $type the search type + * @param string $q the filter + * @param string $sort the sort field + * @param string $order asc/desc + * + * @return array list of issues found + */ + public function find($type = 'issues', $q = '', $sort = 'updated', $order = 'desc') + { + if (!in_array($type, array('issues', 'repositories', 'code', 'users'))) { + $type = 'issues'; + } + return $this->get("/search/$type", array('q' => $q, 'sort' => $sort, 'order' => $order)); + } + +} diff --git a/lib/Github/Client.php b/lib/Github/Client.php index 6909264ed66..92e81254e56 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -162,6 +162,10 @@ public function api($name) $api = new Api\Repo($this); break; + case 'search': + $api = new Api\Search($this); + break; + case 'team': case 'teams': $api = new Api\Organization\Teams($this); @@ -310,7 +314,7 @@ public function getSupportedApiVersions() /** * @param string $name - * + * * @return ApiInterface * * @throws InvalidArgumentException From 3dd65b7bae223030c102b9ca985daa48e451c58a Mon Sep 17 00:00:00 2001 From: Greg Payne Date: Mon, 8 Sep 2014 15:00:59 -0500 Subject: [PATCH 2/7] Split find into 4 methods. --- lib/Github/Api/Search.php | 58 +++++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/lib/Github/Api/Search.php b/lib/Github/Api/Search.php index 4d3416a73fe..26ee09debed 100644 --- a/lib/Github/Api/Search.php +++ b/lib/Github/Api/Search.php @@ -16,23 +16,65 @@ */ class Search extends AbstractApi { + + /** + * Search repositories by filter (q) + * @link https://developer.github.com/v3/search/#search-repositories + * + * @param string $q the filter + * @param string $sort the sort field + * @param string $order asc/desc + * + * @return array list of repositories found + */ + public function repositories($q, $sort = 'updated', $order = 'desc') + { + return $this->get("/search/repositories", array('q' => $q, 'sort' => $sort, 'order' => $order)); + } + /** - * Search by filter (q) - * @link http://developer.github.com/v3/search/ + * Search issues by filter (q) + * @link https://developer.github.com/v3/search/#search-issues * - * @param string $type the search type * @param string $q the filter * @param string $sort the sort field * @param string $order asc/desc * * @return array list of issues found */ - public function find($type = 'issues', $q = '', $sort = 'updated', $order = 'desc') + public function issues($q, $sort = 'updated', $order = 'desc') + { + return $this->get("/search/issues", array('q' => $q, 'sort' => $sort, 'order' => $order)); + } + + /** + * Search code by filter (q) + * @link https://developer.github.com/v3/search/#search-code + * + * @param string $q the filter + * @param string $sort the sort field + * @param string $order asc/desc + * + * @return array list of code found + */ + public function code($q, $sort = 'updated', $order = 'desc') + { + return $this->get("/search/code", array('q' => $q, 'sort' => $sort, 'order' => $order)); + } + + /** + * Search users by filter (q) + * @link https://developer.github.com/v3/search/#search-users + * + * @param string $q the filter + * @param string $sort the sort field + * @param string $order asc/desc + * + * @return array list of users found + */ + public function users($q, $sort = 'updated', $order = 'desc') { - if (!in_array($type, array('issues', 'repositories', 'code', 'users'))) { - $type = 'issues'; - } - return $this->get("/search/$type", array('q' => $q, 'sort' => $sort, 'order' => $order)); + return $this->get("/search/users", array('q' => $q, 'sort' => $sort, 'order' => $order)); } } From 32ae587ee22e15dd89f20cd1a9607a504c780f47 Mon Sep 17 00:00:00 2001 From: Greg Payne Date: Mon, 8 Sep 2014 15:11:31 -0500 Subject: [PATCH 3/7] Added method phpdoc. --- lib/Github/Client.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Github/Client.php b/lib/Github/Client.php index 92e81254e56..c1779663858 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -31,6 +31,7 @@ * @method Api\Repo repos() * @method Api\Repo repository() * @method Api\Repo repositories() + * @method Api\Search search() * @method Api\Organization team() * @method Api\Organization teams() * @method Api\User user() From bb2b30e76a084df80b792f6d7134ea5018e8b3de Mon Sep 17 00:00:00 2001 From: Greg Payne Date: Tue, 23 Sep 2014 12:17:36 -0500 Subject: [PATCH 4/7] Changed to single quotes. --- lib/Github/Api/Search.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Github/Api/Search.php b/lib/Github/Api/Search.php index 26ee09debed..bf7dab21ce7 100644 --- a/lib/Github/Api/Search.php +++ b/lib/Github/Api/Search.php @@ -29,7 +29,7 @@ class Search extends AbstractApi */ public function repositories($q, $sort = 'updated', $order = 'desc') { - return $this->get("/search/repositories", array('q' => $q, 'sort' => $sort, 'order' => $order)); + return $this->get('/search/repositories', array('q' => $q, 'sort' => $sort, 'order' => $order)); } /** @@ -44,7 +44,7 @@ public function repositories($q, $sort = 'updated', $order = 'desc') */ public function issues($q, $sort = 'updated', $order = 'desc') { - return $this->get("/search/issues", array('q' => $q, 'sort' => $sort, 'order' => $order)); + return $this->get('/search/issues', array('q' => $q, 'sort' => $sort, 'order' => $order)); } /** @@ -59,7 +59,7 @@ public function issues($q, $sort = 'updated', $order = 'desc') */ public function code($q, $sort = 'updated', $order = 'desc') { - return $this->get("/search/code", array('q' => $q, 'sort' => $sort, 'order' => $order)); + return $this->get('/search/code', array('q' => $q, 'sort' => $sort, 'order' => $order)); } /** @@ -74,7 +74,7 @@ public function code($q, $sort = 'updated', $order = 'desc') */ public function users($q, $sort = 'updated', $order = 'desc') { - return $this->get("/search/users", array('q' => $q, 'sort' => $sort, 'order' => $order)); + return $this->get('/search/users', array('q' => $q, 'sort' => $sort, 'order' => $order)); } } From 5c1f9fc7f526d3f12ef8248e3f071b826e563347 Mon Sep 17 00:00:00 2001 From: Yevgen Kovalienia Date: Sat, 29 Nov 2014 22:39:50 +0100 Subject: [PATCH 5/7] KnpLabs/php-github-api#178: Added "Search" check to Github\ClientTest --- test/Github/Tests/ClientTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/Github/Tests/ClientTest.php b/test/Github/Tests/ClientTest.php index 04b3a710397..b7a8751fb39 100644 --- a/test/Github/Tests/ClientTest.php +++ b/test/Github/Tests/ClientTest.php @@ -186,6 +186,8 @@ public function getApiClassesProvider() array('repository', 'Github\Api\Repo'), array('repositories', 'Github\Api\Repo'), + array('search', 'Github\Api\Search'), + array('pr', 'Github\Api\PullRequest'), array('pullRequest', 'Github\Api\PullRequest'), array('pull_request', 'Github\Api\PullRequest'), From 164c142f224b1659b5169e44448f6af157bbd094 Mon Sep 17 00:00:00 2001 From: Yevgen Kovalienia Date: Sat, 29 Nov 2014 22:59:36 +0100 Subject: [PATCH 6/7] KnpLabs/php-github-api#178: Added tests for Github\Api\Search --- test/Github/Tests/Api/SearchTest.php | 183 +++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 test/Github/Tests/Api/SearchTest.php diff --git a/test/Github/Tests/Api/SearchTest.php b/test/Github/Tests/Api/SearchTest.php new file mode 100644 index 00000000000..d5fe9b08494 --- /dev/null +++ b/test/Github/Tests/Api/SearchTest.php @@ -0,0 +1,183 @@ + '0')); + + $api = $this->getApiMock(); + + $api->expects($this->once()) + ->method('get') + ->with( + '/search/repositories', + array('q' => 'query text', 'sort' => 'updated', 'order' => 'desc') + ) + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->repositories('query text')); + } + + /** + * @test + */ + public function shouldSearchRepositoriesRegardingSortAndOrder() + { + $expectedArray = array(array('total_count' => '0')); + + $api = $this->getApiMock(); + + $api->expects($this->once()) + ->method('get') + ->with( + '/search/repositories', + array('q' => 'query text', 'sort' => 'created', 'order' => 'asc') + ) + ->will($this->returnValue($expectedArray)); + + $this->assertEquals( + $expectedArray, + $api->repositories('query text', 'created', 'asc') + ); + } + + /** + * @test + */ + public function shouldSearchIssuesByQuery() + { + $expectedArray = array(array('total_count' => '0')); + + $api = $this->getApiMock(); + + $api->expects($this->once()) + ->method('get') + ->with( + '/search/issues', + array('q' => 'query text', 'sort' => 'updated', 'order' => 'desc') + ) + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->issues('query text')); + } + + /** + * @test + */ + public function shouldSearchIssuesRegardingSortAndOrder() + { + $expectedArray = array(array('total_count' => '0')); + + $api = $this->getApiMock(); + + $api->expects($this->once()) + ->method('get') + ->with( + '/search/issues', + array('q' => 'query text', 'sort' => 'created', 'order' => 'asc') + ) + ->will($this->returnValue($expectedArray)); + + $this->assertEquals( + $expectedArray, + $api->issues('query text', 'created', 'asc') + ); + } + + /** + * @test + */ + public function shouldSearchCodeByQuery() + { + $expectedArray = array(array('total_count' => '0')); + + $api = $this->getApiMock(); + + $api->expects($this->once()) + ->method('get') + ->with( + '/search/code', + array('q' => 'query text', 'sort' => 'updated', 'order' => 'desc') + ) + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->code('query text')); + } + + /** + * @test + */ + public function shouldSearchCodeRegardingSortAndOrder() + { + $expectedArray = array(array('total_count' => '0')); + + $api = $this->getApiMock(); + + $api->expects($this->once()) + ->method('get') + ->with( + '/search/code', + array('q' => 'query text', 'sort' => 'created', 'order' => 'asc') + ) + ->will($this->returnValue($expectedArray)); + + $this->assertEquals( + $expectedArray, + $api->code('query text', 'created', 'asc') + ); + } + + /** + * @test + */ + public function shouldSearchUsersByQuery() + { + $expectedArray = array(array('total_count' => '0')); + + $api = $this->getApiMock(); + + $api->expects($this->once()) + ->method('get') + ->with( + '/search/users', + array('q' => 'query text', 'sort' => 'updated', 'order' => 'desc') + ) + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->users('query text')); + } + + /** + * @test + */ + public function shouldSearchUsersRegardingSortAndOrder() + { + $expectedArray = array(array('total_count' => '0')); + + $api = $this->getApiMock(); + + $api->expects($this->once()) + ->method('get') + ->with( + '/search/users', + array('q' => 'query text', 'sort' => 'created', 'order' => 'asc') + ) + ->will($this->returnValue($expectedArray)); + + $this->assertEquals( + $expectedArray, + $api->users('query text', 'created', 'asc') + ); + } + + protected function getApiClass() + { + return 'Github\Api\Search'; + } +} From a7e394687c0acc49e653b02ab188b44cf44abbb1 Mon Sep 17 00:00:00 2001 From: Yevgen Kovalienia Date: Sun, 30 Nov 2014 13:04:57 +0100 Subject: [PATCH 7/7] KnpLabs/php-github-api#178: Documentation added for Github\Client\Search --- doc/search.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 doc/search.md diff --git a/doc/search.md b/doc/search.md new file mode 100644 index 00000000000..85936170162 --- /dev/null +++ b/doc/search.md @@ -0,0 +1,48 @@ +## Search API +[Back to the navigation](index.md) + +Searching repositories, code, issues and users. +Wrap [GitHub Search API](http://developer.github.com/v3/search/). All methods are described on that page. + +### Search repositories + +```php +$repos = $client->api('search')->repositories('github language:php'); +``` + +Returns a list of repositories found by such criteria. + +### Search code + +```php +$repos = $client->api('search')->code('@todo language:php'); +``` + +Returns a list of files found by such criteria (containing "@todo" and language==php). + +### Search issues + +```php +$repos = $client->api('search')->issues('bug language:php'); +``` + +Returns a list of issues found by such criteria. + +### Search users + +```php +$repos = $client->api('search')->users('location:Amsterdam language:php'); +``` + +Returns a list of users found by such criteria. + +### Sorting results + +You can sort results using 2-3 arguments. + +```php +$repos = $client->api('repo')->repositories('...', 'created', 'asc'); +$repos = $client->api('repo')->code('...........', 'indexed', 'desc'); +$repos = $client->api('repo')->issues('.........', 'comments', 'asc'); +$repos = $client->api('repo')->users('..........', 'followers', 'asc'); +```