Skip to content

Commit 6496b7d

Browse files
author
Artem Stepin
committed
added support for comments and subpages
1 parent de2bf86 commit 6496b7d

File tree

5 files changed

+132
-30
lines changed

5 files changed

+132
-30
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ $client->content()->update($createdPage);
5353
//get child content
5454
$childContent = $client->content()->children($createdPage, Content::CONTENT_TYPE_PAGE);
5555

56+
//create a comment
57+
$commentContent = $createdPage->createComment('test comment');
58+
$createdComment = $client->content()->create($commentContent);
59+
60+
//update a comment
61+
$createdComment->setContent('new comment');
62+
$client->content()->update($createdComment);
63+
64+
//delete a content
65+
$client->content()->remove($createdComment);
5666

5767

5868
```

src/Api/Content.php

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ class Content extends AbstractApi
3939
*/
4040
public const CONTENT_TYPE_PAGE = 'page';
4141

42+
/**
43+
* ContentType for confluence global content
44+
*/
45+
public const CONTENT_TYPE_GLOBAL = 'global';
46+
47+
/**
48+
* default value for expand query parameter
49+
*/
50+
private const DEFAULT_EXPAND = 'space,version,body.storage,container';
51+
4252
/**
4353
* @param string|int|null ...$parameter
4454
* @return string
@@ -65,7 +75,7 @@ private static function getContentUri(...$parameter): string
6575
*/
6676
public function findOneById(int $contentId): AbstractContent
6777
{
68-
$response = $this->get(self::getContentUri($contentId), ['expand' => 'space,version,body.storage']);
78+
$response = $this->get(self::getContentUri($contentId), ['expand' => self::DEFAULT_EXPAND]);
6979

7080
if ($response->getStatusCode() !== 200) {
7181
throw new RequestException($response);
@@ -75,61 +85,69 @@ public function findOneById(int $contentId): AbstractContent
7585
}
7686

7787
/**
78-
* @param AbstractContent $page
88+
* @param AbstractContent $content
7989
* @return ResponseInterface
8090
* @throws Exception
8191
* @throws JsonException
8292
* @throws HttpClientException
8393
*/
84-
public function update(AbstractContent $page): ResponseInterface
94+
public function update(AbstractContent $content): ResponseInterface
8595
{
86-
$contentId = $page->getId();
96+
$contentId = $content->getId();
8797
if (null === $contentId) {
88-
throw new Exception('Only saved pages can be updated.');
98+
throw new Exception('Only saved content can be updated.');
8999
}
90100
$data = [
91101
'id' => $contentId,
92-
'type' => $page->getType(),
93-
'title' => $page->getTitle(),
94-
'space' => ['key' => $page->getSpace()],
102+
'type' => $content->getType(),
103+
'title' => $content->getTitle(),
104+
'space' => ['key' => $content->getSpace()],
95105
'body' => [
96106
'storage' => [
97-
'value' => $page->getContent(),
107+
'value' => $content->getContent(),
98108
'representation' => 'storage',
99109
],
100110
],
101-
'version' => ['number' => $page->getVersion() + 1]
111+
'version' => ['number' => $content->getVersion() + 1]
102112
];
103113

104114
return $this->put(self::getContentUri($contentId), $data);
105115

106116
}
107117

108118
/**
109-
* @param AbstractContent $page
119+
* @param AbstractContent $content
110120
* @return AbstractContent
111121
* @throws Exception
112122
* @throws HttpClientException
113123
* @throws JsonException
114124
*/
115-
public function create(AbstractContent $page): AbstractContent
125+
public function create(AbstractContent $content): AbstractContent
116126
{
117-
if (null !== $page->getId()) {
127+
if (null !== $content->getId()) {
118128
throw new Exception('Only new pages can be created.');
119129
}
120130

121131
$data = [
122-
'type' => $page->getType(),
123-
'title' => $page->getTitle(),
124-
'space' => ['key' => $page->getSpace()],
132+
'type' => $content->getType(),
133+
'title' => $content->getTitle(),
134+
'space' => ['key' => $content->getSpace()],
125135
'body' => [
126136
'storage' => [
127-
'value' => $page->getContent(),
137+
'value' => $content->getContent(),
128138
'representation' => 'storage',
129139
],
130140
],
131141
];
132142

143+
/* attach content to content */
144+
if(null !== $content->getContainerId()) {
145+
$data['container'] = [
146+
'id' => $content->getContainerId(),
147+
'type' => $content->getContainerType(),
148+
];
149+
}
150+
133151
$response = $this->post(self::getContentUri(), $data);
134152

135153
if ($response->getStatusCode() !== 200) {
@@ -141,16 +159,16 @@ public function create(AbstractContent $page): AbstractContent
141159
}
142160

143161
/**
144-
* @param AbstractContent $page
162+
* @param AbstractContent $content
145163
* @return ResponseInterface
146164
*/
147-
public function remove(AbstractContent $page): ResponseInterface
165+
public function remove(AbstractContent $content): ResponseInterface
148166
{
149-
$contentId = $page->getId();
167+
$contentId = $content->getId();
150168
if (null === $contentId) {
151169
throw new Exception('Only saved pages can be removed.');
152170
}
153-
return $this->put(self::getContentUri($contentId));
171+
return $this->delete(self::getContentUri($contentId));
154172
}
155173

156174
/**
@@ -165,7 +183,7 @@ public function children(AbstractContent $content, ?string $contentType = null):
165183
return $this->parseSearchResults(
166184
$this->get(
167185
self::getContentUri($content->getId(), 'child', $contentType),
168-
['expand' => 'space,version,body.storage']
186+
['expand' => self::DEFAULT_EXPAND]
169187
),
170188
);
171189
}
@@ -195,7 +213,7 @@ public function findOneBy(array $searchParameter): ?AbstractContent
195213
return in_array($searchKey, $allowedSearchParameter, true);
196214
}, ARRAY_FILTER_USE_KEY);
197215

198-
$queryParameter['expand'] = 'space,version,body.storage';
216+
$queryParameter['expand'] = self::DEFAULT_EXPAND;
199217

200218
$searchResponse = $this->get('content?', $queryParameter);
201219

@@ -280,7 +298,6 @@ private function deserializeContent(array $decodedData): AbstractContent
280298
}
281299

282300
$content->setId((int)$decodedData['id']);
283-
$content->setType($decodedData['type']);
284301
$content->setTitle((string)$decodedData['title']);
285302
$content->setUrl((string)$decodedData['_links']['self']);
286303
if (isset($decodedData['space']['key'])) {
@@ -289,8 +306,9 @@ private function deserializeContent(array $decodedData): AbstractContent
289306
if (isset($decodedData['version']['number'])) {
290307
$content->setVersion((int)$decodedData['version']['number']);
291308
}
292-
if(isset($decodedData['body']['storage']['value'])) {
293-
$content->setContent($decodedData['body']['storage']['value']);
309+
if(isset($decodedData['body']['storage']['value']) ) {
310+
assert(is_array($decodedData['body']['storage']));
311+
$content->setContent((string)$decodedData['body']['storage']['value']);
294312
}
295313

296314
return $content;

src/Entity/AbstractContent.php

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
namespace CloudPlayDev\ConfluenceClient\Entity;
55

66

7+
use CloudPlayDev\ConfluenceClient\Api\Content;
8+
79
abstract class AbstractContent
810
{
911
private ?int $id = null;
@@ -18,12 +20,15 @@ abstract class AbstractContent
1820
*/
1921
private array $children = [];
2022
private ?string $url = null;
21-
private ?string $type = null;
23+
protected string $type = Content::CONTENT_TYPE_GLOBAL;
24+
25+
private ?int $containerId = null;
26+
private string $containerType = Content::CONTENT_TYPE_PAGE;
2227

2328
/**
24-
* @return null|string
29+
* @return string
2530
*/
26-
public function getType(): ?string
31+
public function getType(): string
2732
{
2833
return $this->type;
2934
}
@@ -160,4 +165,65 @@ public function setUrl(string $url): self
160165
$this->url = $url;
161166
return $this;
162167
}
168+
169+
/**
170+
* @param string $comment
171+
* @return ContentComment
172+
*/
173+
public function createComment(string $comment): ContentComment
174+
{
175+
$contentComment = new ContentComment();
176+
$contentComment->setContainerId($this->getId());
177+
$contentComment->setContainerType($this->getType());
178+
$contentComment->setContent($comment);
179+
return $contentComment;
180+
}
181+
182+
/**
183+
* @param string $title
184+
* @param string $body
185+
* @return ContentPage
186+
*/
187+
public function createSubpage(string $title, string $body): ContentPage
188+
{
189+
$contentPage = new ContentPage();
190+
$contentPage->setContainerId($this->getId());
191+
$contentPage->setContainerType($this->getType());
192+
$contentPage->setContent($body);
193+
$contentPage->setTitle($title);
194+
return $contentPage;
195+
}
196+
197+
/**
198+
* @return int|null
199+
*/
200+
public function getContainerId(): ?int
201+
{
202+
return $this->containerId;
203+
}
204+
205+
/**
206+
* @param int|null $containerId
207+
*/
208+
public function setContainerId(?int $containerId): void
209+
{
210+
$this->containerId = $containerId;
211+
}
212+
213+
/**
214+
* @return string
215+
*/
216+
public function getContainerType(): string
217+
{
218+
return $this->containerType;
219+
}
220+
221+
/**
222+
* @param string $containerType
223+
*/
224+
public function setContainerType(string $containerType): void
225+
{
226+
$this->containerType = $containerType;
227+
}
228+
163229
}

src/Entity/ContentComment.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
namespace CloudPlayDev\ConfluenceClient\Entity;
55

66

7+
use CloudPlayDev\ConfluenceClient\Api\Content;
8+
79
class ContentComment extends AbstractContent
810
{
11+
protected string $type = Content::CONTENT_TYPE_COMMENT;
912

1013
}

src/Entity/ContentPage.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@
99

1010
namespace CloudPlayDev\ConfluenceClient\Entity;
1111

12-
class ContentPage extends AbstractContent{}
12+
use CloudPlayDev\ConfluenceClient\Api\Content;
13+
14+
class ContentPage extends AbstractContent{
15+
16+
protected string $type = Content::CONTENT_TYPE_PAGE;
17+
}

0 commit comments

Comments
 (0)