Debian Codegraph @

vendor/wikimedia/json-codec/src/Hint.php

raw
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 use Stringable;
26
27 /**
28 * Class hints with modifiers.
29 * @template T
30 */
31 class Hint implements Stringable {
32 /**
33 * The default class hint behavior: an exact match for class name,
34 * and the serialization for an object will always use curly
35 * braces `{}` but the return value from `::toJsonArray()` will
36 * always be an array. This requires adding an explicit
37 * `JsonCodec::TYPE_ANNOTATION` element to lists even if proper
38 * type hints are supplied.
39 */
40 public const DEFAULT = 0;
41 /**
42 * A list of the hinted type.
43 */
44 public const LIST = 1;
45 /**
46 * A map of the hinted type. The value is a stdClass object with
47 * string keys and property values of the specified type.
48 */
49 public const STDCLASS = 2;
50 /**
51 * Prefer to use square brackets to serialize this object, when
52 * possible. Not compatible with `ALLOW_OBJECT`.
53 */
54 public const USE_SQUARE = 3;
55 /**
56 * Tweak the return type of `JsonCodec::toJsonArray()` to return
57 * a `stdClass` object instead of array where that makes it possible
58 * to generate curly braces instead of adding an extra
59 * `JsonCodec::TYPE_ANNOTATION` value. Not compatible with `USE_SQUARE`.
60 */
61 public const ALLOW_OBJECT = 4;
62 /**
63 * The value is an `instanceof` the hinted type, and the
64 * `JsonClassCodec` for the hinted type will be able to
65 * deserialize the object. This is useful for tagged objects of
66 * various kinds, where a superclass can look at the json data to
67 * determine which of its subclasses to instantiate. Note that in
68 * this case hints will be taken from the superclass's codec.
69 */
70 public const INHERITED = 5;
71 /**
72 * Mark the supplied hint for use only during deserialization
73 * (`JsonCodec::newFromJsonArray`). The full class information
74 * will still be recorded during serialization (`::toJsonArray`).
75 * This allows the hint to be used for forward-compatibility
76 * with a future release that will utilize implicit class
77 * information, without harming backward-compatibility by
78 * (yet) omitting the explicit class information.
79 */
80 public const ONLY_FOR_DECODE = 6;
81
82 /** @var class-string<T>|Hint<T> */
83 public $parent;
84 public int $modifier;
85
86 /**
87 * Create a new serialization class type hint.
88 * @param class-string<T>|Hint<T> $parent
89 * @param int $modifier A hint modifier
90 */
91 public function __construct( $parent, int $modifier = 0 ) {
92 $this->parent = $parent;
93 $this->modifier = $modifier;
94 }
95
96 /**
97 * Helper function to create nested hints. For example, the
98 * `Foo[][]` type can be created as
99 * `Hint::build(Foo::class, Hint:LIST, Hint::LIST)`.
100 *
101 * Note that, in the grand (?) tradition of C-like types,
102 * modifiers are read right-to-left. That is, a "stdClass containing
103 * values which are lists of Foo" is written 'backwards' as:
104 * `Hint::build(Foo::class, Hint::LIST, Hint::STDCLASS)`.
105 *
106 * @phan-template T
107 * @param class-string<T> $className
108 * @param int ...$modifiers
109 * @return class-string<T>|Hint<T>
110 */
111 public static function build( string $className, int ...$modifiers ) {
112 if ( count( $modifiers ) === 0 ) {
113 return $className;
114 }
115 $last = array_pop( $modifiers );
116 return new Hint( self::build( $className, ...$modifiers ), $last );
117 }
118
119 public function __toString(): string {
120 $parent = strval( $this->parent );
121 switch ( $this->modifier ) {
122 case self::DEFAULT:
123 return "DEFAULT($parent)";
124 case self::LIST:
125 return "LIST($parent)";
126 case self::STDCLASS:
127 return "STDCLASS($parent)";
128 case self::USE_SQUARE:
129 return "USE_SQUARE($parent)";
130 case self::ALLOW_OBJECT:
131 return "ALLOW_OBJECT($parent)";
132 case self::INHERITED:
133 return "INHERITED($parent)";
134 default:
135 return "UNKNOWN($parent)";
136 }
137 }
138 }