/** * Reset type map * * @return void */ public static function resetMap() { self::$classMap = self::$_defaultClassMap; }
/** * Find if the class name is a class mapped name and return the * respective classname if it is. * * @param object $object * @return false|string $className */ protected function getClassName($object) { require_once 'Maz/Amf/Parse/TypeLoader.php'; //Check to see if the object is a typed object and we need to change $className = ''; switch (true) { // the return class mapped name back to actionscript class name. case Maz_Amf_Parse_TypeLoader::getMappedClassName(get_class($object)): $className = Maz_Amf_Parse_TypeLoader::getMappedClassName(get_class($object)); break; // Check to see if the user has defined an explicit Action Script type. // Check to see if the user has defined an explicit Action Script type. case isset($object->_explicitType): $className = $object->_explicitType; unset($object->_explicitType); break; // Check if user has defined a method for accessing the Action Script type // Check if user has defined a method for accessing the Action Script type case method_exists($object, 'getASClassName'): $className = $object->getASClassName(); break; // No return class name is set make it a generic object // No return class name is set make it a generic object default: break; } if (!$className == '') { return $className; } else { return false; } }
/** * Map ActionScript classes to PHP classes * * @param string $asClass * @param string $phpClass * @return Maz_Amf_Server */ public function setClassMap($asClass, $phpClass, $moreMappedFields = null) { require_once 'Maz/Amf/Parse/TypeLoader.php'; Maz_Amf_Parse_TypeLoader::setMapping($asClass, $phpClass, $moreMappedFields); return $this; }
/** * Read Class that is to be mapped to a server class. * * Commonly used for Value Objects on the server * * @todo implement Typed Class mapping * @return object * @throws Maz_Amf_Exception if unable to load type */ public function readTypedObject() { require_once 'Maz/Amf/Parse/TypeLoader.php'; // get the remote class name $className = $this->_stream->readUTF(); $loader = Maz_Amf_Parse_TypeLoader::loadType($className); $returnObject = new $loader(); $properties = get_object_vars($this->readObject()); foreach ($properties as $key => $value) { if ($key) { $returnObject->{$key} = $value; } } return $returnObject; }
/** * Read an object from the AMF stream and convert it into a PHP object * * @todo Rather than using an array of traitsInfo create Maz_Amf_Value_TraitsInfo * @return object */ public function readObject() { $traitsInfo = $this->readInteger(); $storedObject = ($traitsInfo & 0x1) == 0; $traitsInfo = $traitsInfo >> 1; // Check if the Object is in the stored Objects reference table if ($storedObject) { $ref = $traitsInfo; if (!isset($this->_referenceObjects[$ref])) { require_once 'Maz/Amf/Exception.php'; throw new Maz_Amf_Exception('Unknown Object reference: ' . $ref); } $returnObject = $this->_referenceObjects[$ref]; } else { // Check if the Object is in the stored Definistions reference table $storedClass = ($traitsInfo & 0x1) == 0; $traitsInfo = $traitsInfo >> 1; if ($storedClass) { $ref = $traitsInfo; if (!isset($this->_referenceDefinitions[$ref])) { require_once 'Maz/Amf/Exception.php'; throw new Maz_Amf_Exception('Unknows Definition reference: ' . $ref); } // Populate the reference attributes $className = $this->_referenceDefinitions[$ref]['className']; $encoding = $this->_referenceDefinitions[$ref]['encoding']; $propertyNames = $this->_referenceDefinitions[$ref]['propertyNames']; } else { // The class was not in the reference tables. Start reading rawdata to build traits. // Create a traits table. Maz_Amf_Value_TraitsInfo would be ideal $className = $this->readString(); $encoding = $traitsInfo & 0x3; $propertyNames = array(); $traitsInfo = $traitsInfo >> 2; } // We now have the object traits defined in variables. Time to go to work: if (!$className) { // No class name generic object $returnObject = new stdClass(); } else { // Defined object // Typed object lookup agsinst registered classname maps if ($loader = Maz_Amf_Parse_TypeLoader::loadType($className)) { $returnObject = new $loader(); } else { //user defined typed object require_once 'Maz/Amf/Exception.php'; throw new Maz_Amf_Exception('Typed object not found: ' . $className . ' '); } } // Add the Object ot the reference table $this->_referenceObjects[] = $returnObject; // Check encoding types for additional processing. switch ($encoding) { case Maz_Amf_Constants::ET_EXTERNAL: // Externalizable object such as {ArrayCollection} and {ObjectProxy} if (!$storedClass) { $this->_referenceDefinitions[] = array('className' => $className, 'encoding' => $encoding, 'propertyNames' => $propertyNames); } $returnObject->externalizedData = $this->readTypeMarker(); break; case Maz_Amf_Constants::ET_DYNAMIC: // used for Name-value encoding if (!$storedClass) { $this->_referenceDefinitions[] = array('className' => $className, 'encoding' => $encoding, 'propertyNames' => $propertyNames); } // not a refrence object read name value properties from byte stream $properties = array(); // clear value do { $property = $this->readString(); if ($property != "") { $propertyNames[] = $property; $properties[$property] = $this->readTypeMarker(); } } while ($property != ""); break; default: // basic property list object. if (!$storedClass) { $count = $traitsInfo; // Number of properties in the list for ($i = 0; $i < $count; $i++) { $propertyNames[] = $this->readString(); } // Add a refrence to the class. $this->_referenceDefinitions[] = array('className' => $className, 'encoding' => $encoding, 'propertyNames' => $propertyNames); } $properties = array(); // clear value foreach ($propertyNames as $property) { $properties[$property] = $this->readTypeMarker(); } break; } // Add properties back to the return object. foreach ($properties as $key => $value) { if ($key) { $returnObject->{$key} = $value; } } } return $returnObject; }
/** * Write object to ouput stream * * @param mixed $data * @return Maz_Amf_Parse_Amf3_Serializer */ public function writeObject($object) { $encoding = Maz_Amf_Constants::ET_PROPLIST; $className = ''; $moreMappedFields = array(); //Check to see if the object is a typed object and we need to change switch (true) { // the return class mapped name back to actionscript class name. case $className = Maz_Amf_Parse_TypeLoader::getMappedClassName(get_class($object)): $moreMappedFields = Maz_Amf_Parse_TypeLoader::getMoreMappedFields(get_class($object)); break; // Check to see if the user has defined an explicit Action Script type. // Check to see if the user has defined an explicit Action Script type. case isset($object->_explicitType): $className = $object->_explicitType; unset($object->_explicitType); break; // Check if user has defined a method for accessing the Action Script type // Check if user has defined a method for accessing the Action Script type case method_exists($object, 'getASClassName'): $className = $object->getASClassName(); break; // No return class name is set make it a generic object // No return class name is set make it a generic object default: break; } $traitsInfo = Maz_Amf_Constants::AMF3_OBJECT_ENCODING; $traitsInfo |= $encoding << 2; try { switch ($encoding) { case Maz_Amf_Constants::ET_PROPLIST: $count = 0; foreach ($object as $key => $value) { $count++; } foreach ($moreMappedFields as $mappedField) { $count++; } $traitsInfo |= $count << 4; // Write the object ID $this->writeInteger($traitsInfo); // Write the classname $this->writeString($className); // Write the object Key's to the output stream foreach ($object as $key => $value) { $this->writeString($key); } foreach ($moreMappedFields as $mappedField) { $this->writeString($mappedField['key']); } //Write the object values to the output stream. foreach ($object as $key => $value) { $this->writeTypeMarker($value); } foreach ($moreMappedFields as $mappedField) { $key = $mappedField['key']; $this->writeTypeMarker($object->{$key}); } break; case Maz_Amf_Constants::ET_EXTERNAL: require_once 'Maz/Amf/Exception.php'; throw new Maz_Amf_Exception('External Object Encoding not implemented'); break; default: require_once 'Maz/Amf/Exception.php'; throw new Maz_Amf_Exception('Unknown Object Encoding type: ' . $encoding); } } catch (Exception $e) { require_once 'Maz/Amf/Exception.php'; throw new Maz_Amf_Exception('Unable to writeObject output: ' . $e->getMessage()); } return $this; }