當前位置: 首頁>>代碼示例>>PHP>>正文


PHP FieldDescriptorProto::hasDefaultValue方法代碼示例

本文整理匯總了PHP中Google\Protobuf\FieldDescriptorProto::hasDefaultValue方法的典型用法代碼示例。如果您正苦於以下問題:PHP FieldDescriptorProto::hasDefaultValue方法的具體用法?PHP FieldDescriptorProto::hasDefaultValue怎麽用?PHP FieldDescriptorProto::hasDefaultValue使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Google\Protobuf\FieldDescriptorProto的用法示例。


在下文中一共展示了FieldDescriptorProto::hasDefaultValue方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: defaultValueAsString

 public function defaultValueAsString(FieldDescriptorProto $field)
 {
     if (!$field->hasDefaultValue()) {
         return 'null';
     }
     $default_value = null;
     switch ($field->getType()) {
         case \ProtocolBuffers::TYPE_DOUBLE:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_FLOAT:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_INT64:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_UINT64:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_INT32:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_FIXED64:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_FIXED32:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_BOOL:
             return $field->getDefaultValue() == "true" ? "true" : "false";
         case \ProtocolBuffers::TYPE_STRING:
             return "\"" . addcslashes($field->getDefaultValue(), "\"\n\r\$") . "\"";
         case \ProtocolBuffers::TYPE_GROUP:
             return;
         case \ProtocolBuffers::TYPE_MESSAGE:
             return "null";
             break;
         case \ProtocolBuffers::TYPE_BYTES:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_UINT32:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_ENUM:
             $value = $field->getTypeName();
             $descriptor = MessagePool::get($value);
             if (!FieldDescriptorProto\Label::isRepeated($field)) {
                 $def = $field->getDefaultValue();
                 if (!empty($def)) {
                     $value = str_replace(".", "\\", $descriptor->full_name) . "::" . $field->getDefaultValue();
                 } else {
                     $value = "null";
                 }
                 return $value;
             } else {
                 return "array()";
             }
         case \ProtocolBuffers::TYPE_SFIXED32:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_SFIXED64:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_SINT32:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_SINT64:
             $default_value = $field->getDefaultValue();
             break;
     }
     if (!$default_value && $default_value !== 0) {
         return "null";
     } else {
         return $default_value;
     }
 }
開發者ID:asamaru7,項目名稱:protoc-gen-php,代碼行數:76,代碼來源:MessageGenerator.php

示例2: compileField

 protected function compileField(proto\FieldDescriptorProto $field, $ns, $indent)
 {
     // Fetch constants by reflecton
     $refl = new \ReflectionClass('\\DrSlump\\Protobuf');
     $constants = $refl->getConstants();
     // Separate rules and types
     $rules = $types = array();
     foreach ($constants as $k => $v) {
         if (false === strpos($k, '_')) {
             continue;
         }
         list($prefix, $name) = explode('_', $k, 2);
         if ('RULE' === $prefix) {
             $rules[$name] = $v;
         } else {
             if ('TYPE' === $prefix) {
                 $types[$name] = $v;
             }
         }
     }
     // Get the key for the rule and type
     $rule = array_search($field->getLabel(), $rules);
     $type = array_search($field->getType(), $types);
     $s[] = "// {$rule} {$type} " . $field->getName() . " = " . $field->getNumber();
     $s[] = '$f = new \\DrSlump\\Protobuf\\Field();';
     $s[] = '$f->number    = ' . $field->getNumber() . ';';
     $s[] = '$f->name      = "' . $field->getName() . '";';
     $s[] = '$f->type      = \\DrSlump\\Protobuf::TYPE_' . $type . ';';
     $s[] = '$f->rule      = \\DrSlump\\Protobuf::RULE_' . $rule . ';';
     if ($field->hasTypeName()) {
         $ref = $field->getTypeName();
         if (substr($ref, 0, 1) !== '.') {
             throw new \RuntimeException("Only fully qualified names are supported but found '{$ref}' at {$ns}");
         }
         $s[] = '$f->reference = \'\\' . $this->normalizeNS($ref) . "';";
     }
     if ($field->hasDefaultValue()) {
         switch ($field->getType()) {
             case Protobuf::TYPE_BOOL:
                 $bool = filter_var($field->getDefaultValue(), FILTER_VALIDATE_BOOLEAN);
                 $s[] = '$f->default   = ' . ($bool ? 'true' : 'false') . ';';
                 break;
             case Protobuf::TYPE_STRING:
                 $s[] = '$f->default   = "' . addcslashes($field->getDefaultValue(), '"\\') . '";';
                 break;
             case Protobuf::TYPE_ENUM:
                 $value = '\\' . $this->normalizeNS($field->getTypeName()) . '::' . $field->getDefaultValue();
                 $s[] = '$f->default   = ' . $value . ';';
                 break;
             default:
                 // Numbers
                 $s[] = '$f->default   = ' . $field->getDefaultValue() . ';';
         }
     }
     $s[] = '// @@protoc_insertion_point(scope_field)';
     $s[] = '// @@protoc_insertion_point(field_' . $ns . ':' . $field->getName() . ')';
     return $indent . implode(PHP_EOL . $indent, $s);
 }
開發者ID:stanley-cheung,項目名稱:protobuf-php,代碼行數:58,代碼來源:PhpGenerator.php

示例3: generateField

 public function generateField(proto\FieldDescriptorProto $field)
 {
     $reference = 'null';
     if ($field->hasTypeName()) {
         $reference = $field->getTypeName();
         if (substr($reference, 0, 1) !== '.') {
             throw new \RuntimeException('Only fully qualified names are supported: ' . $reference);
         }
         $reference = "'" . $this->normalizeNS($reference) . "'";
     }
     $default = 'null';
     if ($field->hasDefaultValue()) {
         switch ($field->getType()) {
             case Protobuf::TYPE_BOOL:
                 $default = $field->getDefaultValue() ? 'true' : 'false';
                 break;
             case Protobuf::TYPE_STRING:
                 $default = '"' . addcslashes($field->getDefaultValue(), '"\\') . '"';
                 break;
             case Protobuf::TYPE_ENUM:
                 $default = $this->normalizeNS($field->getTypeName()) . '.' . $field->getDefaultValue();
                 break;
             default:
                 // Numbers
                 $default = $field->getDefaultValue();
         }
     }
     $data = array("'" . $field->getName() . "'", $field->getLabel(), $field->getType(), $reference, $default, '{}');
     return '[' . implode(', ', $data) . ']';
 }
開發者ID:JosephGarrone,項目名稱:flerovium,代碼行數:30,代碼來源:JsonGenerator.php


注:本文中的Google\Protobuf\FieldDescriptorProto::hasDefaultValue方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。