Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hint
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 2
12
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 build
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2declare( 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
23namespace Wikimedia\JsonCodec;
24
25/**
26 * Class hints with modifiers.
27 * @template T
28 */
29class Hint {
30    /**
31     * The default class hint behavior: an exact match for class name,
32     * and the serialization for an object will always use curly
33     * braces `{}` but the return value from `::toJsonArray()` will
34     * always be an array.  This requires adding an explicit
35     * `JsonCodec::TYPE_ANNOTATION` element to lists even if proper
36     * type hints are supplied.
37     */
38    public const DEFAULT = 0;
39    /**
40     * A list of the hinted type.
41     */
42    public const LIST = 1;
43    /**
44     * A map of the hinted type.  The value is a stdClass object with
45     * string keys and property values of the specified type.
46     */
47    public const STDCLASS = 2;
48    /**
49     * Prefer to use square brackets to serialize this object, when
50     * possible. Not compatible with `ALLOW_OBJECT`.
51     */
52    public const USE_SQUARE = 3;
53    /**
54     * Tweak the return type of `JsonCodec::toJsonArray()` to return
55     * a `stdClass` object instead of array where that makes it possible
56     * to generate curly braces instead of adding an extra
57     * `JsonCodec::TYPE_ANNOTATION` value.  Not compatible with `USE_SQUARE`.
58     */
59    public const ALLOW_OBJECT = 4;
60    /**
61     * The value is an `instanceof` the hinted type, and the
62     * `JsonClassCodec` for the hinted type will be able to
63     * deserialize the object.  This is useful for tagged objects of
64     * various kinds, where a superclass can look at the json data to
65     * determine which of its subclasses to instantiate.  Note that in
66     * this case hints will be taken from the superclass's codec.
67     */
68    public const INHERITED = 5;
69
70    /** @var class-string<T>|Hint<T> */
71    public $parent;
72    public int $modifier;
73
74    /**
75     * Create a new serialization class type hint.
76     * @param class-string<T>|Hint<T> $parent
77     * @param int $modifier A hint modifier
78     */
79    public function __construct( $parent, int $modifier = 0 ) {
80        $this->parent = $parent;
81        $this->modifier = $modifier;
82    }
83
84    /**
85     * Helper function to create nested hints.  For example, the
86     * `Foo[][]` type can be created as
87     * `Hint::build(Foo::class, Hint:LIST, Hint::LIST)`.
88     *
89     * Note that, in the grand (?) tradition of C-like types,
90     * modifiers are read right-to-left.  That is, a "stdClass containing
91     * values which are lists of Foo" is written 'backwards' as:
92     * `Hint::build(Foo::class, Hint::LIST, Hint::STDCLASS)`.
93     *
94     * @phan-template T
95     * @param class-string<T> $className
96     * @param int ...$modifiers
97     * @return class-string<T>|Hint<T>
98     */
99    public static function build( string $className, int ...$modifiers ) {
100        if ( count( $modifiers ) === 0 ) {
101            return $className;
102        }
103        $last = array_pop( $modifiers );
104        return new Hint( self::build( $className, ...$modifiers ), $last );
105    }
106}