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 | * 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 | |
23 | namespace Wikimedia\JsonCodec; |
24 | |
25 | /** |
26 | * This is a simple class codec used for `stdClass` objects. |
27 | * @internal |
28 | */ |
29 | // This class should @implements JsonClassCodec<stdClass> but phan's incomplete |
30 | // support for generics doesn't allow that yet. |
31 | class JsonStdClassCodec 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 | return (array)$obj; |
45 | } |
46 | |
47 | /** |
48 | * Creates a new instance of the given class and initializes it from the |
49 | * $json array, using a static method on $className. |
50 | * @param class-string<T> $className |
51 | * @param array $json |
52 | * @return T |
53 | * @inheritDoc |
54 | * @phan-template T |
55 | */ |
56 | public function newFromJsonArray( string $className, array $json ) { |
57 | // @phan-suppress-next-line PhanTypeMismatchReturn inadequate generics |
58 | return (object)$json; |
59 | } |
60 | |
61 | /** |
62 | * Returns null, to indicate no type hint for any properties in the |
63 | * `stdClass` value being encoded. |
64 | * |
65 | * @param class-string<T> $className |
66 | * @param string $keyName |
67 | * @return null Always returns null |
68 | */ |
69 | public function jsonClassHintFor( string $className, string $keyName ) { |
70 | return null; |
71 | } |
72 | |
73 | /** |
74 | * Return a singleton instance of this stdClass codec. |
75 | * @return JsonStdClassCodec a singleton instance of this class |
76 | */ |
77 | public static function getInstance(): JsonStdClassCodec { |
78 | static $instance = null; |
79 | if ( $instance == null ) { |
80 | $instance = new JsonStdClassCodec(); |
81 | } |
82 | return $instance; |
83 | } |
84 | } |