JsonCodec
Interfaces to serialize and deserialize PHP objects to/from JSON
Loading...
Searching...
No Matches
Wikimedia\JsonCodec\Hint Class Reference

Class hints with modifiers. More...

Public Member Functions

 __construct ( $parent, int $modifier=0)
 Create a new serialization class type hint.
 

Static Public Member Functions

static build (string $className, int ... $modifiers)
 Helper function to create nested hints.
 

Public Attributes

const DEFAULT = 0
 The default class hint behavior: an exact match for class name, and the serialization for an object will always use curly braces {} but the return value from ::toJsonArray() will always be an array.
 
const LIST = 1
 A list of the hinted type.
 
const STDCLASS = 2
 A map of the hinted type.
 
const USE_SQUARE = 3
 Prefer to use square brackets to serialize this object, when possible.
 
const ALLOW_OBJECT = 4
 Tweak the return type of JsonCodec::toJsonArray() to return a stdClass object instead of array where that makes it possible to generate curly braces instead of adding an extra JsonCodec::TYPE_ANNOTATION value.
 
const INHERITED = 5
 The value is an instanceof the hinted type, and the JsonClassCodec for the hinted type will be able to deserialize the object.
 
 $parent
 
int $modifier
 

Detailed Description

Class hints with modifiers.

@template T

Constructor & Destructor Documentation

◆ __construct()

Wikimedia\JsonCodec\Hint::__construct ( $parent,
int $modifier = 0 )

Create a new serialization class type hint.

Parameters
class-string<T>|Hint<T>$parent
int$modifierA hint modifier

Member Function Documentation

◆ build()

static Wikimedia\JsonCodec\Hint::build ( string $className,
int ... $modifiers )
static

Helper function to create nested hints.

For example, the Foo[][] type can be created as Hint::build(Foo::class, Hint:LIST, Hint::LIST).

Note that, in the grand (?) tradition of C-like types, modifiers are read right-to-left. That is, a "stdClass containing values which are lists of Foo" is written 'backwards' as: Hint::build(Foo::class, Hint::LIST, Hint::STDCLASS).

@phan-template T

Parameters
class-string<T>$className
int...$modifiers
Returns
class-string<T>|Hint<T>

Member Data Documentation

◆ ALLOW_OBJECT

const Wikimedia\JsonCodec\Hint::ALLOW_OBJECT = 4

Tweak the return type of JsonCodec::toJsonArray() to return a stdClass object instead of array where that makes it possible to generate curly braces instead of adding an extra JsonCodec::TYPE_ANNOTATION value.

Not compatible with USE_SQUARE.

◆ DEFAULT

const Wikimedia\JsonCodec\Hint::DEFAULT = 0

The default class hint behavior: an exact match for class name, and the serialization for an object will always use curly braces {} but the return value from ::toJsonArray() will always be an array.

This requires adding an explicit JsonCodec::TYPE_ANNOTATION element to lists even if proper type hints are supplied.

◆ INHERITED

const Wikimedia\JsonCodec\Hint::INHERITED = 5

The value is an instanceof the hinted type, and the JsonClassCodec for the hinted type will be able to deserialize the object.

This is useful for tagged objects of various kinds, where a superclass can look at the json data to determine which of its subclasses to instantiate. Note that in this case hints will be taken from the superclass's codec.

◆ STDCLASS

const Wikimedia\JsonCodec\Hint::STDCLASS = 2

A map of the hinted type.

The value is a stdClass object with string keys and property values of the specified type.

◆ USE_SQUARE

const Wikimedia\JsonCodec\Hint::USE_SQUARE = 3

Prefer to use square brackets to serialize this object, when possible.

Not compatible with ALLOW_OBJECT.


The documentation for this class was generated from the following file: