Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 7 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| JsonStdClassCodec | |
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 | */ |
| 8 | |
| 9 | namespace Wikimedia\JsonCodec; |
| 10 | |
| 11 | use stdClass; |
| 12 | |
| 13 | /** |
| 14 | * This is a simple class codec used for `stdClass` objects. |
| 15 | * |
| 16 | * @internal |
| 17 | * @implements JsonClassCodec<stdClass> |
| 18 | */ |
| 19 | class JsonStdClassCodec 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 stdClass $obj An object of the type handled by this JsonClassCodec |
| 27 | * @return array A Json representation of the object. |
| 28 | * @see JsonCodecableTrait |
| 29 | */ |
| 30 | public function toJsonArray( $obj ): array { |
| 31 | return (array)$obj; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Creates a new instance of the given class and initializes it from the |
| 36 | * $json array, using a static method on $className. |
| 37 | * |
| 38 | * @param class-string<stdClass> $className |
| 39 | * @param array $json |
| 40 | * @return stdClass |
| 41 | */ |
| 42 | public function newFromJsonArray( string $className, array $json ) { |
| 43 | return (object)$json; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns null, to indicate no type hint for any properties in the |
| 48 | * `stdClass` value being encoded. |
| 49 | * |
| 50 | * @param class-string<T> $className |
| 51 | * @param string $keyName |
| 52 | * @return null Always returns null |
| 53 | */ |
| 54 | public function jsonClassHintFor( string $className, string $keyName ) { |
| 55 | return null; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Return a singleton instance of this stdClass codec. |
| 60 | * @return JsonStdClassCodec a singleton instance of this class |
| 61 | */ |
| 62 | public static function getInstance(): JsonStdClassCodec { |
| 63 | static $instance = null; |
| 64 | if ( $instance == null ) { |
| 65 | $instance = new JsonStdClassCodec(); |
| 66 | } |
| 67 | return $instance; |
| 68 | } |
| 69 | } |