Beispiel #1
0
 public static function serializeFieldToStream($value, $field, $need_tag, &$output)
 {
     if ($need_tag) {
         if (!GPBWire::writeTag($output, self::makeTag($field->getNumber(), $field->getType()))) {
             return false;
         }
     }
     switch ($field->getType()) {
         case GPBType::DOUBLE:
             if (!GPBWire::writeDouble($output, $value)) {
                 return false;
             }
             break;
         case GPBType::FLOAT:
             if (!GPBWire::writeFloat($output, $value)) {
                 return false;
             }
             break;
         case GPBType::INT64:
             if (!GPBWire::writeInt64($output, $value)) {
                 return false;
             }
             break;
         case GPBType::UINT64:
             if (!GPBWire::writeUint64($output, $value)) {
                 return false;
             }
             break;
         case GPBType::INT32:
             if (!GPBWire::writeInt32($output, $value)) {
                 return false;
             }
             break;
         case GPBType::FIXED32:
             if (!GPBWire::writeFixed32($output, $value)) {
                 return false;
             }
             break;
         case GPBType::FIXED64:
             if (!GPBWire::writeFixed64($output, $value)) {
                 return false;
             }
             break;
         case GPBType::BOOL:
             if (!GPBWire::writeBool($output, $value)) {
                 return false;
             }
             break;
         case GPBType::STRING:
             if (!GPBWire::writeString($output, $value)) {
                 return false;
             }
             break;
             //    case GPBType::GROUP:
             //      echo "GROUP\xA";
             //      trigger_error("Not implemented.", E_ERROR);
             //      break;
         //    case GPBType::GROUP:
         //      echo "GROUP\xA";
         //      trigger_error("Not implemented.", E_ERROR);
         //      break;
         case GPBType::MESSAGE:
             if (!GPBWire::writeMessage($output, $value)) {
                 return false;
             }
             break;
         case GPBType::BYTES:
             if (!GPBWire::writeBytes($output, $value)) {
                 return false;
             }
             break;
         case GPBType::UINT32:
             if (!GPBWire::writeUint32($output, $value)) {
                 return false;
             }
             break;
         case GPBType::ENUM:
             if (!GPBWire::writeInt32($output, $value)) {
                 return false;
             }
             break;
         case GPBType::SFIXED32:
             if (!GPBWire::writeSfixed32($output, $value)) {
                 return false;
             }
             break;
         case GPBType::SFIXED64:
             if (!GPBWire::writeSfixed64($output, $value)) {
                 return false;
             }
             break;
         case GPBType::SINT32:
             if (!GPBWire::writeSint32($output, $value)) {
                 return false;
             }
             break;
         case GPBType::SINT64:
             if (!GPBWire::writeSint64($output, $value)) {
                 return false;
             }
             break;
         default:
             user_error("Unsupported type.");
             return false;
     }
     return true;
 }
Beispiel #2
0
 /**
  * @ignore
  */
 private function serializeRepeatedFieldToStream($field, &$output)
 {
     $getter = $field->getGetter();
     $values = $this->{$getter}();
     $count = count($values);
     if ($count === 0) {
         return true;
     }
     $packed = $field->getPacked();
     if ($packed) {
         if (!GPBWire::writeTag($output, GPBWire::makeTag($field->getNumber(), GPBType::STRING))) {
             return false;
         }
         $size = 0;
         foreach ($values as $value) {
             $size += $this->fieldDataOnlyByteSize($field, $value);
         }
         if (!$output->writeVarint32($size)) {
             return false;
         }
     }
     foreach ($values as $value) {
         if (!GPBWire::serializeFieldToStream($value, $field, !$packed, $output)) {
             return false;
         }
     }
     return true;
 }