PHP Class JsonMapper

Author: Christian Weiske ([email protected])
Afficher le fichier Open project: netresearch/jsonmapper Class Usage Examples

Méthodes publiques

Свойство 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

Свойство 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

Méthodes publiques

Méthode 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

Méthodes protégées

Méthode 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 méthode

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
Résultat void

createInstance() public méthode

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
Résultat object Freshly created object

getCamelCaseName() protected méthode

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

getFullNamespace() protected méthode

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
Résultat string Fully-qualified type name with namespace

getSafeName() protected méthode

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

inspectProperty() protected méthode

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
Résultat 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 méthode

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()
Résultat boolean True if it is a non-nested PHP type

isNullable() protected méthode

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

isObjectOfSameType() protected méthode

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

isSimpleType() protected méthode

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

log() protected méthode

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
Résultat null

map() public méthode

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
Résultat object Mapped object is returned.

mapArray() public méthode

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
Résultat mixed Mapped $array is returned

parseAnnotations() protected static méthode

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

removeNullable() protected méthode

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

setLogger() public méthode

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

setProperty() protected méthode

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
Résultat 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
Résultat 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
Résultat 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
Résultat boolean

$bIgnoreVisibility public_oe property

Allow mapping of private and proteted properties.
public bool $bIgnoreVisibility
Résultat 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
Résultat boolean

$bStrictObjectTypeChecking public_oe property

Throw an exception when an object is expected but the JSON contains a non-object type.
public bool $bStrictObjectTypeChecking
Résultat 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
Résultat array

$logger protected_oe property

PSR-3 compatible logger object
See also: setLogger()
protected object $logger
Résultat object

$undefinedPropertyHandler public_oe property

Callback used when an undefined property is found.
public callable $undefinedPropertyHandler
Résultat callable