@@ -39,6 +39,16 @@ class Content extends AbstractApi
39
39
*/
40
40
public const CONTENT_TYPE_PAGE = 'page ' ;
41
41
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
+
42
52
/**
43
53
* @param string|int|null ...$parameter
44
54
* @return string
@@ -65,7 +75,7 @@ private static function getContentUri(...$parameter): string
65
75
*/
66
76
public function findOneById (int $ contentId ): AbstractContent
67
77
{
68
- $ response = $ this ->get (self ::getContentUri ($ contentId ), ['expand ' => ' space,version,body.storage ' ]);
78
+ $ response = $ this ->get (self ::getContentUri ($ contentId ), ['expand ' => self :: DEFAULT_EXPAND ]);
69
79
70
80
if ($ response ->getStatusCode () !== 200 ) {
71
81
throw new RequestException ($ response );
@@ -75,61 +85,69 @@ public function findOneById(int $contentId): AbstractContent
75
85
}
76
86
77
87
/**
78
- * @param AbstractContent $page
88
+ * @param AbstractContent $content
79
89
* @return ResponseInterface
80
90
* @throws Exception
81
91
* @throws JsonException
82
92
* @throws HttpClientException
83
93
*/
84
- public function update (AbstractContent $ page ): ResponseInterface
94
+ public function update (AbstractContent $ content ): ResponseInterface
85
95
{
86
- $ contentId = $ page ->getId ();
96
+ $ contentId = $ content ->getId ();
87
97
if (null === $ contentId ) {
88
- throw new Exception ('Only saved pages can be updated. ' );
98
+ throw new Exception ('Only saved content can be updated. ' );
89
99
}
90
100
$ data = [
91
101
'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 ()],
95
105
'body ' => [
96
106
'storage ' => [
97
- 'value ' => $ page ->getContent (),
107
+ 'value ' => $ content ->getContent (),
98
108
'representation ' => 'storage ' ,
99
109
],
100
110
],
101
- 'version ' => ['number ' => $ page ->getVersion () + 1 ]
111
+ 'version ' => ['number ' => $ content ->getVersion () + 1 ]
102
112
];
103
113
104
114
return $ this ->put (self ::getContentUri ($ contentId ), $ data );
105
115
106
116
}
107
117
108
118
/**
109
- * @param AbstractContent $page
119
+ * @param AbstractContent $content
110
120
* @return AbstractContent
111
121
* @throws Exception
112
122
* @throws HttpClientException
113
123
* @throws JsonException
114
124
*/
115
- public function create (AbstractContent $ page ): AbstractContent
125
+ public function create (AbstractContent $ content ): AbstractContent
116
126
{
117
- if (null !== $ page ->getId ()) {
127
+ if (null !== $ content ->getId ()) {
118
128
throw new Exception ('Only new pages can be created. ' );
119
129
}
120
130
121
131
$ 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 ()],
125
135
'body ' => [
126
136
'storage ' => [
127
- 'value ' => $ page ->getContent (),
137
+ 'value ' => $ content ->getContent (),
128
138
'representation ' => 'storage ' ,
129
139
],
130
140
],
131
141
];
132
142
143
+ /* attach content to content */
144
+ if (null !== $ content ->getContainerId ()) {
145
+ $ data ['container ' ] = [
146
+ 'id ' => $ content ->getContainerId (),
147
+ 'type ' => $ content ->getContainerType (),
148
+ ];
149
+ }
150
+
133
151
$ response = $ this ->post (self ::getContentUri (), $ data );
134
152
135
153
if ($ response ->getStatusCode () !== 200 ) {
@@ -141,16 +159,16 @@ public function create(AbstractContent $page): AbstractContent
141
159
}
142
160
143
161
/**
144
- * @param AbstractContent $page
162
+ * @param AbstractContent $content
145
163
* @return ResponseInterface
146
164
*/
147
- public function remove (AbstractContent $ page ): ResponseInterface
165
+ public function remove (AbstractContent $ content ): ResponseInterface
148
166
{
149
- $ contentId = $ page ->getId ();
167
+ $ contentId = $ content ->getId ();
150
168
if (null === $ contentId ) {
151
169
throw new Exception ('Only saved pages can be removed. ' );
152
170
}
153
- return $ this ->put (self ::getContentUri ($ contentId ));
171
+ return $ this ->delete (self ::getContentUri ($ contentId ));
154
172
}
155
173
156
174
/**
@@ -165,7 +183,7 @@ public function children(AbstractContent $content, ?string $contentType = null):
165
183
return $ this ->parseSearchResults (
166
184
$ this ->get (
167
185
self ::getContentUri ($ content ->getId (), 'child ' , $ contentType ),
168
- ['expand ' => ' space,version,body.storage ' ]
186
+ ['expand ' => self :: DEFAULT_EXPAND ]
169
187
),
170
188
);
171
189
}
@@ -195,7 +213,7 @@ public function findOneBy(array $searchParameter): ?AbstractContent
195
213
return in_array ($ searchKey , $ allowedSearchParameter , true );
196
214
}, ARRAY_FILTER_USE_KEY );
197
215
198
- $ queryParameter ['expand ' ] = ' space,version,body.storage ' ;
216
+ $ queryParameter ['expand ' ] = self :: DEFAULT_EXPAND ;
199
217
200
218
$ searchResponse = $ this ->get ('content? ' , $ queryParameter );
201
219
@@ -280,7 +298,6 @@ private function deserializeContent(array $decodedData): AbstractContent
280
298
}
281
299
282
300
$ content ->setId ((int )$ decodedData ['id ' ]);
283
- $ content ->setType ($ decodedData ['type ' ]);
284
301
$ content ->setTitle ((string )$ decodedData ['title ' ]);
285
302
$ content ->setUrl ((string )$ decodedData ['_links ' ]['self ' ]);
286
303
if (isset ($ decodedData ['space ' ]['key ' ])) {
@@ -289,8 +306,9 @@ private function deserializeContent(array $decodedData): AbstractContent
289
306
if (isset ($ decodedData ['version ' ]['number ' ])) {
290
307
$ content ->setVersion ((int )$ decodedData ['version ' ]['number ' ]);
291
308
}
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 ' ]);
294
312
}
295
313
296
314
return $ content ;
0 commit comments