コード例 #1
0
 /**
  * Serializes the 'properties' attribute (a dictionary) into the
  * raw bytes making up a set of property flags and a property
  * list, suitable for putting into a content frame header.
  *
  * @return string
  * @todo Inject the AMQPWriter to make the method easier to test
  */
 public function serializeProperties()
 {
     if ($this->properties == []) {
         return "";
         // optimization for empty properties
     } else {
         $shift = 15;
         $flag_bits = 0;
         $flags = array();
         $raw_bytes = new AMQPBufferWriter();
         foreach ($this->prop_types as $key => $prototype) {
             $val = isset($this->properties[$key]) ? $this->properties[$key] : null;
             // Very important: PHP type eval is weak, use the === to test the
             // value content. Zero or false value should not be removed
             if ($val === null) {
                 $shift -= 1;
                 continue;
             }
             if ($shift === 0) {
                 $flags[] = $flag_bits;
                 $flag_bits = 0;
                 $shift = 15;
             }
             $flag_bits |= 1 << $shift;
             if ($prototype != 'bit') {
                 $raw_bytes->{'write_' . $prototype}($val);
             }
             $shift -= 1;
         }
         $flags[] = $flag_bits;
         $result = new AMQPBufferWriter();
         foreach ($flags as $flag_bits) {
             $result->write_short($flag_bits);
         }
         $result->write($raw_bytes->getvalue());
         return $result->getvalue();
     }
 }