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