PHP Класс JsonMapper

Автор: Christian Weiske ([email protected])
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$bEnforceMapType boolean You have to disable it if you're using the json_decode "assoc" parameter. json_decode($str, false)
$bExceptionOnMissingData boolean Throw an exception if the JSON data miss a property that is marked with @required in the PHP class
$bExceptionOnUndefinedProperty boolean Throw an exception when JSON data contain a property that is not defined in the PHP class
$bIgnoreVisibility boolean Allow mapping of private and proteted properties.
$bStrictNullTypes boolean Throw an exception, if null value is found but the type of attribute does not allow nulls.
$bStrictObjectTypeChecking boolean Throw an exception when an object is expected but the JSON contains a non-object type.
$classMap array Useful when your setter methods accept abstract classes or interfaces. Works only when $bExceptionOnUndefinedProperty is disabled. Parameters to this function are: 1. Object that is being filled 2. Name of the unknown JSON property 3. JSON value of the property
$undefinedPropertyHandler callable Callback used when an undefined property is found.

Защищенные свойства (Protected)

Свойство Тип Описание
$arInspectedClasses Runtime cache for inspected classes. This is particularly effective if mapArray() is called with a large number of objects
$logger object PSR-3 compatible logger object

Открытые методы

Метод Описание
createInstance ( string $class, boolean $useParameter = false, mixed $parameter = null ) : object Create a new object of the given type.
map ( object $json, object $object ) : object Map data all data in $json into the given $object instance.
mapArray ( array $json, mixed $array, string $class = null ) : mixed Map an array
setLogger ( LoggerInterface $logger ) : null Sets a logger instance on the object

Защищенные методы

Метод Описание
checkMissingData ( array $providedProperties, ReflectionClass $rc ) : void Check required properties exist in json
getCamelCaseName ( string $name ) : string Removes - and _ and makes the next letter uppercase
getFullNamespace ( string $type, string $strNs ) : string Convert a type name to a fully namespaced type name.
getSafeName ( string $name ) : string Since hyphens cannot be used in variables we have to uppercase them.
inspectProperty ( ReflectionClass $rc, string $name ) : array Try to find out if a property exists in a given class.
isFlatType ( string $type ) : boolean Checks if the given type is a type that is not nested (simple type except array and object)
isNullable ( string $type ) : boolean Checks if the given type is nullable
isObjectOfSameType ( string $type, mixed $value ) : boolean Checks if the object is of this type or has this type as one of its parents
isSimpleType ( string $type ) : boolean Checks if the given type is a "simple type"
log ( string $level, string $message, array $context = [] ) : null Log a message to the $logger object
parseAnnotations ( string $docblock ) : array Copied from PHPUnit 3.7.29, Util/Test.php
removeNullable ( string $type ) : string Remove the 'null' section of a type
setProperty ( object $object, object $accessor, mixed $value ) : void Set a property on a given object to a given value.

Описание методов

checkMissingData() защищенный Метод

Check required properties exist in json
protected checkMissingData ( array $providedProperties, ReflectionClass $rc ) : void
$providedProperties array array with json properties
$rc ReflectionClass Reflection class to check
Результат void

createInstance() публичный Метод

This method exists to be overwritten in child classes, so you can do dependency injection or so.
public createInstance ( string $class, boolean $useParameter = false, mixed $parameter = null ) : object
$class string Class name to instantiate
$useParameter boolean Pass $parameter to the constructor or not
$parameter mixed Constructor parameter
Результат object Freshly created object

getCamelCaseName() защищенный Метод

Removes - and _ and makes the next letter uppercase
protected getCamelCaseName ( string $name ) : string
$name string Property name
Результат string CamelCasedVariableName

getFullNamespace() защищенный Метод

Convert a type name to a fully namespaced type name.
protected getFullNamespace ( string $type, string $strNs ) : string
$type string Type name (simple type or class name)
$strNs string Base namespace that gets prepended to the type name
Результат string Fully-qualified type name with namespace

getSafeName() защищенный Метод

Technically you may use them, but they are awkward to access.
protected getSafeName ( string $name ) : string
$name string Property name
Результат string Name without hyphen

inspectProperty() защищенный Метод

Checks property first, falls back to setter method.
protected inspectProperty ( ReflectionClass $rc, string $name ) : array
$rc ReflectionClass Reflection class to check
$name string Property name
Результат array First value: if the property exists Second value: the accessor to use ( ReflectionMethod or ReflectionProperty, or null) Third value: type of the property

isFlatType() защищенный Метод

Checks if the given type is a type that is not nested (simple type except array and object)
См. также: isSimpleType()
protected isFlatType ( string $type ) : boolean
$type string type name from gettype()
Результат boolean True if it is a non-nested PHP type

isNullable() защищенный Метод

Checks if the given type is nullable
protected isNullable ( string $type ) : boolean
$type string type name from the phpdoc param
Результат boolean True if it is nullable

isObjectOfSameType() защищенный Метод

Checks if the object is of this type or has this type as one of its parents
protected isObjectOfSameType ( string $type, mixed $value ) : boolean
$type string class name of type being required
$value mixed Some PHP value to be tested
Результат boolean True if $object has type of $type

isSimpleType() защищенный Метод

Checks if the given type is a "simple type"
См. также: isFlatType()
protected isSimpleType ( string $type ) : boolean
$type string type name from gettype()
Результат boolean True if it is a simple PHP type

log() защищенный Метод

Log a message to the $logger object
protected log ( string $level, string $message, array $context = [] ) : null
$level string Logging level
$message string Text to log
$context array Additional information
Результат null

map() публичный Метод

Map data all data in $json into the given $object instance.
См. также: mapArray()
public map ( object $json, object $object ) : object
$json object JSON object structure from json_decode()
$object object Object to map $json data into
Результат object Mapped object is returned.

mapArray() публичный Метод

Map an array
public mapArray ( array $json, mixed $array, string $class = null ) : mixed
$json array JSON array structure from json_decode()
$array mixed Array or ArrayObject that gets filled with data from $json
$class string Class 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
Результат mixed Mapped $array is returned

parseAnnotations() защищенный статический Метод

Copied from PHPUnit 3.7.29, Util/Test.php
protected static parseAnnotations ( string $docblock ) : array
$docblock string Full method docblock
Результат array

removeNullable() защищенный Метод

Remove the 'null' section of a type
protected removeNullable ( string $type ) : string
$type string type name from the phpdoc param
Результат string The new type value

setLogger() публичный Метод

Sets a logger instance on the object
public setLogger ( LoggerInterface $logger ) : null
$logger LoggerInterface PSR-3 compatible logger object
Результат null

setProperty() защищенный Метод

Checks if the setter or the property are public are made before calling this method.
protected setProperty ( object $object, object $accessor, mixed $value ) : void
$object object Object to set property on
$accessor object ReflectionMethod or ReflectionProperty
$value mixed Value of property
Результат void

Описание свойств

$arInspectedClasses защищенное свойство

Runtime cache for inspected classes. This is particularly effective if mapArray() is called with a large number of objects
protected $arInspectedClasses

$bEnforceMapType публичное свойство

You have to disable it if you're using the json_decode "assoc" parameter. json_decode($str, false)
public bool $bEnforceMapType
Результат boolean

$bExceptionOnMissingData публичное свойство

Throw an exception if the JSON data miss a property that is marked with @required in the PHP class
public bool $bExceptionOnMissingData
Результат boolean

$bExceptionOnUndefinedProperty публичное свойство

Throw an exception when JSON data contain a property that is not defined in the PHP class
public bool $bExceptionOnUndefinedProperty
Результат boolean

$bIgnoreVisibility публичное свойство

Allow mapping of private and proteted properties.
public bool $bIgnoreVisibility
Результат boolean

$bStrictNullTypes публичное свойство

Throw an exception, if null value is found but the type of attribute does not allow nulls.
public bool $bStrictNullTypes
Результат boolean

$bStrictObjectTypeChecking публичное свойство

Throw an exception when an object is expected but the JSON contains a non-object type.
public bool $bStrictObjectTypeChecking
Результат boolean

$classMap публичное свойство

Useful when your setter methods accept abstract classes or interfaces. Works only when $bExceptionOnUndefinedProperty is disabled. Parameters to this function are: 1. Object that is being filled 2. Name of the unknown JSON property 3. JSON value of the property
public array $classMap
Результат array

$logger защищенное свойство

PSR-3 compatible logger object
См. также: setLogger()
protected object $logger
Результат object

$undefinedPropertyHandler публичное свойство

Callback used when an undefined property is found.
public callable $undefinedPropertyHandler
Результат callable