Debian Codegraph @

includes/content/ContentJsonCodec.php

raw symbols SCIP
1 <?php
2 declare( strict_types = 1 );
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 */
21
22 namespace MediaWiki\Content;
23
24 use Wikimedia\JsonCodec\JsonClassCodec;
25
26 /**
27 * ContentJsonCodec handles serialization of Content objects to/from
28 * JSON using methods of the appropriate ContentHandler.
29 *
30 * @template-implements JsonClassCodec<Content>
31 * @internal
32 */
33 class ContentJsonCodec implements JsonClassCodec {
34
35 public function __construct(
36 private IContentHandlerFactory $contentHandlerFactory
37 ) {
38 }
39
40 /** @inheritDoc */
41 public function toJsonArray( $content ): array {
42 '@phan-var Content $content'; /** @var Content $content */
43 // To serialize content we need a handler.
44 $model = $content->getModel();
45 $handler = $this->contentHandlerFactory->getContentHandler(
46 $model
47 );
48 return [ 'model' => $model ] +
49 $handler->serializeContentToJsonArray( $content );
50 }
51
52 /** @inheritDoc */
53 public function newFromJsonArray( string $className, array $json ) {
54 // To deserialize content we need a handler.
55 $model = $json['model'];
56 $handler = $this->contentHandlerFactory->getContentHandler(
57 $model
58 );
59 $content = $handler->deserializeContentFromJsonArray( $json );
60 // phan's support for generics is broken :(
61 // @phan-suppress-next-line PhanTypeMismatchReturn
62 return $content;
63 }
64
65 /** @inheritDoc */
66 public function jsonClassHintFor( string $className, string $keyName ) {
67 return null;
68 }
69 }