Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 7 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| JsonStaticClassCodec | |
0.00% |
0 / 7 |
|
0.00% |
0 / 4 |
30 | |
0.00% |
0 / 1 |
| toJsonArray | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| newFromJsonArray | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| jsonClassHintFor | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getInstance | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | |
| 4 | /** |
| 5 | * @license GPL-2.0-or-later |
| 6 | * @file |
| 7 | * @ingroup Json |
| 8 | */ |
| 9 | |
| 10 | namespace Wikimedia\JsonCodec; |
| 11 | |
| 12 | /** |
| 13 | * This is a simple class codec which proxies to methods on the object for |
| 14 | * serialization and a static method on the class for deserialization. |
| 15 | * It is intended for use as a singleton helper to JsonCodecableTrait. |
| 16 | * |
| 17 | * @implements JsonClassCodec<JsonCodecableTrait> |
| 18 | */ |
| 19 | class JsonStaticClassCodec implements JsonClassCodec { |
| 20 | |
| 21 | /** |
| 22 | * Returns a JSON array representing the contents of the given object, that |
| 23 | * can be deserialized with the corresponding newFromJsonArray() method, |
| 24 | * using a ::toJsonArray() method on the object itself. |
| 25 | * |
| 26 | * @param object $obj An object of the type handled by this JsonClassCodec |
| 27 | * @return array A Json representation of the object. |
| 28 | * @inheritDoc |
| 29 | * @see JsonCodecableTrait |
| 30 | */ |
| 31 | public function toJsonArray( $obj ): array { |
| 32 | // Proxy to a method on the object itself. |
| 33 | // @see JsonCodecableTrait |
| 34 | return $obj->toJsonArray(); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Creates a new instance of the given class and initializes it from the |
| 39 | * $json array, using a static method on $className. |
| 40 | * |
| 41 | * @inheritDoc |
| 42 | * @see JsonCodecableTrait |
| 43 | */ |
| 44 | public function newFromJsonArray( string $className, array $json ) { |
| 45 | // Proxy to a static method on the class. |
| 46 | // @see JsonCodecableTrait |
| 47 | return $className::newFromJsonArray( $json ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Return an optional type hint for the given array key in the result of |
| 52 | * ::toJsonArray() / input to ::newFromJsonArray. If a class name is |
| 53 | * returned here and it matches the runtime type of the value of that |
| 54 | * array key, then type information will be omitted from the generated |
| 55 | * JSON which can save space. The class name can be suffixed with `[]` |
| 56 | * to indicate an array or list containing objects of the given class |
| 57 | * name. |
| 58 | * |
| 59 | * @param class-string<T> $className |
| 60 | * @param string $keyName |
| 61 | * @return class-string|string|Hint|null A class string, Hint or null. |
| 62 | * For backward compatibility, a class string suffixed with `[]` can |
| 63 | * also be returned, but that is deprecated. |
| 64 | */ |
| 65 | public function jsonClassHintFor( string $className, string $keyName ) { |
| 66 | // Proxy to a static method on the class. |
| 67 | // @see JsonCodecableTrait |
| 68 | return $className::jsonClassHintFor( $keyName ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Return a singleton instance of this class codec. |
| 73 | * @return JsonStaticClassCodec a singleton instance of this class |
| 74 | */ |
| 75 | public static function getInstance(): JsonStaticClassCodec { |
| 76 | static $instance = null; |
| 77 | if ( $instance == null ) { |
| 78 | $instance = new JsonStaticClassCodec(); |
| 79 | } |
| 80 | return $instance; |
| 81 | } |
| 82 | } |