Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
CompatJsonCodec
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 codecFor
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2declare( strict_types=1 );
3
4/**
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22namespace Wikimedia\Parsoid\Utils;
23
24use JsonSerializable;
25use Wikimedia\JsonCodec\JsonClassCodec;
26use Wikimedia\JsonCodec\JsonCodec;
27
28/**
29 * This is a "compatible" JSON codec for the use of Parsoid test runners, etc.
30 * In addition to supporting objects which implement `JsonCodecable`, it
31 * tries to handle objects we might get from mediawiki-core which implement
32 * JsonSerializable and other legacy serialization types.
33 *
34 * This should not be relied on for production!
35 *
36 * However, it is good enough to use in test cases, etc, and hopefully makes
37 * them a little bit less fragile by not blowing up if it gets a martian
38 * object from mediawiki-core stuck into the parser's extension data.
39 */
40class CompatJsonCodec extends JsonCodec {
41    /** @inheritDoc */
42    protected function codecFor( string $className ): ?JsonClassCodec {
43        $codec = parent::codecFor( $className );
44        if ( $codec === null && is_a( $className, JsonSerializable::class, true ) ) {
45            /** @implements JsonClassCodec<JsonSerializable> */
46            $codec = new class() implements JsonClassCodec {
47                /** @inheritDoc */
48                public function toJsonArray( $obj ): array {
49                    return $obj->jsonSerialize();
50                }
51
52                /**
53                 * @param class-string $className
54                 * @param array $json
55                 * @return never
56                 */
57                public function newFromJsonArray( string $className, array $json ) {
58                    // We can't use the core JsonUnserializable interface
59                    // (even blindly) because we can't make a non-null
60                    // JsonUnserializer which is required as the first argument
61                    // T346829, T327439#8634426
62                    // That's ok, though, we can still *serialize* objects for
63                    // test cases or logging even if we can't unserialize them.
64                    throw new \InvalidArgumentException( "Unserialization of this $className not possible" );
65                }
66
67                /** @inheritDoc */
68                public function jsonClassHintFor( string $className, string $keyName ) {
69                    return null;
70                }
71            };
72            // Cache this for future use
73            $this->addCodecFor( $className, $codec );
74        }
75        return $codec;
76    }
77}