ArrayOutputStrategy.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
15 
16 /**
17  * ArrayOutputStrategy outputs an object's content into an array.
18  *
19  * @author ingo herwig <ingo@wemove.com>
20  */
22 
24 
25  /**
26  * Constructor
27  * @param $writeValueProperties Boolean whether to write value properties or not (default: _false_)
28  */
29  public function __construct($writeValueProperties=false) {
30  $this->_writeValueProperties = $writeValueProperties;
31  }
32 
33  /**
34  * @see OutputStrategy::writeHeader
35  */
36  public function writeHeader() {
37  // do nothing
38  }
39 
40  /**
41  * @see OutputStrategy::writeFooter
42  */
43  public function writeFooter() {
44  // do nothing
45  }
46 
47  /**
48  * @see OutputStrategy::writeObject
49  */
50  public function writeObject(PersistentObject $obj) {
51  $content = array();
52  $content['oid'] = $obj->getOID();
53  $content['type'] = $obj->getType();
54  $content['properties'] = array();
55  foreach($obj->getPropertyNames() as $name) {
56  $content['properties'][$name] = $this->writeValue($obj->getProperty($name));
57  }
58  $content['values'] = array();
59  foreach($obj->getValueNames() as $name) {
60  $content['values'][$name] = array();
61  $value = $this->writeValue($obj->getValue($name));
62  $content['values'][$name]['value'] = $value;
63  if ($this->_writeValueProperties) {
64  $content['values'][$name]['properties'] = array();
65  foreach($obj->getValuePropertyNames($name) as $propertyName) {
66  $content['values'][$name]['properties'][$propertyName] = $this->writeValue($obj->getValueProperty($name, $propertyname));
67  }
68  }
69  }
70  return $content;
71  }
72 
73  /**
74  * Write the objects content.
75  * @param $value The value to write.
76  */
77  private function writeValue($value) {
78  $content = '';
79  if (is_array($value)) {
80  $content = array();
81  for ($i=0; $i<sizeof($value); $i++) {
82  $content[] = utf8_encode($value[$i]);
83  }
84  }
85  else if ($value instanceof PersistentObject) {
86  $content = $this->writeObject($value);
87  }
88  else {
89  $content = utf8_encode($value);
90  }
91  return $content;
92  }
93 }
94 ?>
getType()
Get the type of the object.
getOID()
Get the object id of the PersistentObject.
OutputStrategy defines the interface for classes that write an object's content to a destination (cal...
getValueNames($excludeTransient=false)
Get the names of all items.
getProperty($name)
Get the value of a named property in the object.
getPropertyNames()
Get the names of all properties in the object.
ArrayOutputStrategy outputs an object's content into an array.
getValuePropertyNames($name)
Get the names of all properties of a value in the object.
getValue($name)
Get the value of a named item.
getValueProperty($name, $property)
Get the value of one property of a named item.
__construct($writeValueProperties=false)
Constructor.
PersistentObject defines the interface of all persistent objects.