-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.ODataServiceDoc.php
64 lines (62 loc) · 2.55 KB
/
class.ODataServiceDoc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
class ODataServiceDoc
{
private $collections = array();
public function registerCollection($collection)
{
array_push($this->collections, $collection);
}
public function serialize($fmt, $app)
{
if(trim($fmt) === '')
{
$fmt = 'application/json';
}
$types = explode(',', $fmt);
$count = count($types);
$base = $app->request->getUrl().$app->request->getRootUri();
for($i = 0; $i < $count; $i++)
{
if(strstr($types[$i], 'application/xml') !== false)
{
$app->response->headers->set('Content-Type', 'application/xml;odata.metadata=minimal');
$xml = new SimpleXmlElement('<?xml version="1.0" encoding="utf-8"?><service/>');
$xml->addAttribute('xmlns', 'http://www.w3.org/2007/app');
$xml->addAttribute('xmlns:atom', 'http://www.w3.org/2005/Atom');
$xml->addAttribute('xmlns:m', 'http://docs.oasis-open.org/odata/ns/metadata');
$xml->addAttribute('xml:base', $base);
$xml->addAttribute('m:context', $base.'/$metadata');
$workspace = $xml->addChild('workspace');
$title = $workspace->addChild('atom:title', 'Default');
$title->addAttribute('type', 'text');
foreach($this->collections as $collection)
{
$col = $workspace->addChild('collection');
$col->addAttribute('href', $collection->getUrl());
$title = $col->addChild('atom:title', $collection->getName());
$title->addAttribute('type', 'text');
}
echo $xml->asXml();
return;
}
else if(strstr($types[$i], 'application/json') !== false)
{
$app->response->headers->set('Content-Type', 'application/json;odata.metadata=minimal');
$resp = array();
$resp['@odata.context'] = $base.'/$metadata';
$collections = array();
foreach($this->collections as $collection)
{
array_push($collections, array('name'=>$collection->getName(), 'kind'=>'EntitySet', 'url'=>$collection->getUrl()));
}
$resp['value'] = $collections;
echo json_encode($resp);
return;
}
}
$app->response->setStatus(406);
echo 'Unknown format for Service Doc: '.$fmt;
error_log($fmt);
}
}
?>