本文整理汇总了PHP中google\protobuf\FieldDescriptorProto\Label::LABEL_REQUIRED方法的典型用法代码示例。如果您正苦于以下问题:PHP Label::LABEL_REQUIRED方法的具体用法?PHP Label::LABEL_REQUIRED怎么用?PHP Label::LABEL_REQUIRED使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google\protobuf\FieldDescriptorProto\Label
的用法示例。
在下文中一共展示了Label::LABEL_REQUIRED方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: generateRequiredFieldException
/**
* @param \Protobuf\Compiler\Entity $entity
* @param \google\protobuf\FieldDescriptorProto $field
*
* @return string[]
*/
protected function generateRequiredFieldException(Entity $entity, FieldDescriptorProto $field)
{
$name = $field->getName();
$label = $field->getLabel();
$tag = $field->getNumber();
$isRequired = $label === Label::LABEL_REQUIRED();
if (!$isRequired) {
return [];
}
$class = $entity->getNamespacedName();
$format = 'Field "%s#%s" (tag %s) is required but has no value.';
$message = var_export(sprintf($format, $class, $name, $tag), true);
$body[] = 'if ($this->' . $name . ' === null) {';
$body[] = ' throw new \\UnexpectedValueException(' . $message . ');';
$body[] = '}';
$body[] = null;
return $body;
}
示例2: descriptor
/**
* {@inheritdoc}
*/
public static function descriptor()
{
return \google\protobuf\DescriptorProto::fromArray(['name' => 'NamePart', 'field' => [\google\protobuf\FieldDescriptorProto::fromArray(['number' => 1, 'name' => 'name_part', 'type' => \google\protobuf\FieldDescriptorProto\Type::TYPE_STRING(), 'label' => \google\protobuf\FieldDescriptorProto\Label::LABEL_REQUIRED()]), \google\protobuf\FieldDescriptorProto::fromArray(['number' => 2, 'name' => 'is_extension', 'type' => \google\protobuf\FieldDescriptorProto\Type::TYPE_BOOL(), 'label' => \google\protobuf\FieldDescriptorProto\Label::LABEL_REQUIRED()])]]);
}
示例3: generateDefaultValues
/**
* @param \Protobuf\Compiler\Entity $entity
*
* @return array
*/
protected function generateDefaultValues(Entity $entity)
{
$descriptor = $entity->getDescriptor();
$fields = $descriptor->getFieldList() ?: [];
$size = count($fields);
$lines = [];
foreach ($fields as $i => $field) {
$name = $field->getName();
$comma = $i + 1 < $size ? ',' : '';
$value = $this->getDefaultFieldValue($field);
// required field throw InvalidArgumentException
if ($field->getLabel() === Label::LABEL_REQUIRED()) {
continue;
}
if ($field->getLabel() === Label::LABEL_REPEATED()) {
$value = '[]';
}
$lines[] = "'{$name}' => {$value}" . $comma;
}
return $lines;
}
示例4: generateSetterMethod
/**
* @param \Protobuf\Compiler\Entity $entity
* @param google\protobuf\FieldDescriptorProto $field
*
* @return \Zend\Code\Generator\GeneratorInterface
*/
protected function generateSetterMethod(Entity $entity, FieldDescriptorProto $field)
{
$body = [];
$fieldName = $field->getName();
$fieldType = $field->getType();
$fieldLabel = $field->getLabel();
$typeHint = $this->getTypeHint($field);
if ($fieldType === Type::TYPE_BYTES() && $fieldLabel !== Label::LABEL_REPEATED()) {
$body[] = 'if ($value !== null && ! $value instanceof \\Protobuf\\Stream) {';
$body[] = ' $value = \\Protobuf\\Stream::wrap($value);';
$body[] = '}';
$body[] = null;
$typeHint = null;
}
$body[] = '$this->' . $fieldName . ' = $value;';
$methodName = $this->getAccessorName('set', $field);
$method = MethodGenerator::fromArray(['name' => $methodName, 'body' => implode(PHP_EOL, $body), 'parameters' => [['name' => 'value', 'type' => $typeHint]], 'docblock' => ['shortDescription' => "Set '{$fieldName}' value", 'tags' => [['name' => 'param', 'description' => $this->getDocBlockType($field) . ' $value']]]]);
$method->getDocblock()->setWordWrap(false);
if ($fieldLabel !== Label::LABEL_REQUIRED()) {
$method->getParameters()['value']->setDefaultValue(null);
}
return $method;
}