Exemplo n.º 1
0
 public function setUp()
 {
     $this->_server = new Server();
     $this->_server->setProduction(false);
     TypeLoader::resetMap();
     $this->_acl = new \fproject\amf\acl\Acl();
 }
Exemplo n.º 2
0
 /**
  * Write object to output stream
  *
  * @param mixed $object
  * @return Amf3Serializer
  * @throws AmfException
  */
 public function writeObject($object)
 {
     if ($this->writeObjectReference($object)) {
         return $this;
     }
     //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 = 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;
     }
     //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'];
         if (array_key_exists('reflectProperties', $this->_referenceDefinitions[$className])) {
             $reflectProperties = $this->_referenceDefinitions[$className]['reflectProperties'];
         }
         $traitsInfo = $traitsInfo << 2 | 0x1;
         $writeTraits = false;
     } else {
         $propertyNames = [];
         $reflectProperties = null;
         if ($className == '') {
             //if there is no className, we interpret the class as dynamic without any sealed members
             $encoding = Constants::ET_DYNAMIC;
         } else {
             $encoding = Constants::ET_PROPLIST;
             foreach ($object as $key => $value) {
                 if ($key[0] != "_") {
                     $propertyNames[] = $key;
                 }
                 if (is_array($value) && is_null($reflectProperties)) {
                     $reflector = new AmfReflector($object);
                     $reflectProperties = $reflector->annotations;
                 }
             }
         }
         $this->_referenceDefinitions[$className] = ['id' => count($this->_referenceDefinitions), 'encoding' => $encoding, 'propertyNames' => $propertyNames];
         if (!empty($reflectProperties)) {
             $this->_referenceDefinitions[$className]['reflectProperties'] = $reflectProperties;
         }
         $traitsInfo = Constants::AMF3_OBJECT_ENCODING;
         $traitsInfo |= $encoding << 2;
         $traitsInfo |= count($propertyNames) << 4;
         $writeTraits = true;
     }
     $this->writeInteger($traitsInfo);
     if ($writeTraits) {
         $this->writeString($className);
         foreach ($propertyNames as $key) {
             $this->writeString($key);
         }
     }
     try {
         switch ($encoding) {
             case Constants::ET_PROPLIST:
                 //Write the sealed values to the output stream.
                 foreach ($propertyNames as $key) {
                     $markerType = null;
                     $extraData = false;
                     if (!empty($reflectProperties) && !is_null($object->{$key})) {
                         if (array_key_exists($key, $reflectProperties)) {
                             if ($reflectProperties[$key]['isVector']) {
                                 $markerType = $reflectProperties[$key]['vectorElementType'];
                                 $extraData = ['elementType' => $reflectProperties[$key]['typeName'], 'fixed' => $reflectProperties[$key]['isFixedVector']];
                             }
                         }
                     }
                     $this->writeTypeMarker($object->{$key}, $markerType, $extraData);
                 }
                 break;
             case 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 Constants::ET_EXTERNAL:
                 throw new AmfException('External Object Encoding not implemented');
                 break;
             default:
                 throw new AmfException('Unknown Object Encoding type: ' . $encoding);
         }
     } catch (\Exception $e) {
         throw new AmfException('Unable to writeObject output: ' . $e->getMessage(), 0, $e);
     }
     return $this;
 }
Exemplo n.º 3
0
 /**
  * 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 TypeLoader::getMappedClassName(get_class($object)):
             $className = 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;
     }
 }
Exemplo n.º 4
0
 public function testUnknownClassMap()
 {
     $class = TypeLoader::loadType('com.example.vo.Bogus');
     $this->assertEquals('stdClass', $class);
 }
Exemplo n.º 5
0
 /**
  * Add a class mapping and lookup the mapping to make sure
  * the mapping succeeds
  */
 public function testClassMap()
 {
     $this->_server->setClassMap('controller.test', 'Zend_Amf_testclass');
     $className = TypeLoader::getMappedClassName('Zend_Amf_testclass');
     $this->assertEquals('controller.test', $className);
 }
Exemplo n.º 6
0
 /**
  * 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
  * @throws \fproject\amf\AmfException if unable to load type
  */
 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 ArrayCollection) {
         $returnObject = get_object_vars($returnObject);
     }
     return $returnObject;
 }
Exemplo n.º 7
0
 /**
  * Map ActionScript classes to PHP classes
  *
  * @param  string $asClass
  * @param  string $phpClass
  * @return Server
  */
 public function setClassMap($asClass, $phpClass)
 {
     TypeLoader::setMapping($asClass, $phpClass);
     return $this;
 }
Exemplo n.º 8
0
 /**
  * @param $className
  * @return array
  * @throws \fproject\amf\AmfException
  */
 protected function createObjectInstance($className)
 {
     // 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();
             $className = $loader;
         } else {
             //user defined typed object
             $this->throwAmfException('Typed object not found: {0}', [$className]);
         }
     }
     return ['object' => $returnObject, 'className' => $className];
 }
Exemplo n.º 9
0
 public function testAmf0TypedObjecDeserializedToNativePHPObject()
 {
     TypeLoader::setMapping("ContactVO", "Contact");
     $myRequest = file_get_contents(dirname(__FILE__) . '/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 MessageBody);
     $data = $bodies[0]->getData();
     // Make sure that we are dealing with a PHP simpleXml element
     $this->assertTrue($data[0] instanceof Contact);
     // Make sure that the xml was deserialized properly and check its value
     $this->assertEquals('arnold', (string) $data[0]->lastname);
 }
Exemplo n.º 10
0
 public function setUp()
 {
     date_default_timezone_set('America/Chicago');
     TypeLoader::resetMap();
 }
Exemplo n.º 11
0
 /**
  * Defining new unknown resource type, handler has no parse()
  *
  */
 public function testCtxNoParse()
 {
     TypeLoader::setResourceLoader(new Zend_Amf_TestResourceLoader("3"));
     try {
         $resp = $this->_callService("returnCtx");
     } catch (\fproject\amf\AmfException $e) {
         $this->assertContains("Could not call parse()", $e->getMessage());
         return;
     }
     $this->fail("Failed to throw exception on unknown resource");
 }
Exemplo n.º 12
0
 public function testPhpObjectSerializedToAmf0TypedObjectClassMap()
 {
     TypeLoader::setMapping("ContactVO", "Contact");
     $data = array();
     $contact = new 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 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 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(dirname(__FILE__) . '/Response/mock/typedObjectAmf0Response.bin');
     // Check that the response matches the expected serialized value
     $this->assertEquals($mockResponse, $testResponse);
 }