source: sandbox/2.4.1-3/prototype/rest/hypermedia/hypermedia.php @ 6523

Revision 6523, 1.4 KB checked in by acoutinho, 12 years ago (diff)

Ticket #2766 - Melhorias e correcoes na api rest, criacao de novo cliente

  • Property svn:executable set to *
Line 
1<?php
2
3require_once('collection.php');
4
5class Hypermedia {
6
7    public $collection;
8    public $version = '0.1';
9
10    function setCollection($collection) {
11        $this->collection = $collection;
12    }
13
14    function getCollection() {
15        return $this->collection;
16    }
17
18    function getHypermedia($accept = 'json') {
19
20        $data = get_object_vars($this);
21
22        switch ($accept) {
23            case 'json':
24                return json_encode($data);
25                break;
26            case 'xml':
27                return $this->generateValidXmlFromArray($data);
28            default :
29                return json_encode($data);
30        }
31    }
32
33    function generateValidXmlFromObj(stdClass $obj, $node_block = 'nodes', $node_name = 'node') {
34        $arr = get_object_vars($obj);
35        return self::generateValidXmlFromArray($arr, $node_block, $node_name);
36    }
37
38    function generateValidXmlFromArray($array, $node_block = 'nodes', $node_name = 'node') {
39        $xml = '<?xml version="1.0" encoding="UTF-8" ?>';
40
41        $xml .= '<' . $node_block . '>';
42        $xml .= self::generateXmlFromArray($array, $node_name);
43        $xml .= '</' . $node_block . '>';
44
45        return $xml;
46    }
47
48    function generateXmlFromArray($array, $node_name) {
49        $xml = '';
50
51        if (is_array($array) || is_object($array)) {
52            foreach ($array as $key => $value) {
53                if (is_numeric($key)) {
54                    $key = $node_name;
55                }
56
57                $xml .= '<' . $key . '>' . self::generateXmlFromArray($value, $node_name) . '</' . $key . '>';
58            }
59        } else {
60            $xml = htmlspecialchars($array, ENT_QUOTES);
61        }
62
63        return $xml;
64    }
65
66}
67
68?>
Note: See TracBrowser for help on using the repository browser.