Skip to content

Commit 09b4523

Browse files
committed
Merge pull request #36 from winzou/show-content
Add a showContents() method to Contents API
2 parents 9fe2848 + bb961f0 commit 09b4523

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

lib/Github/Api/Repository/Contents.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Github\Api\AbstractApi;
66
use Github\Exception\MissingArgumentException;
7+
use Github\Exception\InvalidArgumentException;
8+
use Github\Exception\ErrorException;
79

810
/**
911
* @link http://developer.github.com/v3/repos/contents/
@@ -72,4 +74,38 @@ public function archive($username, $repository, $format, $reference = null)
7274
'ref' => $reference
7375
));
7476
}
77+
78+
/**
79+
* Get the contents of a file in a repository
80+
*
81+
* @param string $username the user who owns the repository
82+
* @param string $repository the name of the repository
83+
* @param string $path path to file
84+
* @param string $reference reference to a branch or commit
85+
*
86+
* @return string|null content of file, or null in case of base64_decode failure
87+
*
88+
* @throws InvalidArgumentException If $path is not a file or if its encoding is different from base64
89+
* @throws ErrorException If $path doesn't include a 'content' index
90+
*/
91+
public function download($username, $repository, $path, $reference = null)
92+
{
93+
$file = $this->show($username, $repository, $path, $reference);
94+
95+
if (!isset($file['type']) || 'file' !== $file['type']) {
96+
throw new InvalidArgumentException(sprintf('Path "%s" is not a file.', $path));
97+
}
98+
99+
if (!isset($file['content'])) {
100+
throw new ErrorException(sprintf('Unable to access "content" for file "%s" (possible keys: "%s").', $path, implode(', ', array_keys($file))));
101+
}
102+
103+
if (!isset($file['encoding'])) {
104+
throw new InvalidArgumentException(sprintf('Can\t decode contents of file "%s" as no encoding is defined.', $path));
105+
} elseif ('base64' !== $file['encoding']) {
106+
throw new InvalidArgumentException(sprintf('Encoding "%s" of file "%s" is not supported.', $file['encoding'], $path));
107+
}
108+
109+
return base64_decode($file['content']) ?: null;
110+
}
75111
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
return json_decode(
4+
'{"sha":"5639a4e6ede372d7ea25b1064be4e2045c2a053d","size":2648,"name":"ContentsTest.php","path":"test\/Github\/Tests\/Api\/Repository\/ContentsTest.php","type":"file","url":"https:\/\/api.github.com\/repos\/KnpLabs\/php-github-api\/contents\/test\/Github\/Tests\/Api\/Repository\/ContentsTest.php","git_url":"https:\/\/api.github.com\/repos\/KnpLabs\/php-github-api\/git\/blobs\/5639a4e6ede372d7ea25b1064be4e2045c2a053d","html_url":"https:\/\/github.com\/KnpLabs\/php-github-api\/blob\/master\/test\/Github\/Tests\/Api\/Repository\/ContentsTest.php","_links":{"self":"https:\/\/api.github.com\/repos\/KnpLabs\/php-github-api\/contents\/test\/Github\/Tests\/Api\/Repository\/ContentsTest.php","git":"https:\/\/api.github.com\/repos\/KnpLabs\/php-github-api\/git\/blobs\/5639a4e6ede372d7ea25b1064be4e2045c2a053d","html":"https:\/\/github.com\/KnpLabs\/php-github-api\/blob\/master\/test\/Github\/Tests\/Api\/Repository\/ContentsTest.php"},"content":"PD9waHAKCm5hbWVzcGFjZSBHaXRodWJcVGVzdHNcQXBpXFJlcG9zaXRvcnk7\nCgp1c2UgR2l0aHViXFRlc3RzXEFwaVxUZXN0Q2FzZTsKCmNsYXNzIENvbnRl\nbnRzVGVzdCBleHRlbmRzIFRlc3RDYXNlCnsKICAgIC8qKgogICAgICogQHRl\nc3QKICAgICAqLwogICAgcHVibGljIGZ1bmN0aW9uIHNob3VsZFNob3dDb250\nZW50Rm9yR2l2ZW5QYXRoKCkKICAgIHsKICAgICAgICAkZXhwZWN0ZWRWYWx1\nZSA9ICc8P3BocCAvLy4uJzsKCiAgICAgICAgJGFwaSA9ICR0aGlzLT5nZXRB\ncGlNb2NrKCk7CiAgICAgICAgJGFwaS0+ZXhwZWN0cygkdGhpcy0+b25jZSgp\nKQogICAgICAgICAgICAtPm1ldGhvZCgnZ2V0JykKICAgICAgICAgICAgLT53\naXRoKCdyZXBvcy9LbnBMYWJzL3BocC1naXRod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lcnRFcXVhbHMoJGV4cGVjdGVkVmFsdWUsICRh\ncGktPmFyY2hpdmUoJ0tucExhYnMnLCAncGhwLWdpdGh1Yi1hcGknLCAndGFy\nYmFsbCcpKTsKICAgIH0KCiAgICAvKioKICAgICAqIEB0ZXN0CiAgICAgKi8K\nICAgIHB1YmxpYyBmdW5jdGlvbiBzaG91bGRGZXRjaFppcGJhbGxBcmNoaXZl\nKCkKICAgIHsKICAgICAgICAkZXhwZWN0ZWRWYWx1ZSA9ICd6aXAnOwoKICAg\nICAgICAkYXBpID0gJHRoaXMtPmdldEFwaU1vY2soKTsKICAgICAgICAkYXBp\nLT5leHBlY3RzKCR0aGlzLT5vbmNlKCkpCiAgICAgICAgICAgIC0+bWV0aG9k\nKCdnZXQnKQogICAgICAgICAgICAtPndpdGgoJ3JlcG9zL0tucExhYnMvcGhw\nLWdpdGh1Yi1hcGkvemlwYmFsbCcsIGFycmF5KCdyZWYnID0+IG51bGwpKQog\nICAgICAgICAgICAtPndpbGwoJHRoaXMtPnJldHVyblZhbHVlKCRleHBlY3Rl\nZFZhbHVlKSk7CgogICAgICAgICR0aGlzLT5hc3NlcnRFcXVhbHMoJGV4cGVj\ndGVkVmFsdWUsICRhcGktPmFyY2hpdmUoJ0tucExhYnMnLCAncGhwLWdpdGh1\nYi1hcGknLCAnemlwYmFsbCcpKTsKICAgIH0KCiAgICBwcm90ZWN0ZWQgZnVu\nY3Rpb24gZ2V0QXBpQ2xhc3MoKQogICAgewogICAgICAgIHJldHVybiAnR2l0\naHViXEFwaVxSZXBvc2l0b3J5XENvbnRlbnRzJzsKICAgIH0KfQo=\n","encoding":"base64"}',
5+
true
6+
);

test/Github/Tests/Api/Repository/ContentsTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,26 @@ public function shouldFetchZipballArchive()
8585

8686
$this->assertEquals($expectedValue, $api->archive('KnpLabs', 'php-github-api', 'zipball'));
8787
}
88+
89+
/**
90+
* @test
91+
*/
92+
public function shouldDownloadForGivenPath()
93+
{
94+
// The show() method return
95+
$getValue = include 'ContentsDownloadFixture.php';
96+
97+
// The download() method return
98+
$expectedValue = base64_decode($getValue['content']);
99+
100+
$api = $this->getApiMock();
101+
$api->expects($this->once())
102+
->method('get')
103+
->with('repos/KnpLabs/php-github-api/contents/test%2FGithub%2FTests%2FApi%2FRepository%2FContentsTest.php', array('ref' => null))
104+
->will($this->returnValue($getValue));
105+
106+
$this->assertEquals($expectedValue, $api->download('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php'));
107+
}
88108

89109
protected function getApiClass()
90110
{

0 commit comments

Comments
 (0)