public function setUp() { $this->_server = new Amf\Server(); $this->_server->setProduction(false); \ZendAmf\Parser\TypeLoader::resetMap(); $this->_acl = new Acl\Acl(); }
public function testAmf0TypedObjecDeserializedToNativePHPObject() { Parser\TypeLoader::setMapping("ContactVO", "ZendAmfTest\\TestAsset\\Contact"); $myRequest = file_get_contents(__DIR__ . '/TestAsset/Request/mock/typedObjectAmf0Request.bin'); // send the mock object request to be deserialized $this->_request->initialize($myRequest); // Make sure that no headers where recieved $this->assertEquals(0, sizeof($this->_request->getAmfHeaders())); // Make sure that the message body was set after deserialization $this->assertEquals(1, sizeof($this->_request->getAmfBodies())); $bodies = $this->_request->getAmfBodies(); $this->assertTrue($bodies[0] instanceof Value\MessageBody); $data = $bodies[0]->getData(); // Make sure that we are dealing with a PHP simpleXml element $this->assertTrue($data[0] instanceof TestAsset\Contact); // Make sure that the xml was deserialized properly and check its value $this->assertEquals('arnold', (string) $data[0]->lastname); }
/** * Add a class mapping and lookup the mapping to make sure * the mapping succeeds */ public function testClassMap() { $this->_server->setClassMap('controller.test', 'ZendTest\\Amf\\TestAsset\\Server\\testclass'); $className = Parser\TypeLoader::getMappedClassName('ZendTest\\Amf\\TestAsset\\Server\\testclass'); $this->assertEquals('controller.test', $className); }
/** * 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) { //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 Parser\TypeLoader::getMappedClassName(get_class($object)): $className = Parser\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; 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 case $object instanceof \stdClass: $className = ''; break; // By default, use object's class name // By default, use object's class name default: $className = get_class($object); break; } if (!$className == '') { return $className; } else { return false; } }
public function testUnknownClassMap() { $class = Parser\TypeLoader::loadType('com.example.vo.Bogus'); $this->assertEquals('stdClass', $class); }
/** * Defining new unknown resource type, handler has no parse() * */ public function testCtxNoParse() { $this->markTestSkipped('Plugin loader implementation needs to be revisited'); Parser\TypeLoader::setResourceLoader(new TestAsset\TestResourceLoader("3")); $this->setExpectedException('ZendAmf\\Exception\\RuntimeException', 'Could not call parse()'); $resp = $this->_callService("returnCtx"); }
/** * Map ActionScript classes to PHP classes * * @param string $asClass * @param string $phpClass * @return Server */ public function setClassMap($asClass, $phpClass) { Parser\TypeLoader::setMapping($asClass, $phpClass); 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|array */ public function readTypedObject() { // get the remote class name $className = $this->_stream->readUTF(); $loader = TypeLoader::loadType($className); $returnObject = new $loader(); $properties = get_object_vars($this->readObject()); foreach ($properties as $key => $value) { if ($key) { $returnObject->{$key} = $value; } } if ($returnObject instanceof \ZendAmf\Value\Messaging\ArrayCollection) { $returnObject = get_object_vars($returnObject); } return $returnObject; }
public function testPhpObjectSerializedToAmf0TypedObjectClassMap() { Parser\TypeLoader::setMapping("ContactVO", "Contact"); $data = array(); $contact = new TestAsset\Contact(); $contact->id = '15'; $contact->firstname = 'Joe'; $contact->lastname = 'Smith'; $contact->email = '*****@*****.**'; $contact->mobile = '123-456-7890'; unset($contact->_explicitType); array_push($data, $contact); $contact = new TestAsset\Contact(); $contact->id = '23'; $contact->firstname = 'Adobe'; $contact->lastname = 'Flex'; $contact->email = '*****@*****.**'; $contact->mobile = '123-456-7890'; unset($contact->_explicitType); array_push($data, $contact); $newBody = new Value\MessageBody('/1/onResult', null, $data); $this->_response->setObjectEncoding(0x0); $this->_response->addAmfBody($newBody); $this->_response->finalize(); $testResponse = $this->_response->getResponse(); // Load the expected response. $mockResponse = file_get_contents(__DIR__ . '/TestAsset/Response/typedObjectAmf0Response.bin'); // Check that the response matches the expected serialized value $this->assertEquals($mockResponse, $testResponse); }
/** * Defining new unknown resource type, handler has no parse() * */ public function testCtxNoParse() { Parser\TypeLoader::setResourceLoader(new TestAsset\TestResourceLoader(3)); $this->setExpectedException('ZendAmf\\Exception\\RuntimeException', 'Could not call parse()'); $resp = $this->_callService("returnCtx"); }
/** * Write object to ouput stream * * @param mixed $data * @return Serializer * @throws Parser\Exception\OutOfBoundsException */ public function writeObject($object) { if ($this->writeObjectReference($object)) { return $this; } $className = ''; //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 = Parser\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; 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 case $object instanceof \stdClass: $className = ''; break; // By default, use object's class name // By default, use object's class name default: $className = get_class($object); break; } $writeTraits = true; //check to see, if we have a corresponding definition if (array_key_exists($className, $this->_referenceDefinitions)) { $traitsInfo = $this->_referenceDefinitions[$className]['id']; $encoding = $this->_referenceDefinitions[$className]['encoding']; $propertyNames = $this->_referenceDefinitions[$className]['propertyNames']; $traitsInfo = $traitsInfo << 2 | 0x1; $writeTraits = false; } else { $propertyNames = array(); if ($className == '') { //if there is no className, we interpret the class as dynamic without any sealed members $encoding = Amf\Constants::ET_DYNAMIC; } else { $encoding = Amf\Constants::ET_PROPLIST; foreach ($object as $key => $value) { if ($key[0] != "_") { $propertyNames[] = $key; } } } $this->_referenceDefinitions[$className] = array('id' => count($this->_referenceDefinitions), 'encoding' => $encoding, 'propertyNames' => $propertyNames); $traitsInfo = Amf\Constants::AMF3_OBJECT_ENCODING; $traitsInfo |= $encoding << 2; $traitsInfo |= count($propertyNames) << 4; } $this->writeInteger($traitsInfo); if ($writeTraits) { $this->writeString($className); foreach ($propertyNames as $value) { $this->writeString($value); } } try { switch ($encoding) { case Amf\Constants::ET_PROPLIST: //Write the sealed values to the output stream. foreach ($propertyNames as $key) { $this->writeTypeMarker($object->{$key}); } break; case Amf\Constants::ET_DYNAMIC: //Write the sealed values to the output stream. foreach ($propertyNames as $key) { $this->writeTypeMarker($object->{$key}); } //Write remaining properties foreach ($object as $key => $value) { if (!in_array($key, $propertyNames) && $key[0] != "_") { $this->writeString($key); $this->writeTypeMarker($value); } } //Write an empty string to end the dynamic part $this->writeString($this->_strEmpty); break; case Amf\Constants::ET_EXTERNAL: throw new Parser\Exception\OutOfBoundsException('External Object Encoding not implemented'); break; default: throw new Parser\Exception\OutOfBoundsException('Unknown Object Encoding type: ' . $encoding); } } catch (\Exception $e) { throw new Parser\Exception\OutOfBoundsException('Unable to writeObject output: ' . $e->getMessage(), 0, $e); } return $this; }
/** * Read an object from the AMF stream and convert it into a PHP object * * @todo Rather than using an array of traitsInfo create Zend_Amf_Value_TraitsInfo * @return object|array * @throws \ZendAmf\Parser\Exception\OutOfBoundsException */ 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])) { throw new OutOfBoundsException('Unknown Object reference: ' . $ref); } $returnObject = $this->_referenceObjects[$ref]; } else { // Check if the Object is in the stored Definitions reference table $storedClass = ($traitsInfo & 0x1) == 0; $traitsInfo = $traitsInfo >> 1; if ($storedClass) { $ref = $traitsInfo; if (!isset($this->_referenceDefinitions[$ref])) { throw new OutOfBoundsException('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. Zend_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 against registered classname maps if ($loader = TypeLoader::loadType($className)) { $returnObject = new $loader(); } else { //user defined typed object throw new OutOfBoundsException('Typed object not found: ' . $className . ' '); } } // Add the Object to the reference table $this->_referenceObjects[] = $returnObject; $properties = array(); // clear value // Check encoding types for additional processing. switch ($encoding) { case 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 Constants::ET_DYNAMIC: // used for Name-value encoding if (!$storedClass) { $this->_referenceDefinitions[] = array('className' => $className, 'encoding' => $encoding, 'propertyNames' => $propertyNames); } // not a reference object read name value properties from byte stream 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 reference to the class. $this->_referenceDefinitions[] = array('className' => $className, 'encoding' => $encoding, 'propertyNames' => $propertyNames); } foreach ($propertyNames as $property) { $properties[$property] = $this->readTypeMarker(); } break; } // Add properties back to the return object. if (!is_array($properties)) { $properties = array(); } foreach ($properties as $key => $value) { if ($key) { $returnObject->{$key} = $value; } } } if ($returnObject instanceof ArrayCollection) { if (isset($returnObject->externalizedData)) { $returnObject = $returnObject->externalizedData; } else { $returnObject = get_object_vars($returnObject); } } return $returnObject; }
/** * Map from PHP type name to AS type name * * @param string $typename PHP type name * @return string AS type name */ protected function _phpTypeToAS($typename) { if (class_exists($typename)) { $vars = get_class_vars($typename); if (isset($vars['_explicitType'])) { return $vars['_explicitType']; } } if (false !== ($asname = \ZendAmf\Parser\TypeLoader::getMappedClassName($typename))) { return $asname; } return $typename; }