Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ <h3>Errors</h3>
<li><a href="errors_all_options.php">All options</a></li>
</ul>

<h3>Misc</h3>
<ul>
<li><a href="meta_only.php">Meta-only use-cases</a></li>
</ul>

<footer>
<p><a href="https://github.com/lode/jsonapi">GitHub</a></p>
</footer>
Expand Down
24 changes: 24 additions & 0 deletions examples/meta_only.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use alsvanzelf\jsonapi;

ini_set('display_errors', 1);
error_reporting(-1);

require '../vendor/autoload.php';

/**
* there are a few use-cases for sending meta-only responses
* in such cases, use the response class
*
* prefer to actually send out a resource, error or collection
*/

$jsonapi = new jsonapi\response();
$jsonapi->add_meta('foo', 'bar');

/**
* sending the response
*/

$jsonapi->send_response();
25 changes: 25 additions & 0 deletions src/response.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,31 @@ public function __toString() {
return $this->get_json();
}

/**
* generates an array for the whole response body
*
* @see jsonapi.org/format
*
* @return array, containing:
* - links
* - meta
*/
public function get_array() {
$response = array();

// links
if ($this->links) {
$response['links'] = $this->links;
}

// meta data
if ($this->meta_data) {
$response['meta'] = $this->meta_data;
}

return $response;
}

/**
* returns the whole response body as json
* it generates the response via ->get_array()
Expand Down