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 | * 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 | * @ingroup Json |
22 | */ |
23 | |
24 | namespace Wikimedia\JsonCodec; |
25 | |
26 | /** |
27 | * This is a simple class codec which proxies to methods on the object for |
28 | * serialization and a static method on the class for deserialization. |
29 | * It is intended for use as a singleton helper to JsonCodecableTrait. |
30 | */ |
31 | class JsonStaticClassCodec implements JsonClassCodec { |
32 | |
33 | /** |
34 | * Returns a JSON array representing the contents of the given object, that |
35 | * can be deserialized with the corresponding newFromJsonArray() method, |
36 | * using a ::toJsonArray() method on the object itself. |
37 | * |
38 | * @param object $obj An object of the type handled by this JsonClassCodec |
39 | * @return array A Json representation of the object. |
40 | * @inheritDoc |
41 | * @see JsonCodecableTrait |
42 | */ |
43 | public function toJsonArray( $obj ): array { |
44 | // Proxy to a method on the object itself. |
45 | // @see JsonCodecableTrait |
46 | return $obj->toJsonArray(); |
47 | } |
48 | |
49 | /** |
50 | * Creates a new instance of the given class and initializes it from the |
51 | * $json array, using a static method on $className. |
52 | * @param class-string<T> $className |
53 | * @param array $json |
54 | * @return T |
55 | * @inheritDoc |
56 | * @phan-template T |
57 | * @see JsonCodecableTrait |
58 | */ |
59 | public function newFromJsonArray( string $className, array $json ) { |
60 | // Proxy to a static method on the class. |
61 | // @see JsonCodecableTrait |
62 | return $className::newFromJsonArray( $json ); |
63 | } |
64 | |
65 | /** |
66 | * Return an optional type hint for the given array key in the result of |
67 | * ::toJsonArray() / input to ::newFromJsonArray. If a class name is |
68 | * returned here and it matches the runtime type of the value of that |
69 | * array key, then type information will be omitted from the generated |
70 | * JSON which can save space. The class name can be suffixed with `[]` |
71 | * to indicate an array or list containing objects of the given class |
72 | * name. |
73 | * |
74 | * @param class-string<T> $className |
75 | * @param string $keyName |
76 | * @return class-string|string|null A class string, a class string suffixed |
77 | * with `[]`, or null |
78 | */ |
79 | public function jsonClassHintFor( string $className, string $keyName ): ?string { |
80 | // Proxy to a static method on the class. |
81 | // @see JsonCodecableTrait |
82 | return $className::jsonClassHintFor( $keyName ); |
83 | } |
84 | |
85 | /** |
86 | * Return a singleton instance of this class codec. |
87 | * @return JsonStaticClassCodec a singleton instance of this class |
88 | */ |
89 | public static function getInstance(): JsonStaticClassCodec { |
90 | static $instance = null; |
91 | if ( $instance == null ) { |
92 | $instance = new JsonStaticClassCodec(); |
93 | } |
94 | return $instance; |
95 | } |
96 | } |