Debian Codegraph @

extensions/DiscussionTools/includes/ThreadItem/ThreadItemTrait.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\DiscussionTools\ThreadItem;
4
5 trait ThreadItemTrait {
6 // phpcs:disable Squiz.WhiteSpace, MediaWiki.Commenting
7 // Required ThreadItem methods (listed for Phan)
8 abstract public function getId(): string;
9 abstract public function getType(): string;
10 abstract public function getReplies(): array;
11 abstract public function getLevel(): int;
12 // phpcs:enable
13
14 /**
15 * @param bool $deep Whether to include full serialized comments in the replies key
16 * @param callable|null $callback Function to call on the returned serialized array, which
17 * will be passed into the serialized replies as well if $deep is used
18 * @return array JSON-serializable array
19 */
20 public function jsonSerialize( bool $deep = false, ?callable $callback = null ): array {
21 // The output of this method can end up in the HTTP cache (Varnish). Avoid changing it;
22 // and when doing so, ensure that frontend code can handle both the old and new outputs.
23 // See ThreadItem.static.newFromJSON in JS.
24
25 $array = [
26 'type' => $this->getType(),
27 'level' => $this->getLevel(),
28 'id' => $this->getId(),
29 'replies' => array_map( static function ( ThreadItem $comment ) use ( $deep, $callback ) {
30 return $deep ? $comment->jsonSerialize( $deep, $callback ) : $comment->getId();
31 }, $this->getReplies() )
32 ];
33 if ( $callback ) {
34 $callback( $array, $this );
35 }
36 return $array;
37 }
38 }