Skip to content

Commit bd680aa

Browse files
authored
Add paginated projects endpoint (replacement for deprecated get all projects) (#120)
* Add PaginatedResult class and getProjectsPaginated method * Improve code formatting in ProjectService.php * Update setSelf method parameter to string type
1 parent 63b3e72 commit bd680aa

File tree

3 files changed

+221
-0
lines changed

3 files changed

+221
-0
lines changed

src/Project/PaginatedResult.php

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<?php
2+
3+
namespace JiraCloud\Project;
4+
5+
/**
6+
* Paginated Result object for BoardService.
7+
*/
8+
class PaginatedResult
9+
{
10+
/**
11+
* @var int
12+
*/
13+
public $startAt;
14+
15+
/**
16+
* @var int
17+
*/
18+
public $maxResults;
19+
20+
/**
21+
* @var string
22+
*/
23+
public $nextPage;
24+
25+
/**
26+
* @var string
27+
*/
28+
public $self;
29+
30+
/**
31+
* @var int
32+
*/
33+
public $total;
34+
35+
/**
36+
* @var array
37+
*/
38+
public $values;
39+
40+
/**
41+
* @var bool
42+
*/
43+
public $isLast;
44+
45+
/**
46+
* @return int
47+
*/
48+
public function getStartAt()
49+
{
50+
return $this->startAt;
51+
}
52+
53+
/**
54+
* @param int $startAt
55+
*/
56+
public function setStartAt($startAt)
57+
{
58+
$this->startAt = $startAt;
59+
}
60+
61+
/**
62+
* @return int
63+
*/
64+
public function getMaxResults()
65+
{
66+
return $this->maxResults;
67+
}
68+
69+
/**
70+
* @param int $maxResults
71+
*/
72+
public function setMaxResults($maxResults)
73+
{
74+
$this->maxResults = $maxResults;
75+
}
76+
77+
/**
78+
* @return string
79+
*/
80+
public function getNextPage()
81+
{
82+
return $this->nextPage;
83+
}
84+
85+
/**
86+
* @param string $nextPage
87+
*/
88+
public function setNextPage($nextPage)
89+
{
90+
$this->nextPage = $nextPage;
91+
}
92+
93+
/**
94+
* @return string
95+
*/
96+
public function getSelf()
97+
{
98+
return $this->self;
99+
}
100+
101+
/**
102+
* @param string $self
103+
*/
104+
public function setSelf($self)
105+
{
106+
$this->self = $self;
107+
}
108+
109+
/**
110+
* @return int
111+
*/
112+
public function getTotal()
113+
{
114+
return $this->total;
115+
}
116+
117+
/**
118+
* @param int $total
119+
*/
120+
public function setTotal($total)
121+
{
122+
$this->total = $total;
123+
}
124+
125+
/**
126+
* @return array
127+
*/
128+
public function getValues()
129+
{
130+
return $this->values;
131+
}
132+
133+
/**
134+
* @param array $values
135+
*/
136+
public function setValues($values)
137+
{
138+
$this->values = $values;
139+
}
140+
141+
/**
142+
* @param int $index
143+
*
144+
* @return mixed
145+
*/
146+
public function getValue($index)
147+
{
148+
return $this->values[$index];
149+
}
150+
151+
/**
152+
* @param bool $isLast
153+
*/
154+
public function setIsLast($isLast)
155+
{
156+
$this->isLast = $isLast;
157+
}
158+
159+
/**
160+
* @return bool
161+
*/
162+
public function getIsLast()
163+
{
164+
return $this->isLast;
165+
}
166+
}

src/Project/ProjectService.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,35 @@ public function getAllProjects($paramArray = [])
3535
return $prjs;
3636
}
3737

38+
/**
39+
* get a paginated list of projects.
40+
*
41+
* @param array $paramArray
42+
*
43+
* @throws \JiraCloud\JiraException
44+
*
45+
* @return PaginatedResult
46+
*/
47+
public function getProjectsPaginated($paramArray = []): PaginatedResult
48+
{
49+
$ret = $this->exec($this->uri.'/search'.$this->toHttpQueryParameter($paramArray), null);
50+
51+
$decodedRet = json_decode($ret, false);
52+
53+
$decodedRet->values = $this->json_mapper->mapArray(
54+
$decodedRet->values,
55+
new \ArrayObject(),
56+
Project::class
57+
);
58+
59+
$prjsPag = $this->json_mapper->map(
60+
$decodedRet,
61+
new PaginatedResult()
62+
);
63+
64+
return $prjsPag;
65+
}
66+
3867
/**
3968
* get Project id By Project Key.
4069
* throws HTTPException if the project is not found, or the calling user does not have permission or view it.

tests/ProjectTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,32 @@ public function get_project_lists() : string
3939

4040
return $projKey;
4141
}
42+
43+
/**
44+
* @test
45+
*
46+
*/
47+
public function get_project_paginated_lists() : string
48+
{
49+
$projKey = 'TEST';
50+
try {
51+
$proj = new ProjectService();
52+
53+
$prjsPag = $proj->getProjectsPaginated();
54+
55+
foreach ($prjsPag->values as $p) {
56+
$this->assertTrue($p instanceof Project);
57+
$this->assertTrue(strlen($p->key) > 0);
58+
$this->assertTrue(!empty($p->id));
59+
$this->assertTrue(strlen($p->name) > 0);
60+
}
61+
} catch (\Exception $e) {
62+
$this->fail('get_project_paginated_lists ' . $e->getMessage());
63+
}
64+
65+
return $projKey;
66+
}
67+
4268
/**
4369
* @test
4470
* @depends get_project_lists

0 commit comments

Comments
 (0)