[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/zend/Zend/Amf/Parse/Amf0/ -> Deserializer.php (source)

   1  <?php
   2  /**
   3   * Zend Framework
   4   *
   5   * LICENSE
   6   *
   7   * This source file is subject to the new BSD license that is bundled
   8   * with this package in the file LICENSE.txt.
   9   * It is also available through the world-wide-web at this URL:
  10   * http://framework.zend.com/license/new-bsd
  11   * If you did not receive a copy of the license and are unable to
  12   * obtain it through the world-wide-web, please send an email
  13   * to [email protected] so we can send you a copy immediately.
  14   *
  15   * @category   Zend
  16   * @package    Zend_Amf
  17   * @subpackage Parse_Amf0
  18   * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  20   * @version    $Id$
  21   */
  22  
  23  /** Zend_Amf_Constants */
  24  require_once 'Zend/Amf/Constants.php';
  25  
  26  /** @see Zend_Amf_Parse_Deserializer */
  27  require_once 'Zend/Amf/Parse/Deserializer.php';
  28  
  29  /**
  30   * Read an AMF0 input stream and convert it into PHP data types
  31   *
  32   * @todo       Implement Typed Object Class Mapping
  33   * @todo       Class could be implemented as Factory Class with each data type it's own class
  34   * @package    Zend_Amf
  35   * @subpackage Parse_Amf0
  36   * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  38   */
  39  class Zend_Amf_Parse_Amf0_Deserializer extends Zend_Amf_Parse_Deserializer
  40  {
  41      /**
  42       * An array of objects used for recursively deserializing an object.
  43       * @var array
  44       */
  45      protected $_reference = array();
  46  
  47      /**
  48       * If AMF3 serialization occurs, update to AMF0 0x03
  49       *
  50       * @var int
  51       */
  52      protected $_objectEncoding = Zend_Amf_Constants::AMF0_OBJECT_ENCODING;
  53  
  54      /**
  55       * Read AMF markers and dispatch for deserialization
  56       *
  57       * Checks for AMF marker types and calls the appropriate methods
  58       * for deserializing those marker types. Markers are the data type of
  59       * the following value.
  60       *
  61       * @param  integer $typeMarker
  62       * @return mixed whatever the data type is of the marker in php
  63       * @throws Zend_Amf_Exception for invalid type
  64       */
  65      public function readTypeMarker($typeMarker = null)
  66      {
  67          if ($typeMarker === null) {
  68              $typeMarker = $this->_stream->readByte();
  69          }
  70  
  71          switch($typeMarker) {
  72              // number
  73              case Zend_Amf_Constants::AMF0_NUMBER:
  74                  return $this->_stream->readDouble();
  75  
  76              // boolean
  77              case Zend_Amf_Constants::AMF0_BOOLEAN:
  78                  return (boolean) $this->_stream->readByte();
  79  
  80              // string
  81              case Zend_Amf_Constants::AMF0_STRING:
  82                  return $this->_stream->readUTF();
  83  
  84              // object
  85              case Zend_Amf_Constants::AMF0_OBJECT:
  86                  return $this->readObject();
  87  
  88              // null
  89              case Zend_Amf_Constants::AMF0_NULL:
  90                  return null;
  91  
  92              // undefined
  93              case Zend_Amf_Constants::AMF0_UNDEFINED:
  94                  return null;
  95  
  96              // Circular references are returned here
  97              case Zend_Amf_Constants::AMF0_REFERENCE:
  98                  return $this->readReference();
  99  
 100              // mixed array with numeric and string keys
 101              case Zend_Amf_Constants::AMF0_MIXEDARRAY:
 102                  return $this->readMixedArray();
 103  
 104              // array
 105              case Zend_Amf_Constants::AMF0_ARRAY:
 106                  return $this->readArray();
 107  
 108              // date
 109              case Zend_Amf_Constants::AMF0_DATE:
 110                  return $this->readDate();
 111  
 112              // longString  strlen(string) > 2^16
 113              case Zend_Amf_Constants::AMF0_LONGSTRING:
 114                  return $this->_stream->readLongUTF();
 115  
 116              //internal AS object,  not supported
 117              case Zend_Amf_Constants::AMF0_UNSUPPORTED:
 118                  return null;
 119  
 120              // XML
 121              case Zend_Amf_Constants::AMF0_XML:
 122                  return $this->readXmlString();
 123  
 124              // typed object ie Custom Class
 125              case Zend_Amf_Constants::AMF0_TYPEDOBJECT:
 126                  return $this->readTypedObject();
 127  
 128              //AMF3-specific
 129              case Zend_Amf_Constants::AMF0_AMF3:
 130                  return $this->readAmf3TypeMarker();
 131  
 132              default:
 133                  require_once 'Zend/Amf/Exception.php';
 134                  throw new Zend_Amf_Exception('Unsupported marker type: ' . $typeMarker);
 135          }
 136      }
 137  
 138      /**
 139       * Read AMF objects and convert to PHP objects
 140       *
 141       * Read the name value pair objects form the php message and convert them to
 142       * a php object class.
 143       *
 144       * Called when the marker type is 3.
 145       *
 146       * @param  array|null $object
 147       * @return object
 148       */
 149      public function readObject($object = null)
 150      {
 151          if ($object === null) {
 152              $object = array();
 153          }
 154  
 155          while (true) {
 156              $key        = $this->_stream->readUTF();
 157              $typeMarker = $this->_stream->readByte();
 158              if ($typeMarker != Zend_Amf_Constants::AMF0_OBJECTTERM ){
 159                  //Recursivly call readTypeMarker to get the types of properties in the object
 160                  $object[$key] = $this->readTypeMarker($typeMarker);
 161              } else {
 162                  //encountered AMF object terminator
 163                  break;
 164              }
 165          }
 166          $this->_reference[] = $object;
 167          return (object) $object;
 168      }
 169  
 170      /**
 171       * Read reference objects
 172       *
 173       * Used to gain access to the private array of reference objects.
 174       * Called when marker type is 7.
 175       *
 176       * @return object
 177       * @throws Zend_Amf_Exception for invalid reference keys
 178       */
 179      public function readReference()
 180      {
 181          $key = $this->_stream->readInt();
 182          if (!array_key_exists($key, $this->_reference)) {
 183              require_once 'Zend/Amf/Exception.php';
 184              throw new Zend_Amf_Exception('Invalid reference key: '. $key);
 185          }
 186          return $this->_reference[$key];
 187      }
 188  
 189      /**
 190       * Reads an array with numeric and string indexes.
 191       *
 192       * Called when marker type is 8
 193       *
 194       * @todo   As of Flash Player 9 there is not support for mixed typed arrays
 195       *         so we handle this as an object. With the introduction of vectors
 196       *         in Flash Player 10 this may need to be reconsidered.
 197       * @return array
 198       */
 199      public function readMixedArray()
 200      {
 201          $length = $this->_stream->readLong();
 202          return $this->readObject();
 203      }
 204  
 205      /**
 206       * Converts numerically indexed actiosncript arrays into php arrays.
 207       *
 208       * Called when marker type is 10
 209       *
 210       * @return array
 211       */
 212      public function readArray()
 213      {
 214          $length = $this->_stream->readLong();
 215          $array = array();
 216          while ($length--) {
 217              $array[] = $this->readTypeMarker();
 218          }
 219          return $array;
 220      }
 221  
 222      /**
 223       * Convert AS Date to Zend_Date
 224       *
 225       * @return Zend_Date
 226       */
 227      public function readDate()
 228      {
 229          // get the unix time stamp. Not sure why ActionScript does not use
 230          // milliseconds
 231          $timestamp = floor($this->_stream->readDouble() / 1000);
 232  
 233          // The timezone offset is never returned to the server; it is always 0,
 234          // so read and ignore.
 235          $offset = $this->_stream->readInt();
 236  
 237          require_once 'Zend/Date.php';
 238          $date   = new Zend_Date($timestamp);
 239          return $date;
 240      }
 241  
 242      /**
 243       * Convert XML to SimpleXml
 244       * If user wants DomDocument they can use dom_import_simplexml
 245       *
 246       * @return SimpleXml Object
 247       */
 248      public function readXmlString()
 249      {
 250          $string = $this->_stream->readLongUTF();
 251          return simplexml_load_string($string);
 252      }
 253  
 254      /**
 255       * Read Class that is to be mapped to a server class.
 256       *
 257       * Commonly used for Value Objects on the server
 258       *
 259       * @todo   implement Typed Class mapping
 260       * @return object|array
 261       * @throws Zend_Amf_Exception if unable to load type
 262       */
 263      public function readTypedObject()
 264      {
 265           require_once 'Zend/Amf/Parse/TypeLoader.php';
 266          // get the remote class name
 267          $className = $this->_stream->readUTF();
 268          $loader = Zend_Amf_Parse_TypeLoader::loadType($className);
 269          $returnObject = new $loader();
 270          $properties = get_object_vars($this->readObject());
 271          foreach($properties as $key=>$value) {
 272              if($key) {
 273                  $returnObject->$key = $value;
 274              }
 275          }
 276          if($returnObject instanceof Zend_Amf_Value_Messaging_ArrayCollection) {
 277              $returnObject = get_object_vars($returnObject);
 278          }
 279          return $returnObject;
 280      }
 281  
 282      /**
 283       * AMF3 data type encountered load AMF3 Deserializer to handle
 284       * type markers.
 285       *
 286       * @return string
 287       */
 288      public function readAmf3TypeMarker()
 289      {
 290          require_once 'Zend/Amf/Parse/Amf3/Deserializer.php';
 291          $deserializer = new Zend_Amf_Parse_Amf3_Deserializer($this->_stream);
 292          $this->_objectEncoding = Zend_Amf_Constants::AMF3_OBJECT_ENCODING;
 293          return $deserializer->readTypeMarker();
 294      }
 295  
 296      /**
 297       * Return the object encoding to check if an AMF3 object
 298       * is going to be return.
 299       *
 300       * @return int
 301       */
 302      public function getObjectEncoding()
 303      {
 304          return $this->_objectEncoding;
 305      }
 306  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1