如何在PHP中处理Protocol Buffers数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在PHP中处理Protocol Buffers数据相关的知识,希望对你有一定的参考价值。

参考技术A 第一步安装Googleprotoc编译器工具proto文件定义Message转换各种编程语言类载release版本直接编译安装
第二步安装protocPHP plugin需要使用composer安装插件proto文件转换PHP文件PHP应用引用二进制格式Protocol Buffers数据转换PHP象
第三步安装php-protocolbuffersPHP扩展第二步已经完proto文件PHP文件转换PHP象各种操作需要扩展API
第四步借助面提扩展各种API简单get自需要字段完续工作
require "....../kingso.proto.php"
$ks_result = KSResult::parseFromString($kingso_res);
$ks_result->get('xxx_name');
具体可以参考 http://www.cnblogs.com/chunguang/p/5618588.html本回答被提问者采纳

如何在 Protocol Buffers 中构建数据模型

【中文标题】如何在 Protocol Buffers 中构建数据模型【英文标题】:How to build a data model in Protocol Buffers 【发布时间】:2021-09-14 06:10:26 【问题描述】:

例如,我需要这样的模型:

[
  
    "type": "input",
    "identity": "protocol_buffers_input",
    "value": "Input Value",
    "rules": 
      "required": true,
      "max_length": 100
    ,
    "properties": 
      "label": "Input Controller",
      "readonly": false,
      "default_value": "Input Default Value",
      "width": "100%",
      "placeholder": "Input Placeholder"
    ,
    "business": 
      "client_visible": true,
      "disabled": false
    ,
    "disabled": true
  ,
  
    "type": "select",
    "identity": "protocol_buffers_select",
    "value": ["key1", "key2"],
    "rules": 
      "required": true
    ,
    "properties": 
      "label": "Select Controller",
      "readonly": false,
      "default_value": ["key1", "key2"],
      "clear_able": true,
      "filterable": false,
      "width": "100%",
      "options": [
         "label": "label1", "value": "key1" ,
         "label": "label2", "value": "key2" ,
         "label": "label3", "value": "key3" 
      ],
      "multiple": true,
      "placeholder": "Select Placeholder"
    ,
    "business": 
      "client_visible": true,
      "disabled": false
    ,
    "disabled": true
  ,
  
    "type": "switch",
    "identity": "protocol_buffers_switch",
    "value": true,
    "rules": 
      "required": true
    ,
    "properties": 
      "label": "Switch Controller",
      "readonly": false,
      "default_value": true
    ,
    "business": 
      "client_visible": true,
      "disabled": false
    ,
    "disabled": true
  ,
  
    "type": "image",
    "identity": "protocol_buffers_image",
    "value": "",
    "rules": 
      "required": true
    ,
    "properties": 
      "label": "Image Controller",
      "readonly": false,
      "tip": "Image Controller Tips"
    ,
    "business": 
      "client_visible": true,
      "disabled": false
    ,
    "disabled": true
  ,
  
    "type": "number",
    "identity": "protocol_buffers_number",
    "value": 1,
    "rules": 
      "min_value": 0,
      "max_value": 100,
      "required": true
    ,
    "properties": 
      "label": "Number Controller",
      "readonly": false,
      "default_value": 1,
      "width": "100%",
      "step": 1,
      "placeholder": "Number Placeholder"
    ,
    "business": 
      "client_visible": true,
      "disabled": false
    ,
    "disabled": true
  
]

虽然input、select、switch、image、number对象的级别相同,但是对象的属性(值、规则、属性)还是有区别的。

如何在没有接口和继承机制的情况下建立相应的数据模型?将每个对象完全分开?

【问题讨论】:

【参考方案1】:
文件结构
proto                   
├─ form                 
│  ├─ business.proto    
│  ├─ input.proto       
│  ├─ select.proto      
│  ├─ image.proto       
│  ├─ number.proto      
│  ├─ ...               
│  └─ switch.proto      
└─ items                
   └─ index.proto       
基本数据模型
// form/business.proto
syntax = "proto3";

package form;

message Business 
  bool client_visible = 1;
  bool disabled = 2;

// form/input.proto
syntax = "proto3";

package form;

import "form/business.proto";

message InputRules 
  bool required = 1;
  uint32 max_length = 2;


message InputProperties 
  string label = 1;
  bool readonly = 2;
  string default_value = 3;
  string width = 4;
  string placeholder = 5;


message Input 
  string type = 1;
  string identity = 2;
  string value = 3;
  InputRules rules = 4;
  InputProperties properties = 5;
  Business business = 6;
  bool disabled = 7;

// form/image.proto
syntax = "proto3";

package form;

import "form/business.proto";

message ImageRules 
  bool required = 1;


message ImageProperties 
  string label = 1;
  bool readonly = 2;
  string tip = 3;


message Image 
  string type = 1;
  string identity = 2;
  string value = 3;
  ImageRules rules = 4;
  ImageProperties properties = 5;
  Business business = 6;
  bool disabled = 7;

实体模型
// items/index.proto
syntax = "proto3";

package items;

import "form/input.proto";
import "form/textarea.proto";
import "form/radio.proto";
import "form/checkbox.proto";
import "form/select.proto";
import "form/date.proto";
import "form/switch.proto";
import "form/image.proto";
import "form/number.proto";

message Nested 
  uint32 id = 1;

  oneof items 
    form.Input input = 2;
    form.Textarea textarea = 3;
    form.Radio radio = 4;
    form.Checkbox checkbox = 5;
    form.Select select = 6;
    form.Date date = 7;
    form.Switch switch = 8;
    form.Image image = 9;
    form.Number number = 10;
  


message SearchRequest 
  string query = 1;
  int32 page_number = 2;
  int32 per_page = 3;


message SearchResponse 
  repeated Nested items = 1;


service SearchService 
  rpc Search(SearchRequest) returns (SearchResponse);

【讨论】:

以上是关于如何在PHP中处理Protocol Buffers数据的主要内容,如果未能解决你的问题,请参考以下文章

如何在 PHP 中处理 Protocol Buffers 数据

如何在 Protocol Buffers 中构建数据模型

使用 Protocol Buffers 代替 JSON 的五个原因

如何从 Protocol Buffers .NET 代码生成 .proto 文件?

我如何知道在使用 Protocol Buffers 库时收到了啥消息?

Protocol buffers--python 实践 protocol buffers vs json