Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
JsonCodecableTrait
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 2
6
0.00% covered (danger)
0.00%
0 / 1
 jsonClassCodec
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 toJsonArray
n/a
0 / 0
n/a
0 / 0
0
 newFromJsonArray
n/a
0 / 0
n/a
0 / 0
0
 jsonClassHintFor
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types=1 );
3
4/**
5 * @license GPL-2.0-or-later
6 * @file
7 */
8
9namespace Wikimedia\JsonCodec;
10
11use Psr\Container\ContainerInterface;
12use stdClass;
13
14/**
15 * The JsonCodecableTrait aids in the implementation of stateless codecs.
16 * The class using the trait need only define stateless ::toJsonArray() and
17 * ::newFromJsonArray() methods.
18 *
19 * The class using the trait should also implement JsonCodecable
20 * (https://wiki.php.net/rfc/traits-with-interfaces may allow the trait
21 * to do this directly in a future PHP version).
22 */
23trait JsonCodecableTrait {
24
25    /**
26     * Implements JsonCodecable by providing an implementation of
27     * ::jsonClassCodec() which does not use the provided $serviceContainer
28     * nor does it maintain any state; it just calls the ::toJsonArray()
29     * and ::newFromJsonArray() methods of this instance.
30     * @param JsonCodecInterface $codec
31     * @param ContainerInterface $serviceContainer
32     * @return JsonClassCodec
33     */
34    public static function jsonClassCodec(
35        JsonCodecInterface $codec, ContainerInterface $serviceContainer
36    ): JsonClassCodec {
37        // In advanced JIT implementations optimization of the method
38        // dispatch in this class can be performed if we keep the
39        // codecs for each class separate.  However, for simplicity
40        // (and to reduce memory usage) we'll use a singleton object
41        // shared with all classes which use this trait.
42        return JsonStaticClassCodec::getInstance();
43    }
44
45    /**
46     * Return an associative array representing the contents of this object,
47     * which can be passed to ::newFromJsonArray() to deserialize it.
48     * @return array
49     */
50    abstract public function toJsonArray(): array;
51
52    /**
53     * Return an instance of this object representing the deserialization
54     * from the array passed in $json.
55     * @param array $json
56     * @return stdClass
57     */
58    abstract public static function newFromJsonArray( array $json );
59
60    /**
61     * Return an optional type hint for the given array key in the result of
62     * ::toJsonArray() / input to ::newFromJsonArray.  If a class name is
63     * returned here and it matches the runtime type of the value of that
64     * array key, then type information will be omitted from the generated
65     * JSON which can save space.  The class name can be suffixed with `[]`
66     * to indicate an array or list containing objects of the given class
67     * name.
68     *
69     * Default implementation of ::jsonClassHintFor() provides no hints.
70     * Implementer can override.
71     *
72     * @param string $keyName
73     * @return class-string|string|Hint|null A class string, Hint, or null.
74     *   For backward compatibility, a class string suffixed with `[]` can
75     *   also be returned, but that is deprecated.
76     */
77    public static function jsonClassHintFor( string $keyName ) {
78        return null;
79    }
80}