Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
TocTopicListFormatter
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 formatApi
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace Flow\Formatter;
4
5use Flow\Data\Pager\PagerPage;
6use Flow\Model\Workflow;
7use Flow\Templating;
8
9/**
10 * The output of this is a strict subset of TopicListFormatter.
11 * Anything accessible from the output of this should be accessible with the same path
12 * from the output of TopicListFormatter.  However, this output is much more minimal.
13 */
14class TocTopicListFormatter extends BaseTopicListFormatter {
15    /**
16     * @var Templating
17     */
18    protected $templating;
19
20    public function __construct( Templating $templating ) {
21        $this->templating = $templating;
22    }
23
24    /**
25     * Formats the response
26     *
27     * @param Workflow $listWorkflow Workflow corresponding to board/list of topics
28     * @param array $topicRootRevisionsByWorkflowId Associative array mapping topic ID (in alphadecimal form)
29     *  to PostRevision for the topic root.
30     * @param array $workflowsByWorkflowId Associative array mapping topic ID (in alphadecimal form) to
31     *  workflow
32     * @param PagerPage $page page from query, to support pagination
33     *
34     * @return array Array formatted for response
35     */
36    public function formatApi(
37        Workflow $listWorkflow,
38        array $topicRootRevisionsByWorkflowId,
39        array $workflowsByWorkflowId,
40        PagerPage $page
41    ): array {
42        $result = $this->buildEmptyResult( $listWorkflow );
43
44        foreach ( $topicRootRevisionsByWorkflowId as $topicId => $postRevision ) {
45            $result['roots'][] = $topicId;
46            $revisionId = $postRevision->getRevisionId()->getAlphadecimal();
47            $result['posts'][$topicId] = [ $revisionId ];
48
49            $contentFormat = 'topic-title-wikitext';
50
51            $workflow = $workflowsByWorkflowId[$topicId];
52
53            $moderatedRevision = $this->templating->getModeratedRevision( $postRevision );
54            $moderationData = $moderatedRevision->isModerated() ?
55                [
56                    'isModerated' => true,
57                    'moderateState' => $moderatedRevision->getModerationState(),
58                ] :
59                [
60                    'isModerated' => false
61                ];
62            $result['revisions'][$revisionId] = [
63                // Keep this as a minimal subset of
64                // RevisionFormatter->formatApi, and keep the same content
65                // format for topic titles as specified in that class for
66                // topic titles.
67
68                'content' => [
69                    // no need to check permissions before fetching content; that should've
70                    // been done by whatever caller supplies $topicRootRevisionsByWorkflowId,
71                    'content' => $this->templating->getContent(
72                        $postRevision,
73                        $contentFormat
74                    ),
75                    'format' => $contentFormat,
76                    'plaintext' => $this->templating->getContent( $postRevision, 'topic-title-plaintext' )
77                ],
78                'last_updated' => $workflow->getLastUpdatedObj()->getTimestamp() * 1000,
79            ] + $moderationData;
80        }
81
82        $pagingOption = $page->getPagingLinksOptions();
83        $result['links']['pagination'] = $this->buildPaginationLinks(
84            $listWorkflow,
85            $pagingOption
86        );
87
88        return $result;
89    }
90}