本文整理汇总了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;
}
}
示例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);
}
示例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) . ']';
}