-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractBoxModel.php
126 lines (116 loc) · 3.81 KB
/
AbstractBoxModel.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* Copyright 2011 Felix Ostrowski, hbz
*
* This file is part of Phresnel.
*
* Phresnel is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Phresnel is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
* more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Phresnel. If not, see <http://www.gnu.org/licenses/>.
*/
abstract class AbstractBoxModel {
/**
* TODO: description.
*
* @var mixed Defaults to array().
*/
protected $_namespaces = array();
/**
* A chain of XSLT files to be executed
* on the renderes output.
*
* @var mixed Defaults to array().
*/
protected $_postTransformations = array();
protected abstract function _render();
/**
* Postprocess and return renderers output.
*
* @return string output
*/
public function render() {
$raw = $this->_render();
// supressing warnings
@$output = DomDocument::loadXml($raw);
// non-valid xml-input, return as is
if (false === $output) return $raw;
foreach ($this->_postTransformations as $trans) {
// supressing warnings
@$trans = DomDocument::load($trans);
// non-valid xml-input, skip
if (false === $trans) continue;
$xslt = new XSLTProcessor();
$xslt->importStylesheet($trans);
$output = $xslt->transformToDoc($output);
}
return $output->saveXml($output->documentElement);
}
/**
* TODO: short description.
*
* @param Lens $lens
*/
public function __construct(Lens $lens) {
$this->_lensObj = $lens;
$this->_lens = $lens->getLensURI();
$this->_lensDef = $lens->getLensGraph();
$this->_resourceURI = $lens->getResourceURI();
$this->_data = $lens->getData();
$this->_namespaces = Phresnel::$_namespaces;
}
/**
* TODO: short description.
*
* @param mixed $trans
* @return TODO
*/
public function registerPostTransformation($trans) {
$this->_postTransformations[] = $trans;
}
/**
* Maps prefixes to namespaces.
*
* @param LibRDF_URINode $uri The URI to split.
* @return array The local part of the name and the prefix.
*/
protected function _nssplit(LibRDF_URINode $uri) {
if (false !== strpos($uri, '#')) {
$name = substr($uri, strrpos($uri, '#') + 1, -1);
$ns = substr($uri, 1, strrpos($uri, '#'));
} else {
$name = substr($uri, strrpos($uri, '/') + 1, -1);
$ns = substr($uri, 1, strrpos($uri, '/'));
}
if (!array_key_exists($ns, $this->_namespaces)) {
$this->_namespaces[$ns] = "ns" . count($this->_namespaces);
}
return array($name, $this->_namespaces[$ns]);
}
/**
* Turns an RDF list into an ordered PHP array.
*
* @param mixed $listURI The URI of the head of the list.
* @return array The list as an array.
*/
protected function _unlist($listURI) {
$lst = array();
try {
$lst[] = $this->_lensDef->getTarget($listURI, new
LibRDF_URINode(RDF."first"));
} catch (LibRDF_LookupError $e) {
return $lst;
}
$tail = $this->_lensDef->getTarget($listURI, new
LibRDF_URINode(RDF."rest"));
return array_merge($lst, $this->_unlist($tail));
}
}