-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
123 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Custom usage | ||
|
||
It is relatively easy to create custom objects. | ||
There are comprehensive accessors, as well basic defaults. To see these in detail, review the reference section. | ||
|
||
Note: when manually creating the objects, they should be formed as per structure of a json api type. | ||
These classes offer basic validation, but it is not comprehensive. | ||
|
||
## Example Approach | ||
|
||
Here is an example of fluently populating then exporting a json api response: | ||
|
||
```php | ||
use Floor9design\JsonApiFormatter\Models\JsonApiFormatter; | ||
|
||
// Some example data: | ||
$id = "2"; | ||
$type = 'test'; | ||
$attributes = ['test' => 'data']; | ||
|
||
$json_api_response = new JsonApiFormatter(); | ||
|
||
$response = $json_api_response | ||
// unset the items that are not relevant | ||
->unsetErrors() | ||
->unsetLinks() | ||
// add the relevant items | ||
->autoIncludeJsonapi() | ||
->addData(['id' => $id]) | ||
->addData(['type' => $type]) | ||
->addData(['attributes' => $attributes]) | ||
->addMeta(['info' => 'object created']) | ||
// export the response | ||
->export(); | ||
``` | ||
|
||
## Reference | ||
|
||
The following accessors exist: | ||
|
||
* `getData()` | ||
* `getErrors()` | ||
* `getMeta()` | ||
* `getJsonapi()` | ||
* `getContentType()` | ||
|
||
The following methods are available to manually change the object: | ||
|
||
* `setData()` | ||
* `setErrors()` | ||
* `setMeta()` | ||
* `setContentType()` | ||
* `setJsonapi()` | ||
|
||
|
||
If you are creating the objects fluently, the following `add` methods can be used to avoid get/set syntactic hassle. | ||
|
||
* `addData()` | ||
* `addErrors()` | ||
* `addMeta()` | ||
|
||
To offer flexibility, the following `unset` methods also exist: | ||
|
||
* `unsetData()` | ||
* `unsetErrors()` | ||
* `unsetMeta()` | ||
* `unsetData()` | ||
|
||
If you are setting up an object manually, the following auto populates the standard values: | ||
|
||
* `autoIncludeJsonapi()` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Simple use | ||
|
||
## data resources | ||
|
||
Data resources can be quickly created using the `dataResourceResponse()` function. | ||
|
||
The following arguments are required: | ||
|
||
* `id` : ID for the data resource (string) | ||
* `type` : type of resource (string) | ||
* `attributes` : array of resource properties | ||
|
||
```php | ||
use Floor9design\JsonApiFormatter\Models\JsonApiFormatter; | ||
|
||
// Some example data: | ||
$id = "2"; | ||
$type = 'test'; | ||
$attributes = ['test' => 'data']; | ||
|
||
$json_api_response = new JsonApiFormatter(); | ||
|
||
$response = $json_api_response->dataResourceResponse($id, $type, $attributes); | ||
``` | ||
|
||
## errors | ||
|
||
Errors can be quickly created using the `errorResponse()` function. | ||
|
||
The following arguments are required: | ||
|
||
* `errors` : array of error objects | ||
|
||
```php | ||
use Floor9design\JsonApiFormatter\Models\JsonApiFormatter; | ||
|
||
// an example error: | ||
$error = (object)['test' => 'error']; | ||
$errors = [$error]; | ||
|
||
$json_api_response = new JsonApiFormatter(); | ||
|
||
$response = $json_api_response->errorResponse($errors); | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters