Skip to content

Commit 4f5e883

Browse files
authored
Merge pull request #224 from fbourigault/options-resolver
Use options resolver almost everywhere
2 parents 9a35ee9 + 4e6800b commit 4f5e883

24 files changed

+479
-430
lines changed

UPGRADE.md

+36-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,32 @@ See [documentation](doc/customize.md) to know how to customize the client timeou
1313
* The `setHttpClient` method have been removed. Use a `Gitlab\HttpClient\Builder` instead.
1414
* The `getHttpClient` method return type is changed to `Http\Client\Common\HttpMethodsClient`.
1515

16+
## `Gitlab\Api\AbstractApi` changes
17+
18+
* The `PER_PAGE` class constant have been removed.
19+
20+
## `Gitlab\Api\DeployKeys` changes
21+
22+
* The `all` method now take a single argument which is an associative array of query string parameters.
23+
* The `ORDER_BY` and `SORT` class constants have been removed.
1624

1725
## `Gitlab\Api\Groups` changes
1826

1927
* The `visibility_level` parameter have been removed from `create` method. Use `visibility` instead.
28+
* The `all` method now take a single argument which is an associative array of query string parameters.
29+
* The `search` method have been removed. Use `all` method instead.
30+
* The `members` method second and subsequent arguments have been replaced by a single associative array of query string parameters.
31+
* The `projects` method second and subsequent arguments have been replaced by a single associative array of query string parameters.
2032

2133
## `Gitlab\Api\Issues` changes
2234

2335
* The second argument of `update`, `remove`, `showComments`, `showComment`, `addComment`, `updateComment`, `removeComment`,
2436
`setTimeEstimate`, `resetTimeEstimate`, `addSpentTime` and `resetSpentTime` methods is now a scoped issue id (iid).
37+
* The `all` method second and subsequent arguments have been replaced by a single associative array of query string parameters.
38+
39+
## `Gitlab\Api\IssueBoards` changes
40+
41+
* The `all` method second and subsequent arguments have been replaced by a single associative array of query string parameters.
2542

2643
## `Gitlab\Api\MergeRequests` changes
2744

@@ -30,6 +47,10 @@ See [documentation](doc/customize.md) to know how to customize the client timeou
3047
* The `all` method now take a single argument which is an associative array of query string parameters.
3148
* The `getNotes` method now take only two arguments, the project id and the merge request iid.
3249

50+
## `Gitlab\Api\Milestones` changes
51+
52+
* The `all` method second and subsequent arguments have been replaced by a single associative array of query string parameters.
53+
3354
## `Gitlab\Api\Projects` changes
3455

3556
* The `keys`, `key`, `addKey`, `removeKey`, `disableKey` and `enableKey` methods have been removed.
@@ -40,11 +61,19 @@ Use the `deployKeys`, `deployKey`, `addDeployKey`, `deleteDeployKey`, `removeDep
4061
* The `trace` method have been removed. Use `Gitlab\Api\Jobs::trace` instead.
4162
* The `builds` method have been removed. Use `Gitlab\Api\Jobs::all` instead.
4263
* The `build` method have been removed. Use `Gitlab\Api\Jobs::show` instead.
64+
* The `events` method second and subsequent arguments have been replaced by a single associative array of query string parameters.
65+
* The `deployments` method second and subsequent arguments have been replaced by a single associative array of query string parameters.
66+
67+
## `Gitlab\Api\ProjectNamespaces` changes
68+
69+
* The `search` method have been removed. Use `all` method instead.
70+
* The `all` method now take a single argument which is an associative array of query string parameters.
4371

4472
## `Gitlab\Api\Repositories` changes
4573

46-
* The `commits` page argument now start from 1 instead of 0.
4774
* The `commitBuilds` method have been removed. Use `Gitlab\Api\Projects::pipelines` instead.
75+
* The `commits` method second and subsequent arguments have been replaced by a single associative array of query string parameters.
76+
* The `commitComments` method third and subsequent arguments have been replaced by a single associative array of query string parameters.
4877

4978
## `Gitlab\Model\Project` changes
5079

@@ -53,4 +82,9 @@ Use the `deployKeys`, `deployKey`, `addDeployKey`, `deleteDeployKey`, `removeDep
5382

5483
## `Gitlab\Model\Snippet` changes
5584

56-
The `expires_at` property have been removed.`
85+
* The `expires_at` property have been removed.`
86+
87+
## `Gitlab\Model\Users` changes
88+
89+
* The `all` method now take a single argument which is an associative array of query string parameters.
90+
* The `lookup` and `search` methods have been removed. Use `all` method instead.

lib/Gitlab/Api/AbstractApi.php

+1-9
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919
*/
2020
abstract class AbstractApi implements ApiInterface
2121
{
22-
/**
23-
* Default entries per page
24-
*/
25-
const PER_PAGE = 20;
26-
2722
/**
2823
* The client
2924
*
@@ -179,7 +174,7 @@ protected function encodePath($path)
179174
}
180175

181176
/**
182-
* Create a new OptionsResolver with page, per_page and sort options.
177+
* Create a new OptionsResolver with page and per_page options.
183178
*
184179
* @return OptionsResolver
185180
*/
@@ -198,9 +193,6 @@ protected function createOptionsResolver()
198193
return $value > 0 && $value <= 100;
199194
})
200195
;
201-
$resolver->setDefined('sort')
202-
->setAllowedValues('sort', ['asc', 'desc'])
203-
;
204196

205197
return $resolver;
206198
}

lib/Gitlab/Api/DeployKeys.php

+6-14
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,15 @@
22

33
class DeployKeys extends AbstractApi
44
{
5-
const ORDER_BY = 'id';
6-
const SORT = 'asc';
7-
85
/**
9-
* @param int $page
10-
* @param int $per_page
11-
* @param string $order_by
12-
* @param string $sort
6+
* @param array $parameters
7+
*
138
* @return mixed
149
*/
15-
public function all($page = 1, $per_page = self::PER_PAGE, $order_by = self::ORDER_BY, $sort = self::SORT)
10+
public function all(array $parameters = [])
1611
{
17-
return $this->get('deploy_keys', array(
18-
'page' => $page,
19-
'per_page' => $per_page,
20-
'order_by' => $order_by,
21-
'sort' => $sort
22-
));
12+
$resolver = $this->createOptionsResolver();
13+
14+
return $this->get('deploy_keys', $resolver->resolve($parameters));
2315
}
2416
}

lib/Gitlab/Api/Groups.php

+100-35
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,52 @@
33
class Groups extends AbstractApi
44
{
55
/**
6-
* @param int $page
7-
* @param int $per_page
6+
* @param array $parameters (
7+
*
8+
* @var int[] $skip_groups Skip the group IDs passes.
9+
* @var bool $all_available Show all the groups you have access to.
10+
* @var string $search Return list of authorized groups matching the search criteria.
11+
* @var string $order_by Order groups by name or path. Default is name.
12+
* @var string $sort Order groups in asc or desc order. Default is asc.
13+
* @var bool $statistics Include group statistics (admins only).
14+
* @var bool $owned Limit by groups owned by the current user.
15+
* )
816
* @return mixed
917
*/
10-
public function all($page = 1, $per_page = self::PER_PAGE)
18+
public function all(array $parameters = [])
1119
{
12-
return $this->get('groups', array(
13-
'page' => $page,
14-
'per_page' => $per_page
15-
));
16-
}
20+
$resolver = $this->createOptionsResolver();
21+
$booleanNormalizer = function ($value) {
22+
return $value ? 'true' : 'false';
23+
};
1724

18-
/**
19-
* @param string $query
20-
* @param int $page
21-
* @param int $per_page
22-
* @return mixed
23-
*/
24-
public function search($query, $page = 1, $per_page = self::PER_PAGE)
25-
{
26-
return $this->get('groups?search='.$this->encodePath($query), array(
27-
'page' => $page,
28-
'per_page' => $per_page
29-
));
25+
$resolver->setDefined('skip_groups')
26+
->setAllowedTypes('skip_groups', 'array')
27+
->setAllowedValues('skip_groups', function (array $value) {
28+
return count($value) == count(array_filter($value, 'is_int'));
29+
})
30+
;
31+
$resolver->setDefined('all_available')
32+
->setAllowedTypes('all_available', 'bool')
33+
->setNormalizer('all_available', $booleanNormalizer)
34+
;
35+
$resolver->setDefined('search');
36+
$resolver->setDefined('order_by')
37+
->setAllowedValues('order_by', ['name', 'path'])
38+
;
39+
$resolver->setDefined('sort')
40+
->setAllowedValues('sort', ['asc', 'desc'])
41+
;
42+
$resolver->setDefined('statistics')
43+
->setAllowedTypes('statistics', 'bool')
44+
->setNormalizer('statistics', $booleanNormalizer)
45+
;
46+
$resolver->setDefined('owned')
47+
->setAllowedTypes('owned', 'bool')
48+
->setNormalizer('owned', $booleanNormalizer)
49+
;
50+
51+
return $this->get('groups', $resolver->resolve($parameters));
3052
}
3153

3254
/**
@@ -85,17 +107,20 @@ public function transfer($group_id, $project_id)
85107
}
86108

87109
/**
88-
* @param int $id
89-
* @param int $page
90-
* @param int $per_page
110+
* @param int $id
111+
* @param array $parameters (
112+
*
113+
* @var string $query A query string to search for members.
114+
* )
115+
*
91116
* @return mixed
92117
*/
93-
public function members($id, $page = 1, $per_page = self::PER_PAGE)
118+
public function members($id, array $parameters = [])
94119
{
95-
return $this->get('groups/'.$this->encodePath($id).'/members', array(
96-
'page' => $page,
97-
'per_page' => $per_page
98-
));
120+
$resolver = $this->createOptionsResolver();
121+
$resolver->setDefined('query');
122+
123+
return $this->get('groups/'.$this->encodePath($id).'/members', $resolver->resolve($parameters));
99124
}
100125

101126
/**
@@ -137,15 +162,55 @@ public function removeMember($group_id, $user_id)
137162

138163
/**
139164
* @param $id
140-
* @param int $page
141-
* @param int $per_page
165+
* @param array $parameters (
166+
*
167+
* @var bool $archived Limit by archived status.
168+
* @var string $visibility Limit by visibility public, internal, or private.
169+
* @var string $order_by Return projects ordered by id, name, path, created_at, updated_at, or last_activity_at fields.
170+
* Default is created_at.
171+
* @var string $sort Return projects sorted in asc or desc order. Default is desc.
172+
* @var string $search Return list of authorized projects matching the search criteria.
173+
* @var bool $simple Return only the ID, URL, name, and path of each project.
174+
* @var bool $owned Limit by projects owned by the current user.
175+
* @var bool $starred Limit by projects starred by the current user.
176+
* )
177+
*
142178
* @return mixed
143179
*/
144-
public function projects($id, $page = 1, $per_page = self::PER_PAGE)
180+
public function projects($id, array $parameters = [])
145181
{
146-
return $this->get('groups/'.$this->encodePath($id).'/projects', array(
147-
'page' => $page,
148-
'per_page' => $per_page
149-
));
182+
$resolver = $this->createOptionsResolver();
183+
$booleanNormalizer = function ($value) {
184+
return $value ? 'true' : 'false';
185+
};
186+
187+
$resolver->setDefined('archived')
188+
->setAllowedTypes('archived', 'bool')
189+
->setNormalizer('archived', $booleanNormalizer)
190+
;
191+
$resolver->setDefined('visibility')
192+
->setAllowedValues('visibility', ['public', 'internal', 'private'])
193+
;
194+
$resolver->setDefined('order_by')
195+
->setAllowedValues('order_by', ['id', 'name', 'path', 'created_at', 'updated_at', 'last_activity_at'])
196+
;
197+
$resolver->setDefined('sort')
198+
->setAllowedValues('sort', ['asc', 'desc'])
199+
;
200+
$resolver->setDefined('search');
201+
$resolver->setDefined('simple')
202+
->setAllowedTypes('simple', 'bool')
203+
->setNormalizer('simple', $booleanNormalizer)
204+
;
205+
$resolver->setDefined('owned')
206+
->setAllowedTypes('owned', 'bool')
207+
->setNormalizer('owned', $booleanNormalizer)
208+
;
209+
$resolver->setDefined('starred')
210+
->setAllowedTypes('starred', 'bool')
211+
->setNormalizer('starred', $booleanNormalizer)
212+
;
213+
214+
return $this->get('groups/'.$this->encodePath($id).'/projects', $resolver->resolve($parameters));
150215
}
151216
}

lib/Gitlab/Api/IssueBoards.php

+6-10
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@ class IssueBoards extends AbstractApi
44
{
55
/**
66
* @param int $project_id
7-
* @param int $page
8-
* @param int $per_page
9-
* @param array $params
7+
* @param array $parameters
8+
*
109
* @return mixed
1110
*/
12-
public function all($project_id = null, $page = 1, $per_page = self::PER_PAGE, array $params = array())
11+
public function all($project_id = null, array $parameters = [])
1312
{
14-
$path = $project_id === null ? 'boards' : $this->getProjectPath($project_id, 'boards');
13+
$resolver = $this->createOptionsResolver();
1514

16-
$params = array_merge(array(
17-
'page' => $page,
18-
'per_page' => $per_page
19-
), $params);
15+
$path = $project_id === null ? 'boards' : $this->getProjectPath($project_id, 'boards');
2016

21-
return $this->get($path, $params);
17+
return $this->get($path, $resolver->resolve($parameters));
2218
}
2319

2420
/**

lib/Gitlab/Api/Issues.php

+35-11
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,46 @@ class Issues extends AbstractApi
44
{
55
/**
66
* @param int $project_id
7-
* @param int $page
8-
* @param int $per_page
9-
* @param array $params
7+
* @param array $parameters (
8+
*
9+
* @var string $state Return all issues or just those that are opened or closed.
10+
* @var string $labels Comma-separated list of label names, issues must have all labels to be returned.
11+
* No+Label lists all issues with no labels.
12+
* @var string $milestone The milestone title.
13+
* @var int[] $iids Return only the issues having the given iid.
14+
* @var string $order_by Return requests ordered by created_at or updated_at fields. Default is created_at.
15+
* @var string $sort Return requests sorted in asc or desc order. Default is desc.
16+
* @var string $search Search issues against their title and description.
17+
* )
18+
*
1019
* @return mixed
1120
*/
12-
public function all($project_id = null, $page = 1, $per_page = self::PER_PAGE, array $params = array())
21+
public function all($project_id = null, array $parameters = [])
1322
{
14-
$path = $project_id === null ? 'issues' : $this->getProjectPath($project_id, 'issues');
23+
$resolver = $this->createOptionsResolver();
24+
25+
$resolver->setDefined('state')
26+
->setAllowedValues('state', ['opened', 'closed'])
27+
;
28+
$resolver->setDefined('labels');
29+
$resolver->setDefined('milestone');
30+
$resolver->setDefined('iids')
31+
->setAllowedTypes('iids', 'array')
32+
->setAllowedValues('iids', function (array $value) {
33+
return count($value) == count(array_filter($value, 'is_int'));
34+
})
35+
;
36+
$resolver->setDefined('order_by')
37+
->setAllowedValues('order_by', ['created_at', 'updated_at'])
38+
;
39+
$resolver->setDefined('sort')
40+
->setAllowedValues('sort', ['asc', 'desc'])
41+
;
42+
$resolver->setDefined('search');
1543

16-
$params = array_intersect_key($params, array('labels' => '', 'state' => '', 'sort' => '', 'order_by' => '', 'milestone' => ''));
17-
$params = array_merge(array(
18-
'page' => $page,
19-
'per_page' => $per_page
20-
), $params);
44+
$path = $project_id === null ? 'issues' : $this->getProjectPath($project_id, 'issues');
2145

22-
return $this->get($path, $params);
46+
return $this->get($path, $resolver->resolve($parameters));
2347
}
2448

2549
/**

0 commit comments

Comments
 (0)