Class Amfphp_Core_Amf_Serializer

Description

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.

TODO spit into 2 classes, one for Amf0 , one for Amf3 or maybe more.

Located in /Amfphp/Core/Amf/Serializer.php (line 22)


	
			
Direct descendents
Class Description
AmfSerializerWrapper This class exports some internal (public) methods. This way, those methods can be tested separately.
Class Constant Summary
Variable Summary
Method Summary
Amfphp_Core_Amf_Serializer __construct (Amfphp_Core_Amf_Packet $packet)
string getAmf3Int (int $d)
void getOutput ()
void handleReference ( &$obj,  &$references, mixed $obj, array $references)
void resetReferences ()
void serialize ()
void writeAmf3AnonymousObject (stdClass $d, [doReference $doReference = true])
void writeAmf3Array ( $d)
nothing writeAmf3Bool (bool $d)
void writeAmf3ByteArray ( $d)
void writeAmf3Data ( &$d)
void writeAmf3Date ( $d)
nothing writeAmf3Int (int $d)
nothing writeAmf3Null ()
void writeAmf3Number ( $d)
The writeAmf3String (string $d)
void writeAmf3TypedObject (object $d)
nothing writeAmf3Undefined ()
void writeAmf3Xml ( $d)
void writeAmf3XmlDocument ( $d)
void writeAnonymousObject (stdClass $d)
void writeArrayOrObject (array $d)
void writeBoolean (bool $d)
void writeByte (int $b)
void writeData (mixed $d)
void writeDate ( $d)
void writeDouble (double $d)
void writeInt (int $n)
void writeLong (long $l)
void writeLongUtf (string $s)
void writeNull ()
void writeNumber (int $d)
void writeObjectEnd ()
void writeObjectFromArray (array $d)
void writePlainArray ( $d)
void writeReference ( $num)
void writeString (string $d)
void writeTypedObject (object $d)
void writeUndefined ()
void writeUtf (string $s)
void writeXML ( $d)
Variables
array $Amf0StoredObjects (line 44)

used for Amf0 references

  • access: protected
array $className2TraitsInfo (line 61)

used for traits references. key: class name. value: array(reference id, array(property names))

  • access: protected
String $outBuffer (line 28)
  • var: the output stream
  • access: protected
Amfphp_Core_Amf_Packet $packet (line 33)
  • access: protected
array $storedObjects (line 50)

used for Amf3 references

  • access: protected
array $storedStrings (line 55)

amf3 references to strings

  • access: protected
Methods
Constructor __construct (line 67)
  • access: public
Amfphp_Core_Amf_Serializer __construct (Amfphp_Core_Amf_Packet $packet)
  • $packet
getAmf3Int (line 714)

Return the serialisation of the given integer (Amf3).

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
  • access: protected
string getAmf3Int (int $d)
  • int $d: the integer to serialise
getOutput (line 131)
  • access: public
void getOutput ()
handleReference (line 796)

looks if $obj already has a reference. 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...

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

  • access: protected
void handleReference ( &$obj,  &$references, mixed $obj, array $references)
  • mixed $obj
  • array $references
  • &$obj
  • array &$references
resetReferences (line 75)

initialize reference arrays and counters. Call before writing a body or a header, as the indices are local to each message body or header

  • access: protected
void resetReferences ()
serialize (line 87)

serializes the Packet passed in the constructor TODO clean up the mess with the temp buffers. A.S.

  • access: public
void serialize ()
writeAmf3AnonymousObject (line 619)

handles writing an anoynous object (stdClass) can also be a reference Also creates a bogus traits entry, as even an anonymous object has traits. In this way a reference to a class trait will have the right id.

  • access: protected
void writeAmf3AnonymousObject (stdClass $d, [doReference $doReference = true])
  • stdClass $d: 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
writeAmf3Array (line 645)
  • access: protected
void writeAmf3Array ( $d)
  • array $d

Redefined in descendants as:
writeAmf3Bool (line 564)

Write a boolean (Amf3).

  • access: protected
nothing writeAmf3Bool (bool $d)
  • bool $d: the boolean to serialise

Redefined in descendants as:
writeAmf3ByteArray (line 773)
  • access: protected
void writeAmf3ByteArray ( $d)
  • $d

Redefined in descendants as:
writeAmf3Data (line 490)
  • todo: no type markers ("\6', for example) in this method!
  • todo: Is the reference still needed? PHP4 needed it for objects, but PHP5 always passes objects by reference. And PHP5 uses a copy-on-write approach, so that all values are passed as 'reference', in case no changes take place.
  • access: protected
void writeAmf3Data ( &$d)
  • &$d

Redefined in descendants as:
writeAmf3Date (line 766)
  • access: protected
void writeAmf3Date ( $d)

Redefined in descendants as:
writeAmf3Int (line 577)

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

nothing writeAmf3Int (int $d)
  • int $d: the integer to serialise
writeAmf3Null (line 553)

Write NULL (Amf3).

  • access: protected
nothing writeAmf3Null ()

Redefined in descendants as:
writeAmf3Number (line 743)
  • access: protected
void writeAmf3Number ( $d)
  • $d

Redefined in descendants as:
writeAmf3String (line 595)

Write a string (Amf3). 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.

  • return: reference index inside the lookup table is returned. In case of an empty string which is sent in a special way, NULL is returned.
  • access: protected
The writeAmf3String (string $d)
  • string $d: the string to send

Redefined in descendants as:
writeAmf3TypedObject (line 842)

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

  • access: protected
void writeAmf3TypedObject (object $d)
  • object $d

Redefined in descendants as:
writeAmf3Undefined (line 544)

Write undefined (Amf3).

  • access: protected
nothing writeAmf3Undefined ()

Redefined in descendants as:
writeAmf3Xml (line 754)
  • access: protected
void writeAmf3Xml ( $d)

Redefined in descendants as:
writeAmf3XmlDocument (line 760)
  • access: protected
void writeAmf3XmlDocument ( $d)

Redefined in descendants as:
writeAnonymousObject (line 386)

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

  • access: protected
void writeAnonymousObject (stdClass $d)
  • stdClass $d: The php object to write
writeArrayOrObject (line 298)

writeArrayOrObject first determines if the PHP array contains all numeric indexes or a mix of keys. Then it either writes the array code (0x0A) or the object code (0x03) and then the associated data.

  • access: protected
void writeArrayOrObject (array $d)
  • array $d: The php array

Redefined in descendants as:
writeBoolean (line 211)

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

  • access: protected
void writeBoolean (bool $d)
  • bool $d: The boolean value

Redefined in descendants as:
writeByte (line 141)

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

  • access: protected
void writeByte (int $b)
  • int $b: An int that can be converted to a byte

Redefined in descendants as:
writeData (line 437)

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

  • access: protected
void writeData (mixed $d)
  • mixed $d: The data
writeDate (line 252)

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

  • access: protected
void writeDate ( $d)

Redefined in descendants as:
writeDouble (line 172)

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

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

  • access: protected
void writeDouble (double $d)
  • double $d: The double to add to the output buffer

Redefined in descendants as:
writeInt (line 151)

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

  • access: protected
void writeInt (int $n)
  • int $n: An integer to convert to a 2 byte binary string

Redefined in descendants as:
writeLong (line 161)

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

  • access: protected
void writeLong (long $l)
  • long $l: A long to convert to a 4 byte binary string

Redefined in descendants as:
writeLongUtf (line 201)

writeLongUTF will write a string longer than 65535 characters.

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

  • access: protected
void writeLongUtf (string $s)
  • string $s: A string to add to the byte stream

Redefined in descendants as:
writeNull (line 272)

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

  • access: protected
void writeNull ()

Redefined in descendants as:
writeNumber (line 264)

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

  • access: protected
void writeNumber (int $d)
  • int $d: The numeric data

Redefined in descendants as:
writeObjectEnd (line 286)

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

  • access: protected
void writeObjectEnd ()

Redefined in descendants as:
writeObjectFromArray (line 372)

writeObjectFromArray handles writing a php array with string or mixed keys. 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.

  • access: protected
void writeObjectFromArray (array $d)
  • array $d: The php array with string keys
writePlainArray (line 354)

Write a plain numeric array without anything fancy

  • access: protected
void writePlainArray ( $d)
  • $d
writeReference (line 346)
  • access: protected
void writeReference ( $num)
  • $num

Redefined in descendants as:
writeString (line 224)

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

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

  • access: protected
void writeString (string $d)
  • string $d: The string data

Redefined in descendants as:
writeTypedObject (line 407)

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

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

  • access: protected
void writeTypedObject (object $d)
  • object $d: 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.

Redefined in descendants as:
writeUndefined (line 279)

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

  • access: protected
void writeUndefined ()

Redefined in descendants as:
writeUtf (line 189)

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

  • access: protected
void writeUtf (string $s)
  • string $s: The string less than 65535 characters to add to the stream

Redefined in descendants as:
writeXML (line 240)

writeXML writes the xml code (0x0F) and the XML string to the output stream

Note: strips whitespace

  • access: protected
void writeXML ( $d)
  • string $d: The XML string

Redefined in descendants as:
Class Constants
MAX_STORED_OBJECTS = 1024 (line 38)

the maximum amount of objects stored for reference

Documentation generated on Mon, 23 Jan 2012 17:22:07 -0300 by phpDocumentor 1.4.3