Skip to content

Commit 3b76f18

Browse files
committed
Merge pull request kbsali#143 from airmoi/master
Add magic getter to retrieve API and improve code completion
2 parents c6b0732 + 1a5b2dc commit 3b76f18

File tree

2 files changed

+63
-28
lines changed

2 files changed

+63
-28
lines changed

README.markdown

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,16 @@ $client = new Redmine\Client('http://redmine.example.com', 'API_ACCESS_KEY');
141141
//-- OR --
142142
$client = new Redmine\Client('http://redmine.example.com', 'username', 'password');
143143

144-
$client->api('user')->all();
145-
$client->api('user')->listing();
144+
$client->user->all();
145+
$client->user->listing();
146146

147-
$client->api('issue')->create([
147+
$client->issue->create([
148148
'project_id' => 'test',
149149
'subject' => 'some subject',
150150
'description' => 'a long description blablabla',
151151
'assigned_to' => 'user1',
152152
]);
153-
$client->api('issue')->all([
153+
$client->issue->all([
154154
'limit' => 1000
155155
]);
156156
```
@@ -169,7 +169,7 @@ $client = new Redmine\Client('http://redmine.example.com', 'API_ACCESS_KEY');
169169
$client->setImpersonateUser('jsmith');
170170

171171
// create a time entry for jsmith
172-
$client->api('time_entry')->create($data);
172+
$client->time_entry->create($data);
173173

174174
// remove impersonation for further calls
175175
$client->setImpersonateUser(null);

lib/Redmine/Client.php

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@
99
*
1010
* @author Kevin Saliou <kevin at saliou dot name>
1111
* Website: http://github.com/kbsali/php-redmine-api
12+
*
13+
* @property Api\Attachment $attachment
14+
* @property Api\Group $group
15+
* @property Api\CustomField $custom_fields
16+
* @property Api\Issue $issue
17+
* @property Api\IssueCategory $issue_category
18+
* @property Api\IssuePriority $issue_priority
19+
* @property Api\IssueRelation $issue_relation
20+
* @property Api\IssueStatus $issue_status
21+
* @property Api\Membership $membership
22+
* @property Api\News $news
23+
* @property Api\Project $project
24+
* @property Api\Query $query
25+
* @property Api\Role $role
26+
* @property Api\TimeEntry $time_entry
27+
* @property Api\TimeEntryActivity $time_entry_activity
28+
* @property Api\Tracker $tracker
29+
* @property Api\User $user
30+
* @property Api\Version $version
31+
* @property Api\Wiki $wiki
1232
*/
1333
class Client
1434
{
@@ -97,6 +117,28 @@ class Client
97117
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
98118
JSON_ERROR_SYNTAX => 'Syntax error',
99119
);
120+
121+
private $classes = array(
122+
'attachment' => 'Attachment',
123+
'group' => 'Group',
124+
'custom_fields' => 'CustomField',
125+
'issue' => 'Issue',
126+
'issue_category' => 'IssueCategory',
127+
'issue_priority' => 'IssuePriority',
128+
'issue_relation' => 'IssueRelation',
129+
'issue_status' => 'IssueStatus',
130+
'membership' => 'Membership',
131+
'news' => 'News',
132+
'project' => 'Project',
133+
'query' => 'Query',
134+
'role' => 'Role',
135+
'time_entry' => 'TimeEntry',
136+
'time_entry_activity' => 'TimeEntryActivity',
137+
'tracker' => 'Tracker',
138+
'user' => 'User',
139+
'version' => 'Version',
140+
'wiki' => 'Wiki',
141+
);
100142

101143
/**
102144
* Usage: apikeyOrUsername can be auth key or username.
@@ -113,6 +155,20 @@ public function __construct($url, $apikeyOrUsername, $pass = null)
113155
$this->apikeyOrUsername = $apikeyOrUsername;
114156
$this->pass = $pass;
115157
}
158+
159+
/**
160+
* PHP getter magic method.
161+
*
162+
* @param string $name
163+
*
164+
* @return Api\AbstractApi
165+
*
166+
* @throws \InvalidArgumentException
167+
*/
168+
public function __get($name)
169+
{
170+
return $this->api($name);
171+
}
116172

117173
/**
118174
* @param string $name
@@ -123,34 +179,13 @@ public function __construct($url, $apikeyOrUsername, $pass = null)
123179
*/
124180
public function api($name)
125181
{
126-
$classes = array(
127-
'attachment' => 'Attachment',
128-
'group' => 'Group',
129-
'custom_fields' => 'CustomField',
130-
'issue' => 'Issue',
131-
'issue_category' => 'IssueCategory',
132-
'issue_priority' => 'IssuePriority',
133-
'issue_relation' => 'IssueRelation',
134-
'issue_status' => 'IssueStatus',
135-
'membership' => 'Membership',
136-
'news' => 'News',
137-
'project' => 'Project',
138-
'query' => 'Query',
139-
'role' => 'Role',
140-
'time_entry' => 'TimeEntry',
141-
'time_entry_activity' => 'TimeEntryActivity',
142-
'tracker' => 'Tracker',
143-
'user' => 'User',
144-
'version' => 'Version',
145-
'wiki' => 'Wiki',
146-
);
147-
if (!isset($classes[$name])) {
182+
if (!isset($this->classes[$name])) {
148183
throw new \InvalidArgumentException();
149184
}
150185
if (isset($this->apis[$name])) {
151186
return $this->apis[$name];
152187
}
153-
$c = 'Redmine\Api\\'.$classes[$name];
188+
$c = 'Redmine\Api\\'.$this->classes[$name];
154189
$this->apis[$name] = new $c($this);
155190

156191
return $this->apis[$name];

0 commit comments

Comments
 (0)