XML codec for PHP object graphs.
The passing of the argument by reference in decode, decodeNode, decodeAttributes, decodeAttribute, decodeChildren, decodeChild, beforeDecode and afterDecode is required since the object may be an array instance, which needs an explicit reference operator even in PHP 5 to be changed in-place.
mxObjectCodec | XML codec for PHP object graphs. |
Variables | |
template | Holds the template object associated with this codec. |
exclude | Array containing the variable names that should be ignored by the codec. |
idrefs | Array containing the variable names that should be turned into or converted from references. |
mapping | Maps from from fieldnames to XML attribute names. |
reverse | Maps from from XML attribute names to fieldnames. |
Functions | |
mxObjectCodec | Constructs a new codec for the specified template object. |
getName | Creates a new instance of the template for this codec. |
cloneTemplate | Creates a new instance of the template for this codec. |
getFieldName | Returns the fieldname for the given attributename. |
getAttributeName | Returns the attributename for the given fieldname. |
isExcluded | Returns true if the given attribute is to be ignored by the codec. |
isReference | Returns true if the given fieldname is to be treated as a textual reference (ID). |
encode | Encodes the specified object and returns a node representing then given object. |
encodeObject | Encodes the value of each member in then given obj into the given node using encodeValue. |
encodeValue | Converts the given value according to the mappings and id-refs in this codec and uses writeAttribute to write the attribute into the given node. |
writeAttribute | Writes the given value into node using writePrimitiveAttribute or writeComplexAttribute depending on the type of the value. |
writePrimitiveAttribute | Writes the given value as an attribute of the given node. |
writeComplexAttribute | Writes the given value as a child node of the given node. |
convertValueToXml | Returns the given value without applying a conversion. |
convertValueFromXml | Returns the given value. |
beforeEncode | Hook for subclassers to pre-process the object before encoding. |
afterEncode | Hook for subclassers to post-process the node for the given object after encoding and return the post-processed node. |
decode | Parses the given node into the object or returns a new object representing the given node. |
decodeNode | Calls decodeAttributes and decodeChildren for the given node. |
decodeAttributes | Decodes all attributes of the given node using decodeAttribute. |
decodeAttribute | Reads the given attribute into the specified object. |
decodeChildren | Decodec all children of the given node using decodeChild. |
decodeChild | Reads the specified child into the given object. |
getFieldTemplate | Returns the template instance for the given field. |
addObjectValue | Sets the decoded child node as a value of the given object. |
processInclude | Returns true if the given node is an include directive and executes the include by decoding the XML document. |
beforeDecode | Hook for subclassers to pre-process the node for the specified object and return the node to be used for further processing by decode. |
afterDecode | Hook for subclassers to post-process the object after decoding. |
var $idrefs
Array containing the variable names that should be turned into or converted from references. See mxCodec.getId and mxCodec.getObject.
function mxObjectCodec( $template, $exclude = array(), $idrefs = array(), $mapping = array() )
Constructs a new codec for the specified template object. The variables in the optional exclude array are ignored by the codec. Variables in the optional idrefs array are turned into references in the XML. The optional mapping may be used to map from variable names to XML attributes.
template | Prototypical instance of the object to be encoded/decoded. |
exclude | Optional array of fieldnames to be ignored. |
idrefs | Optional array of fieldnames to be converted to/from references. |
mapping | Optional mapping from field- to attributenames. |
function getFieldName( $attributename )
Returns the fieldname for the given attributename. Looks up the value in the reverse mapping or returns the input if there is no reverse mapping for the given name.
function getAttributeName( $fieldname )
Returns the attributename for the given fieldname. Looks up the value in the mapping or returns the input if there is no mapping for the given name.
function isExcluded( $obj, $attr, $value, $write )
Returns true if the given attribute is to be ignored by the codec. This implementation returns true if the given fieldname is in exclude or if the fieldname equals <mxObjectIdentity.FIELD_NAME>.
obj | Object instance that contains the field. |
attr | Fieldname of the field. |
value | Value of the field. |
write | Boolean indicating if the field is being encoded or decoded. Write is true if the field is being encoded, else it is being decoded. |
function isReference( $obj, $attr, $value, $write )
Returns true if the given fieldname is to be treated as a textual reference (ID). This implementation returns true if the given fieldname is in idrefs.
obj | Object instance that contains the field. |
attr | Fieldname of the field. |
value | Value of the field. |
write | Boolean indicating if the field is being encoded or decoded. Write is true if the field is being encoded, else it is being decoded. |
function encode( $enc, $obj )
Encodes the specified object and returns a node representing then given object. Calls beforeEncode after creating the node and afterEncode with the resulting node after processing.
Enc is a reference to the calling encoder. It is used to encode complex objects and create references.
This implementation encodes all variables of an object according to the following rules:
If no ID exists for a variable in idrefs or if an object cannot be encoded, a warning is issued using mxLog.warn.
Returns the resulting XML node that represents the given object.
enc | mxCodec that controls the encoding process. |
obj | Object to be encoded. |
function encodeObject( $enc, $obj, $node )
Encodes the value of each member in then given obj into the given node using encodeValue.
enc | mxCodec that controls the encoding process. |
obj | Object to be encoded. |
node | XML node that contains the encoded object. |
function encodeValue( $enc, $obj, $name, $value, $node )
Converts the given value according to the mappings and id-refs in this codec and uses writeAttribute to write the attribute into the given node.
enc | mxCodec that controls the encoding process. |
obj | Object whose property is going to be encoded. |
name | XML node that contains the encoded object. |
value | Value of the property to be encoded. |
node | XML node that contains the encoded object. |
function writeAttribute( $enc, $obj, $attr, $value, $node )
Writes the given value into node using writePrimitiveAttribute or writeComplexAttribute depending on the type of the value.
function beforeEncode( $enc, $obj, $node )
Hook for subclassers to pre-process the object before encoding. This returns the input object. The return value of this function is used in encode to perform the default encoding into the given node.
enc | mxCodec that controls the encoding process. |
obj | Object to be encoded. |
node | XML node to encode the object into. |
function afterEncode( $enc, $obj, $node )
Hook for subclassers to post-process the node for the given object after encoding and return the post-processed node. This implementation returns the input node. The return value of this method is returned to the encoder from encode.
enc | mxCodec that controls the encoding process. |
obj | Object to be encoded. |
node | XML node that represents the default encoding. |
function decode( $dec, $node, & $into = null )
Parses the given node into the object or returns a new object representing the given node.
Dec is a reference to the calling decoder. It is used to decode complex objects and resolve references.
If a node has an id attribute then the object cache is checked for the object. If the object is not yet in the cache then it is constructed using the constructor of template and cached in mxCodec.objects.
This implementation decodes all attributes and childs of a node according to the following rules:
For add nodes where the object is not an array and the variable name is defined, the default mechanism is used, allowing to override/add methods as follows:
<Object> <add as="hello"><![CDATA[ function(arg1) { alert('Hello '+arg1); } ]]></add> </Object>
If no object exists for an ID in idrefs a warning is issued using mxLog.warn.
Returns the resulting object that represents the given XML node or the object given to the method as the into parameter.
dec | mxCodec that controls the decoding process. |
node | XML node to be decoded. |
into | Optional objec to encode the node into. |
function decodeNode( $dec, $node, & $obj )
Calls decodeAttributes and decodeChildren for the given node.
function decodeAttributes( $dec, $node, & $obj )
Decodes all attributes of the given node using decodeAttribute.
function decodeChildren( $dec, $node, & $obj )
Decodec all children of the given node using decodeChild.
function getFieldTemplate( & $obj, $fieldname, $child )
Returns the template instance for the given field. This returns the value of the field, null if the value is an array or an empty collection if the value is a collection. The value is then used to populate the field for a new instance. For strongly typed languages it may be required to override this to return the correct collection instance based on the encoded child.
function addObjectValue( & $obj, $fieldname, $value, $template )
Sets the decoded child node as a value of the given object. If the object is a map, then the value is added with the given fieldname as a key. If the fieldname is not empty, then setFieldValue is called or else, if the object is a collection, the value is added to the collection. For strongly typed languages it may be required to override this with the correct code to add an entry to an object.
function processInclude( $dec, $node, $into )
Returns true if the given node is an include directive and executes the include by decoding the XML document. Returns false if the given node is not an include directive.
dec | mxCodec that controls the encoding/decoding process. |
node | XML node to be checked. |
into | Optional object to pass-thru to the codec. |
function beforeDecode( $dec, $node, & $obj )
Hook for subclassers to pre-process the node for the specified object and return the node to be used for further processing by decode. The object is created based on the template in the calling method and is never null. This implementation returns the input node. The return value of this function is used in decode to perform the default decoding into the given object.
dec | mxCodec that controls the decoding process. |
node | XML node to be decoded. |
obj | Object to encode the node into. |
function afterDecode( $dec, $node, & $obj )
Hook for subclassers to post-process the object after decoding. This implementation returns the given object without any changes. The return value of this method is returned to the decoder from decode.
enc | mxCodec that controls the encoding process. |
node | XML node to be decoded. |
obj | Object that represents the default decoding. |
Holds the template object associated with this codec.
var $template
Array containing the variable names that should be ignored by the codec.
var $exclude
Array containing the variable names that should be turned into or converted from references.
var $idrefs
Maps from from fieldnames to XML attribute names.
var $mapping
Maps from from XML attribute names to fieldnames.
var $reverse
Constructs a new codec for the specified template object.
function mxObjectCodec( $template, $exclude = array(), $idrefs = array(), $mapping = array() )
Creates a new instance of the template for this codec.
function getName()
Creates a new instance of the template for this codec.
function cloneTemplate()
Returns the fieldname for the given attributename.
function getFieldName( $attributename )
Returns the attributename for the given fieldname.
function getAttributeName( $fieldname )
Returns true if the given attribute is to be ignored by the codec.
function isExcluded( $obj, $attr, $value, $write )
Returns true if the given fieldname is to be treated as a textual reference (ID).
function isReference( $obj, $attr, $value, $write )
Encodes the specified object and returns a node representing then given object.
function encode( $enc, $obj )
Encodes the value of each member in then given obj into the given node using encodeValue.
function encodeObject( $enc, $obj, $node )
Converts the given value according to the mappings and id-refs in this codec and uses writeAttribute to write the attribute into the given node.
function encodeValue( $enc, $obj, $name, $value, $node )
Writes the given value into node using writePrimitiveAttribute or writeComplexAttribute depending on the type of the value.
function writeAttribute( $enc, $obj, $attr, $value, $node )
Writes the given value as an attribute of the given node.
function writePrimitiveAttribute( $enc, $obj, $attr, $value, $node )
Writes the given value as a child node of the given node.
function writeComplexAttribute( $enc, $obj, $attr, $value, $node )
Returns the given value without applying a conversion.
function convertValueToXml( $value )
Returns the given value.
function convertValueFromXml( $value )
Hook for subclassers to pre-process the object before encoding.
function beforeEncode( $enc, $obj, $node )
Hook for subclassers to post-process the node for the given object after encoding and return the post-processed node.
function afterEncode( $enc, $obj, $node )
Parses the given node into the object or returns a new object representing the given node.
function decode( $dec, $node, & $into = null )
Calls decodeAttributes and decodeChildren for the given node.
function decodeNode( $dec, $node, & $obj )
Decodes all attributes of the given node using decodeAttribute.
function decodeAttributes( $dec, $node, & $obj )
Decodec all children of the given node using decodeChild.
function decodeChildren( $dec, $node, & $obj )
Reads the given attribute into the specified object.
function decodeAttribute( $dec, $attr, & $obj )
Reads the specified child into the given object.
function decodeChild( $dec, $child, & $obj )
Returns the template instance for the given field.
function getFieldTemplate( & $obj, $fieldname, $child )
Sets the decoded child node as a value of the given object.
function addObjectValue( & $obj, $fieldname, $value, $template )
Returns true if the given node is an include directive and executes the include by decoding the XML document.
function processInclude( $dec, $node, $into )
Hook for subclassers to pre-process the node for the specified object and return the node to be used for further processing by decode.
function beforeDecode( $dec, $node, & $obj )
Hook for subclassers to post-process the object after decoding.
function afterDecode( $dec, $node, & $obj )
Returns the ID of the specified object.
function getId( $obj )
Returns the decoded object for the element with the specified ID in document.
function getObject( $id )
Logs a warn trace.
static function warn( $text )
Maps from IDs to objects.
var $objects