Skip to content

Commit

Permalink
Updates to documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
elb98rm committed Oct 7, 2020
1 parent 540338b commit fdec514
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 62 deletions.
71 changes: 71 additions & 0 deletions docs/project/usage-custom.md
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()`
46 changes: 46 additions & 0 deletions docs/project/usage-simple.md
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);
```


68 changes: 6 additions & 62 deletions docs/project/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,73 +23,17 @@ $json_api_response = new JsonApiFormatter();

## Simple use

### data resources
Most use cases fit the included simple-use functions. These are described as follows:

Data resources can be quickly created using the `dataResourceResponse()` function.
* [simple usage](usage-simple.md)

```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);
```

## Using an object manually

The following accessors exist:

* `getData()`
* `getErrors()`
* `getMeta()`
* `getJsonapi()`
* `getContentType()`

The following methods are available to manually change the object:

* `setData()`
* `setErrors()`
* `setMeta()`

If you are creating the objects fluently, the following `add` methods can be used to avoid get/set syntactic hassle.
## Creating a custom object

* `addData()`
* `addErrors()`
* `addMeta()`
If you need to make a custom object, then this is also possible:

The following functions also exist to change the standard values for a response:

* `setContentType()`
* `setJsonapi()`

If manually creating the objects, they should be formed as per structure of a json api type.
There is basic validation, but it is not comprehensive.

```php
use Floor9design\JsonApiFormatter\Models\JsonApiFormatter;

// Some example data:
$id = "2";
$type = 'test';
$attributes = ['test' => 'data'];

$json_api_response = new JsonApiFormatter();

$json_api_response->addData(['id' => $id]);
$json_api_response->addData(['type' => $type]);
$json_api_response->addData(['attributes' => $attributes]);
$json_api_response->addMeta(['info' => 'object created']);

$response = $json_api_response->dataResourceResponse();

```
* [custom usage](usage-custom.md)

## Responses
## Outside of the JSON

The JSON API requires a specific `Content-Type` header, which can be loaded by `getContentType()`:

Expand Down

0 comments on commit fdec514

Please sign in to comment.