MediaWiki master
ContentJsonCodec.php
Go to the documentation of this file.
1<?php
2declare( strict_types = 1 );
8namespace MediaWiki\Content;
9
10use Wikimedia\JsonCodec\JsonClassCodec;
11
19class ContentJsonCodec implements JsonClassCodec {
20
21 public function __construct(
22 private IContentHandlerFactory $contentHandlerFactory
23 ) {
24 }
25
27 public function toJsonArray( $obj ): array {
28 // To serialize content we need a handler.
29 $model = $obj->getModel();
30 $handler = $this->contentHandlerFactory->getContentHandler(
31 $model
32 );
33 return [ 'model' => $model ] + $handler->serializeContentToJsonArray( $obj );
34 }
35
37 public function newFromJsonArray( string $className, array $json ) {
38 // To deserialize content we need a handler.
39 $model = $json['model'];
40 $handler = $this->contentHandlerFactory->getContentHandler(
41 $model
42 );
43 $content = $handler->deserializeContentFromJsonArray( $json );
44 return $content;
45 }
46
48 public function jsonClassHintFor( string $className, string $keyName ) {
49 return null;
50 }
51}
ContentJsonCodec handles serialization of Content objects to/from JSON using methods of the appropria...
newFromJsonArray(string $className, array $json)
__construct(private IContentHandlerFactory $contentHandlerFactory)
jsonClassHintFor(string $className, string $keyName)