Add JSON serialize methods

Change-Id: Iaa24c39842cbe76370aaeba01eab2c991d290b8c
This commit is contained in:
Ed Sanders 2020-09-16 13:06:14 +01:00
parent 221f5c5884
commit 2a03b6e420
3 changed files with 35 additions and 1 deletions

View file

@ -35,6 +35,16 @@ class CommentItem extends ThreadItem {
$this->author = $author;
}
/**
* @return array JSON-serializable array
*/
public function jsonSerialize() : array {
return array_merge( parent::jsonSerialize(), [
'timestamp' => $this->timestamp,
'author' => $this->author,
] );
}
/**
* Get the HTML of this comment's body
*

View file

@ -16,6 +16,15 @@ class HeadingItem extends ThreadItem {
$this->placeholderHeading = $placeholderHeading;
}
/**
* @return array JSON-serializable array
*/
public function jsonSerialize() : array {
return array_merge( parent::jsonSerialize(), [
'placeholderHeading' => $this->placeholderHeading,
] );
}
/**
* @return bool
*/

View file

@ -3,12 +3,13 @@
namespace MediaWiki\Extension\DiscussionTools;
use DOMNode;
use JsonSerializable;
use Wikimedia\Parsoid\Utils\DOMCompat;
/**
* A thread item, either a heading or a comment
*/
abstract class ThreadItem {
abstract class ThreadItem implements JsonSerializable {
protected $type;
protected $range;
protected $rootNode;
@ -31,6 +32,20 @@ abstract class ThreadItem {
$this->range = $range;
}
/**
* @return array JSON-serializable array
*/
public function jsonSerialize() : array {
return [
'type' => $this->type,
'level' => $this->level,
'id' => $this->id,
'replies' => array_map( function ( CommentItem $comment ) {
return $comment->getId();
}, $this->replies )
];
}
/**
* Get the list of authors in the comment tree below this thread item.
*