当前位置: 首页>>代码示例>>PHP>>正文


PHP FieldDescriptorProto::getDefaultValue方法代码示例

本文整理汇总了PHP中Google\Protobuf\FieldDescriptorProto::getDefaultValue方法的典型用法代码示例。如果您正苦于以下问题:PHP FieldDescriptorProto::getDefaultValue方法的具体用法?PHP FieldDescriptorProto::getDefaultValue怎么用?PHP FieldDescriptorProto::getDefaultValue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Google\Protobuf\FieldDescriptorProto的用法示例。


在下文中一共展示了FieldDescriptorProto::getDefaultValue方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: 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

示例2: 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

示例3: getDefaultFieldValue

 /**
  * @param \google\protobuf\FieldDescriptorProto $field
  *
  * @return string
  */
 protected function getDefaultFieldValue(FieldDescriptorProto $field)
 {
     $type = $field->getType();
     $value = $field->getDefaultValue();
     if ($value === null) {
         return 'null';
     }
     if ($type === Type::TYPE_ENUM()) {
         $typeName = $field->getTypeName();
         $refEntity = $this->getEntity($typeName);
         $reference = $refEntity->getNamespacedName();
         $const = $reference . '::' . $value . '()';
         return $const;
     }
     if ($type === Type::TYPE_BOOL()) {
         $value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
     }
     return var_export($value, true);
 }
开发者ID:protobuf-php,项目名称:protobuf-plugin,代码行数:24,代码来源:BaseGenerator.php

示例4: 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::getDefaultValue方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。