承接 saad/json-response-builder 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

saad/json-response-builder

Composer 安装命令:

composer require saad/json-response-builder

包简介

this package semplifies sending API json response by using a standard and unique response structure

README 文档

README

Json Response Builder Package

Build Status License

Install

	composer require saad/json-response-builder

Change Log

V 1.3.1

add feature to merge meta when adding data

addData(str $key, mix $val, bool $merge_meta)

		
	$builder = new Saad\JsonResponseBuilder\JsonResponseBuilder();
	
	$builder->addData('users', [
		['name' => 'Ahmed Saad'],
		'meta' => [
			'name' => 'iam meta',
		]
	], true); // Note the third argument
	
	
	return $builder->getResponse();
	
	// Output
	
	{
        "success": true,
	
        "meta" : {
            'name' => 'iam meta',
        },
	
        "data": [
            'users': [
            		{"name": "Ahmed Saad"}
            ],
        ],
	
        "message": "Successfully Retrieved"
    }

V 1.3

Add Strict Mode and enabled by default: in strict mode if data or meta are empty it will set it's value to null instead of []

to turn of strickt mode, on constructor pass false to disable strickt mode so that it will return data, and meta as empty array if they are empty

you can set mode on instance by the method strictMode(bool)

Usage

  • Basic Example:

    inside your controller:

     	
     	$builder = new Saad\JsonResponseBuilder\JsonResponseBuilder();
     	
     	$builder->mergeData([
     		['name' => 'Ahmed Saad'],
     		['name' => 'John Doe']
     	]);
     	
     	$builder->addMeta([
     		'pagination' => [
     			'page' => 1,
     			'per_page' => 4,
     		]
     	]);
     	
     	return $builder->getResponse();
     	
  • the above example will output:

     
     	{
     		"success": true,
     		
     		"meta" : {
     			"pagination": {
     				"page": 1,
     				"per_page": 4
     			}
     		},
     		
     		"data": [
     			{"name": "Ahmed Saad"},
     			{"name": "John Doe"}
     		],
     		
     		"message": "Successfully Retrieved"
     	}

Available Methods

addData($key, $value)

Appends to data new member with the given key and value

	$builder->addData('doctors', ['ahmed', 'mohamed', 'saad']);
  $builder->addData('patients', ['patient1', 'patient3', 'patient3']);

	// Output data will be 

  "data": {
  	"doctors": ["ahmed", "mohamed", "saad"],
  	"patients" ["patient1", "patient4", "patient3"]
  },

mergeData($array)

merge given array with data with given array keys as keys, this is usefull when we want to send data as json array insteadof json object with key and value

this method also if the given array has key called 'meta' it will remove that key and add it to response meta

	$builder->mergeData(['ahmed', 'mohamed', 'meta' => ['key' => 'Iam Meta']]);

	// Output will be 

	{
   	"success": true,
   	"meta": {
   		"key": "Iam Meta"
   	},	
  	"data": [
  		"ahmed",
  		"mohamed"
  	],
  	"message": "Successfully Retrieved"
  }
  	 

addMeta($key, $value)

Appends to meta new member with the given key and value

mergeMeta($array)

merge given array with meta

addHeader($header, $value)

add header to response headers

success($response_message = null)

set response success status to true, and set response message if supplied.

setMessage($response_message = null)

set response message if supplied.

error($message = null, $error_code = null)

set response success status to false and set nessage and error code

 $builder->error('Fails!', 2345);

 // Output will be 

 {
  	"success": false,
  	"meta": null,	
 	"data": null,
 	'error': {
 		"message": "Fails!",
 		"code": 2345
 	}
 	"message": "Fails!"
 }

addError($key, mixed $value)

Add key to error array

 $builder->error('Fails!', 2345)
 	->addError('validation', 'validation value');

 // Output will be 

 {
  	"success": false,
  	"meta": null,	
 	"data": null,
 	'error': {
 		"message": "Fails!",
 		"code": 2345,
 		"validation": "validation value"
 	}
 	"message": "Fails!"
 }

setStatusCode(301)

set response status code.

strictMode(bool)

default value true

since V1.3

enable or disable strict mode.

 $builder->getResponse();

 // Strict Mode Enabled, Output will be 

 {
  	"success": false,
  	"meta": null,	
 	"data": null,
 	"message": ""
 }

 $builder->strictMode(false)->getResponse();

 // Strict Mode Disabled, Output will be 

 {
  	"success": false,
  	"meta":[],	
 	"data":[],
 	"message": ""
 }

getResponse($status_code = null)

set response status code if supplied, and return Response Object

License

The Laravel framework is open-sourced software licensed under the MIT license.README.md

saad/json-response-builder 适用场景与选型建议

saad/json-response-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.2k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2018 年 02 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 saad/json-response-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 saad/json-response-builder 我们能提供哪些服务?
定制开发 / 二次开发

基于 saad/json-response-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 9.2k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-02-16