PHP 클래스 JsonMapper

저자: Christian Weiske ([email protected])
파일 보기 프로젝트 열기: netresearch/jsonmapper 1 사용 예제들

공개 프로퍼티들

프로퍼티 타입 설명
$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.

보호된 프로퍼티들

프로퍼티 타입 설명
$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