Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| CompatJsonCodec | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 1 |
| codecFor | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | declare( 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 | */ |
| 22 | namespace Wikimedia\Parsoid\Utils; |
| 23 | |
| 24 | use JsonSerializable; |
| 25 | use Wikimedia\JsonCodec\JsonClassCodec; |
| 26 | use 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 | */ |
| 40 | class 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 | $codec = new class() implements JsonClassCodec { |
| 46 | /** @inheritDoc */ |
| 47 | public function toJsonArray( $obj ): array { |
| 48 | return $obj->jsonSerialize(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @param class-string $className |
| 53 | * @param array $json |
| 54 | * @return never |
| 55 | */ |
| 56 | public function newFromJsonArray( string $className, array $json ) { |
| 57 | // We can't use the core JsonUnserializable interface |
| 58 | // (even blindly) because we can't make a non-null |
| 59 | // JsonUnserializer which is required as the first argument |
| 60 | // T346829, T327439#8634426 |
| 61 | // That's ok, though, we can still *serialize* objects for |
| 62 | // test cases or logging even if we can't unserialize them. |
| 63 | throw new \InvalidArgumentException( "Unserialization of this $className not possible" ); |
| 64 | } |
| 65 | |
| 66 | /** @inheritDoc */ |
| 67 | public function jsonClassHintFor( string $className, string $keyName ) { |
| 68 | return null; |
| 69 | } |
| 70 | }; |
| 71 | // Cache this for future use |
| 72 | $this->addCodecFor( $className, $codec ); |
| 73 | } |
| 74 | return $codec; |
| 75 | } |
| 76 | } |