Skip to content

Commit 152f840

Browse files
author
Law Tony
committed
Basecamp 3 API PHP wrapper
0 parents  commit 152f840

26 files changed

+2388
-0
lines changed

Api/AbstractApi.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
namespace Basecamp\Api;
3+
4+
use Basecamp\Client;
5+
use Buzz\Message\Request;
6+
7+
/**
8+
* Class AbstractApi.
9+
*/
10+
abstract class AbstractApi
11+
{
12+
protected $client = null;
13+
private $timeout = 10;
14+
protected $page_size = 50;
15+
16+
public function __construct(Client $client)
17+
{
18+
$this->client = $client;
19+
}
20+
21+
final protected function get($resource, array $params = [])
22+
{
23+
$resource = empty($params) ? $resource : ($resource . (false === strpos($resource, '?') ? '?' : '&') . http_build_query($params, '', '&'));
24+
$response = $this->client->request(Request::METHOD_GET, $resource, [], $this->timeout);
25+
26+
// Fetch next pages
27+
$page = 2;
28+
$response_count = count($response);
29+
while ($response_count == $this->page_size) {
30+
$next_params = empty($params) ? ['page' => $page] : array_merge($params, ['page' => $page]);
31+
$next_resource = ($resource . (false === strpos($resource, '?') ? '?' : '&') . http_build_query($next_params, '', '&'));
32+
$next_response = $this->client->request(Request::METHOD_GET, $next_resource, [], $this->timeout);
33+
if (!empty($next_response)) {
34+
$response = array_merge($response, $next_response);
35+
}
36+
$response_count = count($next_response);
37+
$page++;
38+
}
39+
40+
return $response;
41+
}
42+
43+
final protected function post($resource, array $params)
44+
{
45+
$response = $this->client->request(Request::METHOD_POST, $resource, $params, $this->timeout);
46+
47+
return $response;
48+
}
49+
50+
final protected function put($resource, array $params)
51+
{
52+
$response = $this->client->request(Request::METHOD_PUT, $resource, $params, $this->timeout);
53+
54+
return $response;
55+
}
56+
57+
final protected function delete($resource)
58+
{
59+
$response = $this->client->request(Request::METHOD_DELETE, $resource, [], $this->timeout);
60+
61+
return $response;
62+
}
63+
64+
}

Api/Accesses.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
namespace Basecamp\Api;
3+
4+
/**
5+
* Accesses API.
6+
*
7+
* @link https://github.com/basecamp/bcx-api/blob/master/sections/accesses.md
8+
*/
9+
class Accesses extends AbstractApi
10+
{
11+
/**
12+
* Get accesses to project.
13+
*
14+
* @param integer $projectId
15+
*
16+
* @return array
17+
*/
18+
public function project($projectId)
19+
{
20+
$data = $this->get('/projects/' . $projectId . '/accesses.json');
21+
22+
return $data;
23+
}
24+
25+
/**
26+
* Grant access to project.
27+
*
28+
* @param integer $projectId
29+
* @param array $userIds
30+
*
31+
* @return object
32+
*/
33+
public function grantProject($projectId, array $userIds)
34+
{
35+
$data = $this->post('/projects/' . $projectId . '/accesses.json', $userIds);
36+
37+
return $data;
38+
}
39+
40+
/**
41+
* Revoke access from project.
42+
*
43+
* @param integer $projectId
44+
* @param integer $userId
45+
*
46+
* @return object
47+
*/
48+
public function revokeProject($projectId, $userId)
49+
{
50+
$data = $this->delete('/projects/' . $projectId . '/accesses/' . $userId . '.json');
51+
52+
return $data;
53+
}
54+
55+
/**
56+
* Get accesses to calendar.
57+
*
58+
* @param integer $calendarId
59+
*
60+
* @return array
61+
*/
62+
public function calendar($calendarId)
63+
{
64+
$data = $this->get('/calendars/' . $calendarId . '/accesses.json');
65+
66+
return $data;
67+
}
68+
69+
/**
70+
* Grant access to calendar.
71+
*
72+
* @param integer $calendarId
73+
* @param array $userIds
74+
*
75+
* @return object
76+
*/
77+
public function grantCalendar($calendarId, array $userIds)
78+
{
79+
$data = $this->post('/calendars/' . $calendarId . '/accesses.json', $userIds);
80+
81+
return $data;
82+
}
83+
84+
/**
85+
* Revoke access from calendar.
86+
*
87+
* @param integer $calendarId
88+
* @param integer $userId
89+
*
90+
* @return object
91+
*/
92+
public function revokeCalendar($calendarId, $userId)
93+
{
94+
$data = $this->delete('/projects/' . $calendarId . '/accesses/' . $userId . '.json');
95+
96+
return $data;
97+
}
98+
}

Api/Attachments.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
namespace Basecamp\Api;
3+
4+
/**
5+
* Attachments API.
6+
*
7+
* @link https://github.com/basecamp/bcx-api/blob/master/sections/attachments.md
8+
*/
9+
class Attachments extends AbstractApi
10+
{
11+
/**
12+
* Get attachments.
13+
*
14+
* @return array
15+
*/
16+
public function all()
17+
{
18+
$data = $this->get('/attachments.json');
19+
20+
return $data;
21+
}
22+
23+
/**
24+
* Get project's attachments.
25+
*
26+
* @param integer $projectId
27+
*
28+
* @return array
29+
*/
30+
public function projectAll($projectId)
31+
{
32+
$data = $this->get('/projects/' . $projectId . '/attachments.json');
33+
34+
return $data;
35+
}
36+
37+
/**
38+
* Get attachment.
39+
*
40+
* @param integer $projectId
41+
* @param integer $attachmentId
42+
*
43+
* @return object
44+
*/
45+
public function show($projectId, $attachmentId)
46+
{
47+
$data = $this->get('GET /projects/' . $projectId . '/attachments/' . $attachmentId . '.json');
48+
49+
return $data;
50+
}
51+
52+
/**
53+
* Create attachment.
54+
*
55+
* @param string $binary
56+
*
57+
* @return object
58+
*/
59+
public function create($binary)
60+
{
61+
$data = $this->post('/attachments.json', ['binary' => $binary]);
62+
63+
return $data;
64+
}
65+
}

Api/Calendars.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
namespace Basecamp\Api;
3+
4+
/**
5+
* Calendars API.
6+
*
7+
* @link https://github.com/basecamp/bcx-api/blob/master/sections/calendars.md
8+
*/
9+
class Calendars extends AbstractApi
10+
{
11+
/**
12+
* Get calendars
13+
*
14+
* @return array
15+
*/
16+
public function all()
17+
{
18+
$data = $this->get('/calendars.json');
19+
20+
return $data;
21+
}
22+
23+
/**
24+
* Get calendar.
25+
*
26+
* @param integer $calendarId
27+
*
28+
* @return object
29+
*/
30+
public function show($calendarId)
31+
{
32+
$data = $this->get('/calendars/' . $calendarId . '.json');
33+
34+
return $data;
35+
}
36+
37+
/**
38+
* Create calendar.
39+
*
40+
* @param array $params
41+
*
42+
* @return object
43+
*/
44+
public function create(array $params)
45+
{
46+
$data = $this->post('/calendars.json', $params);
47+
48+
return $data;
49+
}
50+
51+
/**
52+
* Update calendar.
53+
*
54+
* @param integer $calendarId
55+
* @param array $params
56+
*
57+
* @return object
58+
*/
59+
public function update($calendarId, array $params)
60+
{
61+
$data = $this->put('/calendars/' . $calendarId . '.json', $params);
62+
63+
return $data;
64+
}
65+
66+
/**
67+
* Delete calendar.
68+
*
69+
* @param integer $calendarId
70+
*
71+
* @return object
72+
*/
73+
public function remove($calendarId)
74+
{
75+
$data = $this->delete('/calendars/' . $calendarId . '.json');
76+
77+
return $data;
78+
}
79+
}

0 commit comments

Comments
 (0)