@@ -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 ;
0 commit comments