Skip to content

Commit d7cb4ea

Browse files
committed
Added snippets endpoints and Model
1 parent 050fcc1 commit d7cb4ea

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

lib/Gitlab/Api/Snippets.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Gitlab\Api;
4+
5+
class Snippets extends AbstractApi
6+
{
7+
public function all($project_id)
8+
{
9+
return $this->get('projects/'.urlencode($project_id).'/snippets');
10+
}
11+
12+
public function show($project_id, $snippet_id)
13+
{
14+
return $this->get('projects/'.urlencode($project_id).'/snippets/'.urlencode($snippet_id));
15+
}
16+
17+
public function create($project_id, $title, $filename, $code, $lifetime = null)
18+
{
19+
return $this->post('projects/'.urlencode($project_id).'/snippets', array(
20+
'title' => $title,
21+
'file_name' => $filename,
22+
'code' => $code,
23+
'lifetime' => $lifetime
24+
));
25+
}
26+
27+
public function update($project_id, $snippet_id, array $params)
28+
{
29+
return $this->put('projects/'.urlencode($project_id).'/snippets/'.urlencode($snippet_id), $params);
30+
}
31+
32+
public function content($project_id, $snippet_id)
33+
{
34+
return $this->get('projects/'.urlencode($project_id).'/snippets/'.urlencode($snippet_id).'/raw');
35+
}
36+
37+
public function remove($project_id, $snippet_id)
38+
{
39+
return $this->delete('projects/'.urlencode($project_id).'/snippets/'.urlencode($snippet_id));
40+
}
41+
42+
}

lib/Gitlab/Model/Project.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ public function tree(array $params = array())
243243
return $tree;
244244
}
245245

246+
public function blob($sha, $filepath)
247+
{
248+
return $this->api('repo')->blob($this->id, $sha, $filepath);
249+
}
250+
246251
public function events()
247252
{
248253
$data = $this->api('projects')->events($this->id);
@@ -251,6 +256,8 @@ public function events()
251256
foreach ($data as $event) {
252257
$events[] = Event::fromArray($this->getClient(), $this, $event);
253258
}
259+
260+
return $events;
254261
}
255262

256263
public function mergeRequests($page = 1, $per_page = Api::PER_PAGE)
@@ -381,4 +388,51 @@ public function updateMilestone($id, array $params)
381388

382389
return $milestone->update($params);
383390
}
391+
392+
public function snippets()
393+
{
394+
$data = $this->api('snippets')->all($this->id);
395+
396+
$snippets = array();
397+
foreach ($data as $snippet) {
398+
$snippets[] = Snippet::fromArray($this->getClient(), $this, $snippet);
399+
}
400+
401+
return $snippets;
402+
}
403+
404+
public function createSnippet($title, $filename, $code, $lifetime = null)
405+
{
406+
$data = $this->api('snippets')->create($this->id, $filename, $code, $lifetime);
407+
408+
return Snippet::fromArray($this->getClient(), $this, $data);
409+
}
410+
411+
public function snippet($id)
412+
{
413+
$snippet = new Snippet($this, $id, $this->getClient());
414+
415+
return $snippet->show();
416+
}
417+
418+
public function snippetContent($id)
419+
{
420+
$snippet = new Snippet($this, $id, $this->getClient());
421+
422+
return $snippet->content();
423+
}
424+
425+
public function updateSnippet($id, array $params)
426+
{
427+
$snippet = new Snippet($this, $id, $this->getClient());
428+
429+
return $snippet->update($params);
430+
}
431+
432+
public function removeSnippet($id)
433+
{
434+
$snippet = new Snippet($this, $id, $this->getClient());
435+
436+
return $snippet->remove();
437+
}
384438
}

lib/Gitlab/Model/Snippet.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Gitlab\Model;
4+
5+
use Gitlab\Client;
6+
7+
class Snippet extends AbstractModel
8+
{
9+
protected static $_properties = array(
10+
'id',
11+
'title',
12+
'file_name',
13+
'author',
14+
'expires_at',
15+
'updated_at',
16+
'created_at',
17+
'project'
18+
);
19+
20+
public static function fromArray(Client $client, Project $project, array $data)
21+
{
22+
$snippet = new Snippet($project, $data['id'], $client);
23+
24+
if (isset($data['author'])) {
25+
$data['author'] = User::fromArray($client, $data['author']);
26+
}
27+
28+
return $snippet->hydrate($data);
29+
}
30+
31+
public function __construct($project, $id = null, Client $client = null)
32+
{
33+
$this->setClient($client);
34+
35+
$this->project = $project;
36+
$this->id = $id;
37+
}
38+
39+
public function show()
40+
{
41+
$data = $this->api('snippets')->show($this->project->id, $this->id);
42+
43+
return Snippet::fromArray($this->getClient(), $this, $data);
44+
}
45+
46+
public function update(array $params)
47+
{
48+
$data = $this->api('snippets')->update($this->project->id, $this->id, $params);
49+
50+
return Snippet::fromArray($this->getClient(), $this, $data);
51+
}
52+
53+
public function content()
54+
{
55+
return $this->api('snippets')->content($this->project->id, $this->id);
56+
}
57+
58+
public function remove()
59+
{
60+
$this->api('snippets')->remove($this->project->id, $this->id);
61+
62+
return true;
63+
}
64+
}

0 commit comments

Comments
 (0)