PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
Loading...
Searching...
No Matches
JsonMapper Class Reference
+ Collaboration diagram for JsonMapper:

Public Member Functions

 map ($json, $object)
 
 mapArray ($json, $array, $class=null, $parent_key='')
 
 setLogger ($logger)
 

Public Attributes

 $bEnforceMapType = true
 
 $bExceptionOnMissingData = false
 
 $bExceptionOnUndefinedProperty = false
 
 $bIgnoreVisibility = false
 
 $bRemoveUndefinedAttributes = false
 
 $bStrictNullTypes = true
 
 $bStrictObjectTypeChecking = false
 
 $classMap = array()
 
 $postMappingMethod = null
 
 $postMappingMethodArguments = array()
 
 $undefinedPropertyHandler = null
 

Protected Member Functions

 checkMissingData ($providedProperties, ReflectionClass $rc)
 
 createInstance ( $class, $useParameter=false, $jvalue=null)
 
 getCamelCaseName ($name)
 
 getFullNamespace ($type, $strNs)
 
 getMappedType ($type, $jvalue=null)
 
 getSafeName ($name)
 
 hasVariadicArrayType ($accessor)
 
 inspectProperty (ReflectionClass $rc, $name)
 
 isArrayOfType ($strType)
 
 isFlatType ($type)
 
 isNullable ($type)
 
 isObjectOfSameType ($type, $value)
 
 isSimpleType ($type)
 
 log ($level, $message, array $context=array())
 
 removeNullable ($type)
 
 removeUndefinedAttributes ($object, $providedProperties)
 
 setProperty ( $object, $accessor, $value)
 
 stringifyReflectionType (ReflectionType $type)
 

Static Protected Member Functions

static parseAnnotations ($docblock)
 

Protected Attributes

 $arInspectedClasses = array()
 
 $logger
 

Detailed Description

Definition at line 23 of file JsonMapper.php.

Member Function Documentation

◆ checkMissingData()

JsonMapper::checkMissingData ( $providedProperties,
ReflectionClass $rc )
protected

Check required properties exist in json

Parameters
array$providedPropertiesarray with json properties
object$rcReflection class to check
Exceptions
JsonMapper_Exception
Returns
void

Definition at line 387 of file JsonMapper.php.

Referenced by map().

+ Here is the caller graph for this function:

◆ createInstance()

JsonMapper::createInstance ( $class,
$useParameter = false,
$jvalue = null )
protected

Create a new object of the given type.

This method exists to be overwritten in child classes, so you can do dependency injection or so.

Parameters
string$classClass name to instantiate
boolean$useParameterPass $parameter to the constructor or not
mixed$jvalueConstructor parameter (the json value)
Returns
object Freshly created object

Definition at line 701 of file JsonMapper.php.

Referenced by map().

+ Here is the caller graph for this function:

◆ getCamelCaseName()

JsonMapper::getCamelCaseName ( $name)
protected

Removes - and _ and makes the next letter uppercase

Parameters
string$nameProperty name
Returns
string CamelCasedVariableName

Definition at line 636 of file JsonMapper.php.

◆ getFullNamespace()

JsonMapper::getFullNamespace ( $type,
$strNs )
protected

Convert a type name to a fully namespaced type name.

Parameters
string$typeType name (simple type or class name)
string$strNsBase namespace that gets prepended to the type name
Returns
string Fully-qualified type name with namespace

Definition at line 363 of file JsonMapper.php.

References isSimpleType().

Referenced by map().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getMappedType()

JsonMapper::getMappedType ( $type,
$jvalue = null )
protected

Get the mapped class/type name for this class. Returns the incoming classname if not mapped.

Parameters
string$typeType name to map
mixed$jvalueConstructor parameter (the json value)
Returns
string The mapped type/class name

Definition at line 733 of file JsonMapper.php.

Referenced by map().

+ Here is the caller graph for this function:

◆ getSafeName()

JsonMapper::getSafeName ( $name)
protected

Since hyphens cannot be used in variables we have to uppercase them.

Technically you may use them, but they are awkward to access.

Parameters
string$nameProperty name
Returns
string Name without hyphen

Definition at line 652 of file JsonMapper.php.

Referenced by map().

+ Here is the caller graph for this function:

◆ hasVariadicArrayType()

JsonMapper::hasVariadicArrayType ( $accessor)
protected

Returns true if accessor is a method and has only one parameter which is variadic.

Parameters
ReflectionMethod | ReflectionProperty | null$accessoraccessor to set value
Returns
bool

Definition at line 833 of file JsonMapper.php.

Referenced by map().

+ Here is the caller graph for this function:

◆ inspectProperty()

JsonMapper::inspectProperty ( ReflectionClass $rc,
$name )
protected

Try to find out if a property exists in a given class. Checks property first, falls back to setter method.

Parameters
ReflectionClass$rcReflection class to check
string$nameProperty name
Returns
array First value: if the property exists Second value: the accessor to use ( ReflectionMethod or ReflectionProperty, or null) Third value: type of the property Fourth value: if the property is nullable

Definition at line 508 of file JsonMapper.php.

Referenced by map().

+ Here is the caller graph for this function:

◆ isArrayOfType()

JsonMapper::isArrayOfType ( $strType)
protected

Returns true if type is an array of elements (bracket notation)

Parameters
string$strTypetype to be matched
Returns
bool

Definition at line 819 of file JsonMapper.php.

Referenced by map().

+ Here is the caller graph for this function:

◆ isFlatType()

JsonMapper::isFlatType ( $type)
protected

Checks if the given type is a type that is not nested (simple type except array, object and mixed)

Parameters
string$typetype name from gettype()
Returns
boolean True if it is a non-nested PHP type
See also
isSimpleType()

Definition at line 802 of file JsonMapper.php.

Referenced by map().

+ Here is the caller graph for this function:

◆ isNullable()

JsonMapper::isNullable ( $type)
protected

Checks if the given type is nullable

Parameters
string$typetype name from the phpdoc param
Returns
boolean True if it is nullable

Definition at line 857 of file JsonMapper.php.

◆ isObjectOfSameType()

JsonMapper::isObjectOfSameType ( $type,
$value )
protected

Checks if the object is of this type or has this type as one of its parents

Parameters
string$typeclass name of type being required
mixed$valueSome PHP value to be tested
Returns
boolean True if $object has type of $type

Definition at line 783 of file JsonMapper.php.

Referenced by map().

+ Here is the caller graph for this function:

◆ isSimpleType()

JsonMapper::isSimpleType ( $type)
protected

Checks if the given type is a "simple type"

Parameters
string$typetype name from gettype()
Returns
boolean True if it is a simple PHP type
See also
isFlatType()

Definition at line 764 of file JsonMapper.php.

Referenced by getFullNamespace(), and map().

+ Here is the caller graph for this function:

◆ log()

JsonMapper::log ( $level,
$message,
array $context = array() )
protected

Log a message to the $logger object

Parameters
string$levelLogging level
string$messageText to log
array$contextAdditional information
Returns
null

Definition at line 942 of file JsonMapper.php.

Referenced by map().

+ Here is the caller graph for this function:

◆ map()

JsonMapper::map ( $json,
$object )

Map data all data in $json into the given $object instance.

Parameters
object | array$jsonJSON object structure from json_decode()
object | class-string$objectObject to map $json data into
Returns
mixed Mapped object is returned.
See also
mapArray()

Definition at line 147 of file JsonMapper.php.

References checkMissingData(), createInstance(), getFullNamespace(), getMappedType(), getSafeName(), hasVariadicArrayType(), inspectProperty(), isArrayOfType(), isFlatType(), isObjectOfSameType(), isSimpleType(), log(), map(), mapArray(), removeNullable(), removeUndefinedAttributes(), and setProperty().

Referenced by map().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mapArray()

JsonMapper::mapArray ( $json,
$array,
$class = null,
$parent_key = '' )

Map an array

Parameters
array$jsonJSON array structure from json_decode()
mixed$arrayArray or ArrayObject that gets filled with data from $json
string$classClass name for children objects. All children will get mapped onto this type. Supports class names and simple types like "string" and nullability "string|null". Pass "null" to not convert any values
string$parent_keyDefines the key this array belongs to in order to aid debugging.
Returns
mixed Mapped $array is returned

Definition at line 441 of file JsonMapper.php.

Referenced by map().

+ Here is the caller graph for this function:

◆ parseAnnotations()

static JsonMapper::parseAnnotations ( $docblock)
staticprotected

Copied from PHPUnit 3.7.29, Util/Test.php

Parameters
string$docblockFull method docblock
Returns
array Array of arrays. Key is the "@"-name like "param", each value is an array of the rest of the -lines

Definition at line 914 of file JsonMapper.php.

◆ removeNullable()

JsonMapper::removeNullable ( $type)
protected

Remove the 'null' section of a type

Parameters
string$typetype name from the phpdoc param
Returns
string The new type value

Definition at line 869 of file JsonMapper.php.

Referenced by map().

+ Here is the caller graph for this function:

◆ removeUndefinedAttributes()

JsonMapper::removeUndefinedAttributes ( $object,
$providedProperties )
protected

Remove attributes from object that were not passed in JSON data.

This is to avoid confusion between those that were actually passed as NULL, and those that weren't provided at all.

Parameters
object$objectObject to remove properties from
array$providedPropertiesArray with JSON properties
Returns
void

Definition at line 416 of file JsonMapper.php.

Referenced by map().

+ Here is the caller graph for this function:

◆ setLogger()

JsonMapper::setLogger ( $logger)

Sets a logger instance on the object

Parameters
LoggerInterface$loggerPSR-3 compatible logger object
Returns
null

Definition at line 956 of file JsonMapper.php.

◆ setProperty()

JsonMapper::setProperty ( $object,
$accessor,
$value )
protected

Set a property on a given object to a given value.

Checks if the setter or the property are public are made before calling this method.

Parameters
object$objectObject to set property on
object$accessorReflectionMethod or ReflectionProperty
mixed$valueValue of property
Returns
void

Definition at line 673 of file JsonMapper.php.

Referenced by map().

+ Here is the caller graph for this function:

◆ stringifyReflectionType()

JsonMapper::stringifyReflectionType ( ReflectionType $type)
protected

Get a string representation of the reflection type. Required because named, union and intersection types need to be handled.

Parameters
ReflectionType$typeNative PHP type
Returns
string "foo|bar"

Definition at line 888 of file JsonMapper.php.

Member Data Documentation

◆ $arInspectedClasses

JsonMapper::$arInspectedClasses = array()
protected

Definition at line 120 of file JsonMapper.php.

◆ $bEnforceMapType

JsonMapper::$bEnforceMapType = true

Definition at line 59 of file JsonMapper.php.

◆ $bExceptionOnMissingData

JsonMapper::$bExceptionOnMissingData = false

Definition at line 48 of file JsonMapper.php.

◆ $bExceptionOnUndefinedProperty

JsonMapper::$bExceptionOnUndefinedProperty = false

Definition at line 40 of file JsonMapper.php.

◆ $bIgnoreVisibility

JsonMapper::$bIgnoreVisibility = false

Definition at line 82 of file JsonMapper.php.

◆ $bRemoveUndefinedAttributes

JsonMapper::$bRemoveUndefinedAttributes = false

Definition at line 90 of file JsonMapper.php.

◆ $bStrictNullTypes

JsonMapper::$bStrictNullTypes = true

Definition at line 75 of file JsonMapper.php.

◆ $bStrictObjectTypeChecking

JsonMapper::$bStrictObjectTypeChecking = false

Definition at line 67 of file JsonMapper.php.

◆ $classMap

JsonMapper::$classMap = array()

Definition at line 98 of file JsonMapper.php.

◆ $logger

JsonMapper::$logger
protected

Definition at line 32 of file JsonMapper.php.

◆ $postMappingMethod

JsonMapper::$postMappingMethod = null

Definition at line 129 of file JsonMapper.php.

◆ $postMappingMethodArguments

JsonMapper::$postMappingMethodArguments = array()

Definition at line 136 of file JsonMapper.php.

◆ $undefinedPropertyHandler

JsonMapper::$undefinedPropertyHandler = null

Definition at line 112 of file JsonMapper.php.


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