PHP Class JsonMapper

Author: Christian Weiske ([email protected])
Mostra file Open project: netresearch/jsonmapper Class Usage Examples

Public Properties

Property Type Description
$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 Properties

Property Type Description
$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

Public Methods

Method Description
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

Protected Methods

Method Description
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.

Method Details

checkMissingData() protected method

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
return void

createInstance() public method

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
return object Freshly created object

getCamelCaseName() protected method

Removes - and _ and makes the next letter uppercase
protected getCamelCaseName ( string $name ) : string
$name string Property name
return string CamelCasedVariableName

getFullNamespace() protected method

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
return string Fully-qualified type name with namespace

getSafeName() protected method

Technically you may use them, but they are awkward to access.
protected getSafeName ( string $name ) : string
$name string Property name
return string Name without hyphen

inspectProperty() protected method

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
return 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() protected method

Checks if the given type is a type that is not nested (simple type except array and object)
See also: isSimpleType()
protected isFlatType ( string $type ) : boolean
$type string type name from gettype()
return boolean True if it is a non-nested PHP type

isNullable() protected method

Checks if the given type is nullable
protected isNullable ( string $type ) : boolean
$type string type name from the phpdoc param
return boolean True if it is nullable

isObjectOfSameType() protected method

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
return boolean True if $object has type of $type

isSimpleType() protected method

Checks if the given type is a "simple type"
See also: isFlatType()
protected isSimpleType ( string $type ) : boolean
$type string type name from gettype()
return boolean True if it is a simple PHP type

log() protected method

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
return null

map() public method

Map data all data in $json into the given $object instance.
See also: mapArray()
public map ( object $json, object $object ) : object
$json object JSON object structure from json_decode()
$object object Object to map $json data into
return object Mapped object is returned.

mapArray() public method

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
return mixed Mapped $array is returned

parseAnnotations() protected static method

Copied from PHPUnit 3.7.29, Util/Test.php
protected static parseAnnotations ( string $docblock ) : array
$docblock string Full method docblock
return array

removeNullable() protected method

Remove the 'null' section of a type
protected removeNullable ( string $type ) : string
$type string type name from the phpdoc param
return string The new type value

setLogger() public method

Sets a logger instance on the object
public setLogger ( LoggerInterface $logger ) : null
$logger LoggerInterface PSR-3 compatible logger object
return null

setProperty() protected method

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
return void

Property Details

$arInspectedClasses protected_oe property

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

$bEnforceMapType public_oe property

You have to disable it if you're using the json_decode "assoc" parameter. json_decode($str, false)
public bool $bEnforceMapType
return boolean

$bExceptionOnMissingData public_oe property

Throw an exception if the JSON data miss a property that is marked with @required in the PHP class
public bool $bExceptionOnMissingData
return boolean

$bExceptionOnUndefinedProperty public_oe property

Throw an exception when JSON data contain a property that is not defined in the PHP class
public bool $bExceptionOnUndefinedProperty
return boolean

$bIgnoreVisibility public_oe property

Allow mapping of private and proteted properties.
public bool $bIgnoreVisibility
return boolean

$bStrictNullTypes public_oe property

Throw an exception, if null value is found but the type of attribute does not allow nulls.
public bool $bStrictNullTypes
return boolean

$bStrictObjectTypeChecking public_oe property

Throw an exception when an object is expected but the JSON contains a non-object type.
public bool $bStrictObjectTypeChecking
return boolean

$classMap public_oe property

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
return array

$logger protected_oe property

PSR-3 compatible logger object
See also: setLogger()
protected object $logger
return object

$undefinedPropertyHandler public_oe property

Callback used when an undefined property is found.
public callable $undefinedPropertyHandler
return callable