65536) $type = SabreAMF_AMF0_Const::DT_LONGSTRING; // Normal string if (!$type && is_string($data)) $type = SabreAMF_AMF0_Const::DT_STRING; // Checking if its an array if (!$type && is_array($data)) { // Looping through the array to see if there are any // non-numeric keys foreach(array_keys($data) as $key) { if (!is_numeric($key)) { // There's a non-numeric key.. we'll make it a mixed // array $type = SabreAMF_AMF0_Const::DT_MIXEDARRAY; break; } } // Pure array if (!$type) $type = SabreAMF_AMF0_Const::DT_ARRAY; } // Its an object if (!$type && is_object($data)) { // If its an AMF3 wrapper.. we treat it as such if ($data instanceof SabreAMF_AMF3_Wrapper) $type = SabreAMF_AMF0_Const::DT_AMF3; else if ($data instanceof DateTime) $type = SabreAMF_AMF0_Const::DT_DATE; // We'll see if its registered in the classmapper else if ($this->getRemoteClassName(get_class($data))) $type = SabreAMF_AMF0_Const::DT_TYPEDOBJECT; // Otherwise.. check if it its an TypedObject else if ($data instanceof SabreAMF_ITypedObject) $type = SabreAMF_AMF0_Const::DT_TYPEDOBJECT; // If everything else fails, its a general object else $type = SabreAMF_AMF0_Const::DT_OBJECT; } // If everything failed, throw an exception if ($type===false) { throw new Exception('Unhandled data-type: ' . gettype($data)); return null; } } else $type = $forcetype; $this->stream->writeByte($type); switch ($type) { case SabreAMF_AMF0_Const::DT_NUMBER : return $this->stream->writeDouble($data); case SabreAMF_AMF0_Const::DT_BOOL : return $this->stream->writeByte($data==true); case SabreAMF_AMF0_Const::DT_STRING : return $this->writeString($data); case SabreAMF_AMF0_Const::DT_OBJECT : return $this->writeObject($data); case SabreAMF_AMF0_Const::DT_NULL : return true; case SabreAMF_AMF0_Const::DT_MIXEDARRAY : return $this->writeMixedArray($data); case SabreAMF_AMF0_Const::DT_ARRAY : return $this->writeArray($data); case SabreAMF_AMF0_Const::DT_DATE : return $this->writeDate($data); case SabreAMF_AMF0_Const::DT_LONGSTRING : return $this->writeLongString($data); case SabreAMF_AMF0_Const::DT_TYPEDOBJECT : return $this->writeTypedObject($data); case SabreAMF_AMF0_Const::DT_AMF3 : return $this->writeAMF3Data($data); default : throw new Exception('Unsupported type: ' . gettype($data)); return false; } } /** * writeMixedArray * * @param array $data * @return void */ public function writeMixedArray($data) { $this->stream->writeLong(0); foreach($data as $key=>$value) { $this->writeString($key); $this->writeAMFData($value); } $this->writeString(''); $this->stream->writeByte(SabreAMF_AMF0_Const::DT_OBJECTTERM); } /** * writeArray * * @param array $data * @return void */ public function writeArray($data) { if (!count($data)) { $this->stream->writeLong(0); } else { end($data); $last = key($data); $this->stream->writeLong($last+1); for($i=0;$i<=$last;$i++) { $item = isset($data[$i])?$data[$i]:NULL; $this->writeAMFData($item); } } } /** * writeObject * * @param object $data * @return void */ public function writeObject($data) { foreach($data as $key=>$value) { $this->writeString($key); $this->writeAmfData($value); } $this->writeString(''); $this->stream->writeByte(SabreAMF_AMF0_Const::DT_OBJECTTERM); return true; } /** * writeString * * @param string $string * @return void */ public function writeString($string) { $this->stream->writeInt(strlen($string)); $this->stream->writeBuffer($string); } /** * writeLongString * * @param string $string * @return void */ public function writeLongString($string) { $this->stream->writeLong(strlen($string)); $this->stream->writeBuffer($string); } /** * writeTypedObject * * @param object $data * @return void */ public function writeTypedObject($data) { if ($data instanceof SabreAMF_ITypedObject) { $classname = $data->getAMFClassName(); $data = $data->getAMFData(); } else $classname = $this->getRemoteClassName(get_class($data)); $this->writeString($classname); return $this->writeObject($data); } /** * writeAMF3Data * * @param mixed $data * @return void */ public function writeAMF3Data(SabreAMF_AMF3_Wrapper $data) { $serializer = new SabreAMF_AMF3_Serializer($this->stream); return $serializer->writeAMFData($data->getData()); } /** * Writes a date object * * @param DateTime $data * @return void */ public function writeDate(DateTime $data) { $this->stream->writeDouble($data->format('U')*1000); // empty timezone $this->stream->writeInt(0); } }