44namespace CloudPlayDev \ConfluenceClient \Api ;
55
66use CloudPlayDev \ConfluenceClient \Entity \AbstractContent ;
7+ use CloudPlayDev \ConfluenceClient \Entity \ContentHistory ;
78use CloudPlayDev \ConfluenceClient \Entity \ContentSearchResult ;
89use CloudPlayDev \ConfluenceClient \Entity \ContentBody ;
10+ use CloudPlayDev \ConfluenceClient \Entity \Hydratable ;
911use CloudPlayDev \ConfluenceClient \Exception \ConfluencePhpClientException ;
1012use Http \Client \Exception as HttpClientException ;
1113use JsonException ;
@@ -43,19 +45,28 @@ class Content extends AbstractApi
4345 * default value for expand query parameter
4446 */
4547 private const DEFAULT_EXPAND = 'space,version,body.storage,container ' ;
48+ private const DEFAULT_HISTORY_EXPAND = 'content,content.space,content.version,content.body.storage,content.container ' ;
4649
4750 /**
4851 * @see https://docs.atlassian.com/atlassian-confluence/REST/6.6.0/#content-getContent
49- * @param int $contentId
50- * @return AbstractContent|null
5152 * @throws ConfluencePhpClientException
53+ * @throws HttpClientException
54+ * @throws JsonException
5255 */
53- public function get (int $ contentId ): ?AbstractContent
56+ public function get (int $ contentId, ? int $ version = null ): ?AbstractContent
5457 {
55- $ response = $ this ->httpGet (
56- self ::getRestfulUri ('content ' , $ contentId ),
57- ['expand ' => self ::DEFAULT_EXPAND ]
58- );
58+ $ fetchUri = ['content ' , $ contentId ];
59+ $ parameter = ['expand ' => self ::DEFAULT_EXPAND ];
60+
61+ if ($ version !== null ) {
62+ $ fetchUri [] = 'version ' ;
63+ $ fetchUri [] = $ version ;
64+
65+ $ parameter = ['expand ' => self ::DEFAULT_HISTORY_EXPAND ];
66+ }
67+
68+ $ response = $ this ->httpGet (self ::getRestfulUri (...$ fetchUri ), $ parameter );
69+
5970 return $ this ->hydrateResponse ($ response , AbstractContent::class);
6071 }
6172
@@ -69,7 +80,7 @@ public function get(int $contentId): ?AbstractContent
6980 public function find (array $ searchParameter ): ContentSearchResult
7081 {
7182 $ allowedSearchParameter = ['title ' , 'spaceKey ' , 'type ' , 'id ' ];
72- $ queryParameter = array_filter ($ searchParameter , static function (string $ searchKey ) use ($ allowedSearchParameter ) {
83+ $ queryParameter = array_filter ($ searchParameter , static function (string $ searchKey ) use ($ allowedSearchParameter ) {
7384 return in_array ($ searchKey , $ allowedSearchParameter , true );
7485 }, ARRAY_FILTER_USE_KEY );
7586
@@ -138,7 +149,7 @@ public function create(AbstractContent $content): AbstractContent
138149 ];
139150
140151 if (count ($ content ->getAncestors ()) > 0 ) {
141- $ ancestorsData = array_map (static function (int $ id ) {
152+ $ ancestorsData = array_map (static function (int $ id ) {
142153 return ['id ' => $ id ];
143154 }, $ content ->getAncestors ());
144155
@@ -236,4 +247,19 @@ public function convert(ContentBody $convertBody, string $to = 'view', ?Abstract
236247 );
237248
238249 }
250+
251+ /**
252+ * Returns the history of a particular piece of content, sorted by version number in descending order.
253+ *
254+ * @param int $contentId
255+ * @return Hydratable
256+ * @throws HttpClientException
257+ */
258+ public function history (int $ contentId ): Hydratable
259+ {
260+ return $ this ->hydrateResponse (
261+ $ this ->httpGet (self ::getRestfulUri ('content ' , $ contentId , 'history ' )),
262+ ContentHistory::class
263+ );
264+ }
239265}
0 commit comments