[6019] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /** |
---|
| 4 | * Multiple representation example |
---|
| 5 | * |
---|
| 6 | * A representation is an output format for a resource. A single resource can have |
---|
| 7 | * many output representations that can differ depending on the request data. |
---|
| 8 | * |
---|
| 9 | * Representations are built up as strings within the Response object and it is |
---|
| 10 | * down to the programmer to implement the desired logic for choosing and building |
---|
| 11 | * a required representation. |
---|
| 12 | * |
---|
| 13 | * This example produces three different representations of the resource data |
---|
| 14 | * depending on the request accept header information. |
---|
| 15 | * |
---|
| 16 | * @namespace Tonic\Examples\Multirep |
---|
| 17 | * @uri /multirep |
---|
| 18 | */ |
---|
| 19 | class MultiRepresentationResource extends Resource { |
---|
| 20 | |
---|
| 21 | var $data = array( |
---|
| 22 | 'secret' => 'monkey', |
---|
| 23 | 'hidden' => 'squirrel' |
---|
| 24 | ); |
---|
| 25 | |
---|
| 26 | /** |
---|
| 27 | * Handle a GET request for this resource |
---|
| 28 | * @param Request request |
---|
| 29 | * @return Response |
---|
| 30 | */ |
---|
| 31 | function get($request) { |
---|
| 32 | |
---|
| 33 | $response = new Response($request); |
---|
| 34 | $response->code = Response::OK; |
---|
| 35 | |
---|
| 36 | // select the most acceptable format of the given types from the request |
---|
| 37 | $format = $request->mostAcceptable(array( |
---|
| 38 | 'json', 'html', 'txt' |
---|
| 39 | )); |
---|
| 40 | |
---|
| 41 | switch ($format) { |
---|
| 42 | |
---|
| 43 | // obviously it might make more sense to put the HTML in another file |
---|
| 44 | // and use some kind of templating system |
---|
| 45 | case 'html': |
---|
| 46 | $response->addHeader('Content-type', 'text/html'); |
---|
| 47 | $response->body = '<!DOCTYPE html>'; |
---|
| 48 | $response->body .= '<html>'; |
---|
| 49 | $response->body .= '<head>'; |
---|
| 50 | $response->body .= '<title>HTML Representation</title>'; |
---|
| 51 | $response->body .= '</head>'; |
---|
| 52 | $response->body .= '<body>'; |
---|
| 53 | $response->body .= '<h1>HTML Representation</h1>'; |
---|
| 54 | $response->body .= '<table border="1">'; |
---|
| 55 | foreach ($this->data as $field => $value) { |
---|
| 56 | $response->body .= '<tr>'; |
---|
| 57 | $response->body .= '<th>'.htmlspecialchars($field).'</th>'; |
---|
| 58 | $response->body .= '<td>'.htmlspecialchars($value).'</td>'; |
---|
| 59 | $response->body .= '</tr>'; |
---|
| 60 | } |
---|
| 61 | $response->body .= '</table>'; |
---|
| 62 | $response->body .= '<h2>Explicit links to available formats</h2>'; |
---|
| 63 | $response->body .= '<a href="/multirep.txt">Plain text</a> '; |
---|
| 64 | $response->body .= '<a href="/multirep.json">JSON</a> '; |
---|
| 65 | $response->body .= '<a href="/multirep.html">HTML</a>'; |
---|
| 66 | $response->body .= '</body>'; |
---|
| 67 | $response->body .= '</html>'; |
---|
| 68 | break; |
---|
| 69 | |
---|
| 70 | case 'json': |
---|
| 71 | $response->addHeader('Content-type', 'application/json'); |
---|
| 72 | $response->body = json_encode($this->data); |
---|
| 73 | break; |
---|
| 74 | |
---|
| 75 | case 'txt': |
---|
| 76 | $response->addHeader('Content-type', 'text/plain'); |
---|
| 77 | $response->body = "Plain Text Representation\n"; |
---|
| 78 | $response->body .= "\n"; |
---|
| 79 | ob_start(); |
---|
| 80 | var_dump($this->data); |
---|
| 81 | $response->body .= ob_get_contents(); |
---|
| 82 | ob_end_clean(); |
---|
| 83 | break; |
---|
| 84 | |
---|
| 85 | // we don't have a suitable format, so do a 406 response instead |
---|
| 86 | default: |
---|
| 87 | $response->code = Response::NOTACCEPTABLE; |
---|
| 88 | $response->addHeader('Content-type', 'text/plain'); |
---|
| 89 | $response->body = "406 Not Acceptable\n"; |
---|
| 90 | break; |
---|
| 91 | |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | return $response; |
---|
| 95 | |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | } |
---|
| 99 | |
---|