alirezajavadi/jsonize

JSONize:轻松标准化JSON响应!使用我们的简洁库确保应用程序的一致性。简化数据格式,提高可读性,节省时间。JSONize使开发者能够生成干净、有序的JSON输出,非常适合API、网页和移动应用程序。今天革新你的工作流程!

1.6.5 2024-07-12 20:51 UTC

This package is auto-updated.

Last update: 2024-10-01 00:08:06 UTC


README

JSONize:轻松标准化JSON响应!

使用我们的简洁库确保应用程序的一致性。简化数据格式,提高可读性,节省时间。JSONize使开发者能够生成干净、有序的JSON输出,非常适合API、网页和移动应用程序。今天革新你的工作流程!

安装

您可以通过Composer安装JSONize

composer require alirezajavadi/jsonize

用法

一旦安装了JSONize,您就可以在项目中开始使用它。以下是如何使用JSONize的基本示例

v1.5.5版本中的新功能

简单语法

为了更简洁的语法,请使用

<?php

use JSONize\App\Easy\Response;

// Example 1: Success
Response::message("Deleted Successfully");
/*
{
    "success": true,
    "message": "Deleted Successfully",
    "data": null,
    "status": [
        200,
        "ok"
    ]
}
*/

// Example 2: Error
Response::message("User Not Found")->status(404);
/*
{
    "success": false,
    "message": "User Not Found",
    "data": null,
    "status": [
        404,
        "Not Found"
    ]
}
*/

// Example 3: No Content
Response::status(204);
/*
{
    "success": true,
    "message": null,
    "data": null,
    "status": [
        204,
        "No Content"
    ]
}
*/

// Example 4: Data with Custom Key
Response::data(["id" => 1, "name" => "Item"], "item");
/*
{
    "success": true,
    "message": null,
    "item": {
        "id": 1,
        "name": "Item"
    },
    "status": [
        200,
        "ok"
    ]
}
*/

// Example 5: Data with Custom Key and Hide Status
Response::data(["id" => 1, "name" => "Item"], "item")->hideStatus();
/*
{
    "success": true,
    "message": null,
    "item": {
        "id": 1,
        "name": "Item"
    }
}
*/

// Example 6: Data with Custom Key and Custom Status Message
Response::data(["id" => 1, "name" => "Item"], "item")->status(142, "example info")->hideMessage();
/*
{
    "success": true,
    "item": {
        "id": 1,
        "name": "Item"
    },
    "status": [
        142,
        "example info"
    ]
}
*/

// Example 7: Data with Metadata
Response::data(["id" => 1, "name" => "Item"])
         ->message("Item retrieved successfully")
         ->status(200);
/*
{
    "success": true,
    "message": "Item retrieved successfully",
    "data": {
        "id": 1,
        "name": "Item"
    },
    "status": [
        200,
        "ok"
    ]
}
*/

// Example 8: easy error message
Response::error("Something went wrong");
/*
{
    "success": false,
    "message": "Something went wrong",
    "data": null,
    "status": [
        500,
        "Internal Server Error"
    ]
}
*/
or
// Example 8: easy error message
Response::error("Something went wrong", 400);
/*
{
    "success": false,
    "message": "Something went wrong",
    "data": null,
    "status": [
        400,
        "Bad Request"
    ]
}
*/

高效内存使用

为了高效使用和内存安全,请使用

<?php
// Init
use JSONize\App\Efficient\Response;

$response = Response::getInstance();

// Example 1: Success
$response->message("Deleted Successfully")->get();
/*
{
    "success": true,
    "message": "Deleted Successfully",
    "data": null,
    "status": [
        200,
        "ok"
    ]
}
*/

// Example 2: Error
$response->message("User Not Found")->status(404)->get();
/*
{
    "success": false,
    "message": "User Not Found",
    "data": null,
    "status": [
        404,
        "Not Found"
    ]
}
*/

// Example 3: No Content
$response->status(204)->get();
/*
{
    "success": true,
    "message": null,
    "data": null,
    "status": [
        204,
        "No Content"
    ]
}
*/

// Example 4: Data with Custom Key
$response->data(["id" => 1, "name" => "Item"], "item")->get();
/*
{
    "success": true,
    "message": null,
    "item": {
        "id": 1,
        "name": "Item"
    },
    "status": [
        200,
        "ok"
    ]
}
*/

// Example 5: Data with Custom Key And Hide Status
$response->data(["id" => 1, "name" => "Item"], "item")->hideStatus()->get();
/*
{
    "success": true,
    "message": null,
    "item": {
        "id": 1,
        "name": "Item"
    },
}
*/

// Example 6: Data with Custom Key And Custom Status Message
$response->data(["id" => 1, "name" => "Item"], "item")->status(142, "example info")->get();
/*
{
    "success": true,
    "message": null,
    "item": {
        "id": 1,
        "name": "Item"
    },
    "status": [
        142,
        "example info"
    ]
}
*/

// Example 7: Data with Metadata
$response->data(["id" => 1, "name" => "Item"])
         ->message("Item retrieved successfully")
         ->status(200)
         ->get();
/*
{
    "success": true,
    "message": "Item retrieved successfully",
    "data": {
        "id": 1,
        "name": "Item"
    },
    "status": [
        200,
        "ok"
    ]
}
*/

// Example 8: easy error message
Response::error("Something went wrong")->get();
/*
{
    "success": false,
    "message": "Something went wrong",
    "data": null,
    "status": [
        500,
        "Internal Server Error"
    ]
}
*/
or
// Example 8: easy error message
Response::error("Something went wrong", 400)->get();
/*
{
    "success": false,
    "message": "Something went wrong",
    "data": null,
    "status": [
        400,
        "Bad Request"
    ]
}
*/

// Note: `get()` is important to use.

贡献

欢迎贡献!任何希望贡献的人都可以向此文件添加新的HTTP状态码:System/Traits/HasStatus.php。提交拉取请求或打开问题报告任何问题或建议。

许可

JSONize是开源软件,采用MIT许可。

作者

这个库由Alireza Javadi创建和维护。您可以通过e@alirezajawadi.ir联系我。