AmfSerializer manages the job of translating PHP objects into the actionscript equivalent via Amf.

The main method of the serializer is the serialize method which takes and AmfObject as it's argument and builds the resulting Amf Message.

package Amfphp_Core_Amf

 Methods

get serialized data output

getOutput() : string

Returns

string

converts from php object to binary

serialize(\Amfphp_Core_Amf_Packet $data) : String

The return type is noted as a String, but is a binary stream. echo it to the output buffer

Parameters

Returns

String

Return the serialisation of the given integer (Amf3).

getAmf3Int(int $d) : string

note: There does not seem to be a way to distinguish between signed and unsigned integers. This method just sends the lowest 29 bit as-is, and the receiver is responsible to interpret the result as signed or unsigned based on some context.

note: The limit imposed by Amf3 is 29 bit. So in case the given integer is longer than 29 bit, only the lowest 29 bits will be serialised. No error will be logged!

TODO refactor into writeAmf3Int

Parameters

$d

int

the integer to serialise

Returns

string

looks if $obj already has a reference.

handleReference(mixed $obj, array $references) 

If it does, write it, and return true. If not, add it to the references array. Depending on whether or not the spl_object_hash function can be used ( available (PHP >= 5.2), and can only be used on an object) things are handled a bit differently: - if possible, objects are hashed and the hash is used as a key to the references array. So the array has the structure hash => reference - if not, the object is pushed to the references array, and array_search is used. So the array has the structure reference => object. maxing out the number of stored references improves performance(tested with an array of 9000 identical objects). This may be because isset's performance is linked to the size of the array. weird... note on using $references[$count] = &$obj; rather than $references[] = &$obj; the first one is right, the second is not, as with the second one we could end up with the following: some object hash => 0, 0 => array. (it should be 1 => array)

This also means that 2 completely separate instances of a class but with the same values will be written fully twice if we can't use the hash system

Parameters

$obj

mixed

$references

array

initialize reference arrays and counters.

resetReferences() 

Call before writing a body or a header, as the indices are local to each message body or header

handles writing an anoynous object (stdClass) can also be a reference Also creates a bogus traits entry, as even an anonymous object has traits.

writeAmf3AnonymousObject(\stdClass $d, \doReference $doReference) 

In this way a reference to a class trait will have the right id.

todo it would seem that to create only one traits entry for an anonymous object would be the way to go. this however messes things up in both Flash and Charles Proxy. For testing call discovery service using AMF. investigate.

Parameters

$d

\stdClass

The php object to write

$doReference

\doReference

Boolean This is used by writeAmf3Array, where the reference has already been taken care of, so there this method is called with false

write amf3 array

writeAmf3Array(array $d) 

Parameters

$d

array

Write a boolean (Amf3).

writeAmf3Bool(bool $d) : \nothing

Parameters

$d

bool

the boolean to serialise

Returns

\nothing

write Amfphp_Core_Amf_Types_ByteArray in amf3

writeAmf3ByteArray(\Amfphp_Core_Amf_Types_ByteArray $d) 

write amf 3 data

writeAmf3Data(mixed $d) 
todo no type markers ("\6', for example) in this method!

Parameters

$d

mixed

write Amfphp_Core_Amf_Types_Date in amf 3

writeAmf3Date(\Amfphp_Core_Amf_Types_Date $d) 

Write an (un-)signed integer (Amf3).

writeAmf3Int(int $d) : \nothing
see \getAmf3Int()

Parameters

$d

int

the integer to serialise

Returns

\nothing

Write NULL (Amf3).

writeAmf3Null() : \nothing

Returns

\nothing

write Amf3 Number

writeAmf3Number(\number $d) 

Parameters

$d

\number

Write a string (Amf3).

writeAmf3String(string $d) : \The

Strings are stored in a cache and in case the same string is written again, a reference to the string is sent instead of the string itself.

note: Sending strings larger than 268435455 (2^28-1 byte) will (silently) fail!

note: The string marker is NOT sent here and has to be sent before, if needed.

Parameters

$d

string

the string to send

Returns

\Thereference index inside the lookup table is returned. In case of an empty string which is sent in a special way, NULL is returned.

writes a typed object.

writeAmf3TypedObject(object $d) 

Type is determined by having an "explicit type" field. If this field is not set, call writeAmf3AnonymousObject write all properties as sealed members.

Parameters

$d

object

Write undefined (Amf3).

writeAmf3Undefined() : \nothing

Returns

\nothing

write Amfphp_Core_Amf_Types_Xml in amf3

writeAmf3Xml(\Amfphp_Core_Amf_Types_Xml $d) 

write Amfphp_Core_Amf_Types_XmlDocument in amf3

writeAmf3XmlDocument(\Amfphp_Core_Amf_Types_XmlDocument $d) 

handles writing an anoynous object (stdClass) can also be a reference

writeAnonymousObject(\stdClass $d) 

Parameters

$d

\stdClass

The php object to write

writeArrayOrObject first determines if the PHP array contains all numeric indexes or a mix of keys.

writeArrayOrObject(array $d) 

Then it either writes the array code (0x0A) or the object code (0x03) and then the associated data.

Parameters

$d

array

The php array

writeBoolean writes the boolean code (0x01) and the data to the output stream

writeBoolean(bool $d) 

Parameters

$d

bool

The boolean value

writeByte writes a singe byte to the output stream 0-255 range

writeByte(int $b) 

Parameters

$b

int

An int that can be converted to a byte

writeData checks to see if the type was declared and then either auto negotiates the type or relies on the user defined type to serialize the data into Amf

writeData(mixed $d) 

Parameters

$d

mixed

The data

writeDate writes the date code (0x0B) and the date value (milliseconds from 1 January 1970) to the output stream, along with an empty unsupported timezone

writeDate(\Amfphp_Core_Amf_Types_Date $d) 

Parameters

$d

\Amfphp_Core_Amf_Types_Date

The date value

writeDouble takes a float as the input and writes it to the output stream.

writeDouble(double $d) 

Then if the system is big-endian, it reverses the bytes order because all doubles passed via remoting are passed little-endian.

Parameters

$d

double

The double to add to the output buffer

writeInt takes an int and writes it as 2 bytes to the output stream 0-65535 range

writeInt(int $n) 

Parameters

$n

int

An integer to convert to a 2 byte binary string

writeLong takes an int, float or double and converts it to a 4 byte binary string and adds it to the output buffer

writeLong(\long $l) 

Parameters

$l

\long

A long to convert to a 4 byte binary string

writeLongUTF will write a string longer than 65535 characters.

writeLongUtf(string $s) 

It works exactly as writeUTF does except uses a long for the length flag.

Parameters

$s

string

A string to add to the byte stream

writeNull writes the null code (0x05) to the output stream

writeNull() 

writeNumber writes the number code (0x00) and the numeric data to the output stream All numbers passed through remoting are floats.

writeNumber(int $d) 

Parameters

$d

int

The numeric data

writeObjectEnd writes the object end code (0x009) to the output stream

writeObjectEnd() 

writeObjectFromArray handles writing a php array with string or mixed keys.

writeObjectFromArray(array $d) 

It does not write the object code as that is handled by the writeArrayOrObject and this method is shared with the CustomClass writer which doesn't use the object code.

Parameters

$d

array

The php array with string keys

write reference

writeReference(int $num) 

Parameters

$num

int

writeString writes the string code (0x02) and the UTF8 encoded string to the output stream.

writeString(string $d) 

Note: strings are truncated to 64k max length. Use XML as type to send longer strings

Parameters

$d

string

The string data

writeTypedObject takes an instance of a class and writes the variables defined in it to the output stream.

writeTypedObject(object $d) 

To accomplish this we just blanket grab all of the object vars with get_object_vars, minus the Amfphp_Core_Amf_Constants::FIELD_EXPLICIT_TYPE field, whiuch is used as class name

Parameters

$d

object

The object to serialize the properties. The deserializer looks for Amfphp_Core_Amf_Constants::FIELD_EXPLICIT_TYPE on this object and writes it as the class name.

writeUndefined writes the Undefined code (0x06) to the output stream

writeUndefined() 

writeUTF takes and input string, writes the length as an int and then appends the string to the output buffer

writeUtf(string $s) 

Parameters

$s

string

The string less than 65535 characters to add to the stream

writeXML writes the xml code (0x0F) and the XML string to the output stream Note: strips whitespace

writeXML(\Amfphp_Core_Amf_Types_Xml $d) 

 Properties

 

$Amf0StoredObjects : array
 

$className2TraitsInfo : array

key: class name. value: array(reference id, array(property names))

 

the output stream

$outBuffer : String
   

$storedObjects : array
 

$storedStrings : array

 Constants

 

the maximum amount of objects stored for reference

MAX_STORED_OBJECTS