{"id":21599173,"url":"https://github.com/gridonic/jsonresponse","last_synced_at":"2025-04-11T01:05:20.402Z","repository":{"id":22035957,"uuid":"25364239","full_name":"gridonic/JsonResponse","owner":"gridonic","description":"Nice structured JsonResponses for the HttpFoundation-Component of Symfony","archived":false,"fork":false,"pushed_at":"2016-10-26T07:06:38.000Z","size":23,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-11-18T09:39:36.950Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gridonic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-10-17T15:23:27.000Z","updated_at":"2020-09-13T08:52:14.000Z","dependencies_parsed_at":"2022-08-18T17:50:39.092Z","dependency_job_id":null,"html_url":"https://github.com/gridonic/JsonResponse","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gridonic%2FJsonResponse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gridonic%2FJsonResponse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gridonic%2FJsonResponse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gridonic%2FJsonResponse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gridonic","download_url":"https://codeload.github.com/gridonic/JsonResponse/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226201962,"owners_count":17589343,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-24T18:14:35.406Z","updated_at":"2024-11-24T18:14:35.866Z","avatar_url":"https://github.com/gridonic.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JsonResponse\n\nProvides simple classes for JSON responses that adhere to a standardized structure.\nSince JSON responses may have very different formats, this package supports the specific\n[JSend](http://labs.omniti.com/labs/jsend) format defined at http://labs.omniti.com/labs/jsend.\n\nThis package is an extension of the [HttpFoundation\\JsonResponse](http://symfony.com/doc/current/components/http_foundation/introduction.html)\nclass from the [Symfony](http://symfony.com/) package.\n\n[![Build Status](https://travis-ci.org/gridonic/JsonResponse.svg?branch=master)](https://travis-ci.org/gridonic/JsonResponse)\n\n## Install\n\nThe recommended way to install JsonResponse is [through composer](http://getcomposer.org).\n\nYou can either add it as a depedency to your project using the command line:\n\n    $ composer require gridonic/json-response\n\nor by adding it directly to your ```composer.json``` file:\n\n```json\n{\n    \"require\": {\n        \"gridonic/json-response\": \"1.*\"\n    }\n}\n```\n\nRun these two commands to install it:\n\n    $ curl -s http://getcomposer.org/installer | php\n    $ php composer.phar install\n\nNow you can add the autoloader, and you will have access to the library:\n\n```php\n\u003c?php\nrequire 'vendor/autoload.php';\n```\n\n## JsonResponse\n\nWe differentiate between two different types of Responses:\n* SuccessJsonResponse\n* ErrorJsonResponse\n\n### SuccessJsonResponse\n\nParameter | Type | Needed? | Default value | Description\n--- | --- | --- | --- | ---\n`$data` | mixed | optional | null | The response data\n`$message` | string | optional | null | A success message\n`$title` | string | optional | null | A success title\n`$status` | integer | optional | 200 | The response status code\n`$headers` | array | optional | array() | An array of response headers\n\n```php\n/**\n * @throws \\InvalidArgumentException\n */\n new SuccessJsonResponse($data, 'Success message', 'Success title', 200);\n```\n\n```json\n{\n    \"status\" : \"success\",\n    \"data\" : { ... },\n    \"message\" : \"Sucess message\",\n    \"title\" : \"Success title\"\n}\n```\n\n### ErrorJsonResponse\n\nParameter | Type | Needed? | Default value | Description\n--- | --- | --- | --- | ---\n`$data` | mixed | optional | null | The response data\n`$message` | string | required |  | The error message\n`$title` | string | optional | null | An error title\n`$status` | integer | optional | 400 | The response status code\n`$errorCode` | string | optional | null | An individual error code\n`$errors` | array | optional | array() | An array of errors\n`$headers` | array | optional | array() | An array of response headers\n\n```php\n/**\n * @throws \\InvalidArgumentException\n */\n new ErrorJsonResponse($data, 'Error message', 'Error title', 400, 'e311', $errors);\n```\n\n```json\n{\n    \"status\" : \"error\",\n    \"data\" : { ... },\n    \"message\" : \"Error message\",\n    \"title\" : \"Error title\",\n    \"error_code\" : \"e311\",\n    \"errors\" : { ... }\n}\n```\n\n### JSend\nOur Responses are based on the Model of JSend.\nYou can find the documentation of JSend on the [JSend website](http://labs.omniti.com/labs/jsend)\n\n## Usage\n\n\n### SuccessJsonResponse\nUse a `Gridonic\\JsonResponse\\SuccessJsonResponse` to build a structured JSON Response.\n\n#### Empty SuccessJsonResponse\n\n```php\nnew SuccessJsonResponse();\n```\n\n```json\n{\n    \"status\" : \"success\",\n    \"data\" : null\n}\n```\n\n#### SuccessJsonResponse with Content\n\n```php\n$data = array(\n    'post' =\u003e array(\n        'id' =\u003e 1,\n        'title' =\u003e 'A blog post',\n    )\n);\n$message = 'The Blog post was successfully created.';\n$title = 'Successfully created!';\n$statusCode = 205;\n\nnew SuccessJsonResponse($data, $message, $title, $statusCode);\n```\n\n```json\n{\n    \"status\" : \"success\",\n    \"data\" : {\n        \"post\": {\n            \"id\" : 1,\n            \"title\" : \"A blog post\"\n        }\n    },\n    \"message\" : \"The Blog post was successfully created.\",\n    \"title\" : \"Successfully created!\"\n}\n```\n\n### SuccessJsonResponse\n\nUse a `Gridonic\\JsonResponse\\ErrorJsonResponse` to build a structured JSON Response.\n\n#### ErrorJsonResponse with Message\n\n```php\n$message = 'Oups, data is missing.';\n\nnew ErrorJsonResponse(null, $message); // you have to send a message!\n```\n\n```json\n{\n    \"status\" : \"error\",\n    \"data\" : null,\n    \"message\" : \"Oups, data is missing\"\n}\n```\n\n#### ErrorJsonResponse with Content\n\n```php\n$data = array(\n    'post' =\u003e array(\n        'title' =\u003e 'A blog post',\n    )\n);\n$message = 'Oups, data is not correct.';\n$title = 'An error occured!';\n$statusCode = 400;\n$errorCode = e311;\n$errors = array(\n    'body' =\u003e 'The parameter is missing.',\n    'title' =\u003e 'This parameter is too long.'\n);\n\nnew ErrorJsonResponse($data, $message, $title, $statusCode, $errorCode, $errors);\n```\n\n```json\n{\n    \"status\" : \"error\",\n    \"data\" : {\n        \"post\": {\n            \"title\" : \"A blog post\"\n        }\n    },\n    \"message\" : \"Oups, data is missing\",\n    \"title\" : \"An error occured\",\n    \"error_code\" : \"e311\",\n    \"errors\" : {\n        \"body\" : \"The parameter is missing.\",\n        \"title\" : \"This parameter is too long.\"\n    }\n}\n```\n\n## Major \u0026 Minor [Releases](https://github.com/gridonic/JsonResponse/releases)\n##### 1.1.0\nNew structure of the responses\n\n##### 1.0.0\nInitial Release\n\n## Licence\n\nThe JsonResponse is licensed under the [MIT license](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgridonic%2Fjsonresponse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgridonic%2Fjsonresponse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgridonic%2Fjsonresponse/lists"}