This repository was archived by the owner on Jan 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathCollection.php
More file actions
311 lines (273 loc) · 7.79 KB
/
Collection.php
File metadata and controls
311 lines (273 loc) · 7.79 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/**
* @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
* @author CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
* @license Dual licensed under the MIT (MIT-LICENSE.txt) and LGPL (LGPL-LICENSE.txt) licenses.
* @package Midgard.CreatePHP
*/
namespace Midgard\CreatePHP\Entity;
use Midgard\CreatePHP\Node;
use Midgard\CreatePHP\Metadata\RdfTypeFactory;
use Midgard\CreatePHP\Metadata\TypeNotFoundException;
use Midgard\CreatePHP\NodeInterface;
/**
* Collection holder. Acts at the same time as a property to a parent entity and
* and as a holder for entities of other objects which are linked to the first one
* with some kind of relation
*
* @package Midgard.CreatePHP
*/
class Collection extends Node implements CollectionInterface
{
/**
* @var string
*/
protected $_identifier;
/**
* @var RdfTypeFactory
*/
protected $_typeFactory;
/**
* @var int
*/
protected $_position = 0;
/**
* List of type names this collection may contain
* @var array
*/
protected $_typenames = array();
/**
* @param string $identifier the php property name used for this collection
* @param RdfTypeFactory $typeFactory the typefactory to use with fixed child
* types
* @param array $config application specific configuration to carry in this
* collection
*/
public function __construct($identifier, RdfTypeFactory $typeFactory, array $config = array())
{
parent::__construct($config);
$this->_identifier = (string) $identifier;
$this->_typeFactory = $typeFactory;
}
/**
* {@inheritDoc}
*
* @api
*/
public function setRel($rel)
{
$this->setAttribute('rel', $rel);
}
/**
* {@inheritDoc}
*
* @api
*/
public function getRel()
{
return $this->getAttribute('rel');
}
/**
* {@inheritDoc}
*
* @api
*/
public function setRev($rev)
{
$this->setAttribute('rev', $rev);
}
/**
* {@inheritDoc}
*
* @api
*/
public function getRev()
{
if (empty($this->_attributes['rev']) && count($this->_typenames) > 0) {
$rev = null;
foreach ($this->getTypes() as $child) {
$revs = $child->getRevOptions();
if (count($revs) !== 1) {
$type = $child->getRdfType();
throw new \Exception("Type $type in this collection does not specify exactly one possible rev attribute, please specify the rev attribute to use with this collection explicitly.");
}
if (null == $rev) {
$rev = reset($revs);
break;
} elseif ($rev !== reset($revs)) {
$type = $child->getRdfType();
throw new \Exception("Type $type in this collection does not have the same rev attribute as the previous types, please fix your configuration.");
}
}
$this->setRev($rev);
}
return $this->getAttribute('rev');
}
/**
* {@inheritDoc}
*
* @api
*/
public function addTypeName($type)
{
$this->_typenames[$type] = $type;
}
/**
* {@inheritDoc}
*
* @api
*/
public function getTypes()
{
$types = array();
foreach ($this->_typenames as $typename) {
$types[$typename] = $this->_typeFactory->getTypeByRdf($typename);
if (null == $types[$typename]) {
throw new TypeNotFoundException($typename);
}
}
return $types;
}
/**
* {@inheritDoc}
*
* @api
*/
public function getIdentifier()
{
return $this->_identifier;
}
/**
* {@inheritDoc}
*
* @api
*/
public function createWithParent(EntityInterface $parent)
{
$collection = clone $this;
$collection->loadFromParent($parent);
return $collection;
}
/**
* Never call this method directly, but use createWithParent on your CollectionDefinition
* to get a collection tied to concrete data.
*
* @private
*
* @param EntityInterface $parent
*/
public function loadFromParent(EntityInterface $parent)
{
$this->_children = array();
$object = $parent->getObject();
$parentMapper = $parent->getMapper();
$children = $parentMapper->getChildren($object, $this);
// create entities for children
foreach ($children as $child) {
if (count($this->_typenames) === 1) {
$type = $this->_typeFactory->getTypeByRdf(current($this->_typenames));
} else {
$type = $this->_typeFactory->getTypeByObject($child);
}
foreach ($type->getVocabularies() as $prefix => $uri) {
$this->setAttribute('xmlns:' . $prefix, $uri);
}
$this->_children[] = $type->createWithObject($child);
}
if ($this->_parent->isEditable($object) && sizeof($this->_children) == 0 && count($this->_typenames) == 1) {
// create an empty element to allow adding new elements to an empty editable collection
$type = $this->_typeFactory->getTypeByRdf(reset($this->_typenames));
$mapper = $type->getMapper();
$object = $mapper->prepareObject($type, $object);
$entity = $type->createWithObject($object);
if ($entity instanceof NodeInterface) {
/** @var $entity NodeInterface */
$entity->setAttribute('style', 'display:none');
}
$this->_children[] = $entity;
}
}
/**
* {@inheritDoc}
*
* @api
*/
public function renderStart($tag_name = false)
{
// render this for admin users only
if (!$this->_parent->isEditable()) {
$this->unsetAttribute('about');
}
if (empty($this->_attributes['rev'])) {
$this->getRev(); // trigger determining the rev attribute
}
return parent::renderStart($tag_name);
}
/**
* {@inheritDoc}
*
* Overwrite to not output about attribute again if parent is rendering
*/
public function renderAttributes($attributesToSkip = array())
{
if ($this->_parent && $this->_parent->isRendering()) {
$attributesToSkip[] = 'about';
}
return parent::renderAttributes($attributesToSkip);
}
/**
* {@inheritDoc}
*
* @api
*/
public function renderContent()
{
$ret = '';
foreach ($this->_children as $child) {
/** @var $child NodeInterface */
$ret .= $child->render();
}
return $ret;
}
/* ----- arrayaccess and iterator implementation methods ----- */
public function rewind()
{
$this->_position = 0;
}
public function current()
{
return $this->_children[$this->_position];
}
public function key()
{
return $this->_position;
}
public function next()
{
++$this->_position;
}
public function valid()
{
return isset($this->_children[$this->_position]);
}
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
$this->_children[] = $value;
} else {
$this->_children[$offset] = $value;
}
}
public function offsetExists($offset)
{
return isset($this->_children[$offset]);
}
public function offsetUnset($offset)
{
unset($this->_children[$offset]);
}
public function offsetGet($offset)
{
return isset($this->_children[$offset]) ? $this->_children[$offset] : null;
}
}