SoapNodeSerializer.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  */
11 namespace wcmf\lib\model\impl;
12 
21 
22 /**
23  * SoapNodeSerializer is used to serialize Nodes into the soap format and
24  * vice versa. The format of serialized Nodes is defined by the wCMF soap server.
25  *
26  * @author ingo herwig <ingo@wemove.com>
27  */
29 
30  private $_serializedOIDs = array();
31  private $_persistenceFacade = null;
32 
33  /**
34  * Constructor
35  * @param $persistenceFacade
36  */
37  public function __construct(PersistenceFacade $persistenceFacade) {
38  $this->_persistenceFacade = $persistenceFacade;
39  }
40 
41  /**
42  * @see NodeSerializer::isSerializedNode
43  */
44  public function isSerializedNode($data) {
45  if (is_object($data)) {
46  $data = (array)$data;
47  }
48  $syntaxOk = (is_array($data) && (isset($data['oid'])));
49  // check for oid variables
50  if ($syntaxOk && isset($data['oid']) && preg_match('/^\{.+\}$/', $data['oid'])) {
51  $syntaxOk = false;
52  }
53  return $syntaxOk;
54  }
55 
56  /**
57  * @see NodeSerializer::deserializeNode
58  */
59  public function deserializeNode($data, Node $parent=null, $role=null) {
60  if (!isset($data['oid'])) {
61  throw new IllegalArgumentException("Serialized Node data must contain an 'oid' parameter");
62  }
63  $oid = ObjectId::parse($data['oid']);
64  if ($oid == null) {
65  throw new IllegalArgumentException("The object id '".$oid."' is invalid");
66  }
67 
68  // don't create all values by default (-> don't use PersistenceFacade::create() directly,
69  // just for determining the class)
70  $class = get_class($this->_persistenceFacade->create($oid->getType(), BuildDepth::SINGLE));
71  $node = new $class;
72 
73  $remainingData = array();
74 
75  $foundNodeAttribute = false;
76  $mapper = $node->getMapper();
77  foreach($data as $key => $value) {
78  if ($mapper->hasAttribute($key) || $mapper->hasRelation($key)) {
79  $this->deserializeValue($node, $key, $value);
80  $foundNodeAttribute = true;
81  }
82  else {
83  $remainingData[$key] = $value;
84  }
85  }
86 
87  if ($foundNodeAttribute) {
88  // a node was deserialized -> remove oid from remaining data
89  unset($remainingData['oid']);
90  }
91 
92  // set oid after attributes in order to
93  // avoid it being changed from missing pk values
94  $node->setOID($oid);
95 
96  // create hierarchy
97  if ($parent != null) {
98  $parent->addNode($node, $role);
99  }
100 
101  return array('node' => $node, 'data' => $remainingData);
102  }
103 
104  /**
105  * @see NodeSerializer::serializeNode
106  */
107  public function serializeNode($node) {
108  if (!($node instanceof Node)) {
109  return null;
110  }
111  $curResult = array();
112  $oidStr = $node->getOID()->__toString();
113  $curResult['oid'] = $oidStr;
114 
115  if (!in_array($oidStr, $this->_serializedOIDs)) {
116  $this->_serializedOIDs[] = $oidStr;
117 
118  // serialize attributes
119  // use NodeValueIterator to iterate over all Node values
120  $valueIter = new NodeValueIterator($node, false);
121  foreach($valueIter as $valueName => $value) {
122  $curResult[$valueName] = $value;
123  }
124 
125  // add related objects by creating an attribute that is named as the role of the object
126  // multivalued relations will be serialized into an array
127  $mapper = $node->getMapper();
128  foreach ($mapper->getRelations() as $relation) {
129  $role = $relation->getOtherRole();
130  $relatedNodes = $node->getValue($role);
131  if ($relatedNodes) {
132  // serialize the nodes
133  $curResult[$role] = array();
134  $isMultiValued = $relation->isMultiValued();
135  if ($isMultiValued) {
136  foreach ($relatedNodes as $relatedNode) {
137  // add serialized node
138  $curResult[$role][] = $this->serializeNode($relatedNode);
139  }
140  }
141  else {
142  // add serialized node
143  $relatedNode = $relatedNodes;
144  $curResult[$role][] = $this->serializeNode($relatedNode);
145  }
146  }
147  }
148  }
149  return $curResult;
150  }
151 }
152 ?>
IllegalArgumentException signals an exception in method arguments.
NodeValueIterator is used to iterate over all persistent values of a Node (not including relations)...
deserializeValue(Node $node, $key, $value)
Deserialize a node value.
deserializeNode($data, Node $parent=null, $role=null)
SoapNodeSerializer is used to serialize Nodes into the soap format and vice versa.
static parse($oid)
Parse a serialized object id string into an ObjectId instance.
Definition: ObjectId.php:144
NodeSerializerBase is a base class for NodeSerialize implementations.
PersistenceFacade defines the interface for PersistenceFacade implementations.
__construct(PersistenceFacade $persistenceFacade)
Constructor.
Node adds the concept of relations to PersistentObject.
Definition: Node.php:34