PHP code example of alirezajavadi / jsonize

1. Go to this page and download the library: Download alirezajavadi/jsonize library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alirezajavadi / jsonize example snippets




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"
    ]
}
*/


// 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.