AbstractNodeSerializer.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 
17 
18 /**
19  * NodeSerializerBase is a base class for NodeSerialize implementations.
20  *
21  * @author ingo herwig <ingo@wemove.com>
22  */
23 abstract class AbstractNodeSerializer implements NodeSerializer {
24 
25  private $persistenceFacade = null;
26 
27  /**
28  * Constructor
29  * @param $persistenceFacade
30  */
31  public function __construct(PersistenceFacade $persistenceFacade) {
32  $this->persistenceFacade = $persistenceFacade;
33  }
34 
35  /**
36  * Get a Node instance based on the original values to merge the deserialized values into
37  * @param $oid The object id of the Node
38  * @return Node
39  */
40  protected function getNodeTemplate($oid) {
41  // load object if existing to get the original data
42  $originalNode = $this->persistenceFacade->load($oid, BuildDepth::SINGLE);
43 
44  // create the request node and copy the orignal data
45  $class = get_class($this->persistenceFacade->create($oid->getType(), BuildDepth::SINGLE));
46  $node = new $class;
47  if ($originalNode) {
48  $originalNode->copyValues($node);
49  }
50  return $node;
51  }
52 
53  /**
54  * Deserialize a node value
55  * @param $node Node instance
56  * @param $key The value name or type if value is an array
57  * @param $value The value or child data, if value is an array
58  */
59  protected function deserializeValue(Node $node, $key, $value) {
60  if (!is_array($value)) {
61  // force set value to avoid exceptions in this stage
62  $node->setValue($key, $value, true);
63  }
64  else {
65  $role = $key;
66  if ($this->isMultiValued($node, $role)) {
67  // deserialize children
68  foreach($value as $childData) {
69  $this->deserializeNode($childData, $node, $role);
70  }
71  }
72  else {
73  $this->deserializeNode($value, $node, $role);
74  }
75  }
76  }
77 
78  /**
79  * Check if a relation is multi valued
80  * @param $node The Node that has the relation
81  * @param $role The role of the relation
82  */
83  protected function isMultiValued(Node $node, $role) {
84  $isMultiValued = false;
85  $mapper = $node->getMapper();
86  if ($mapper->hasRelation($role)) {
87  $relation = $mapper->getRelation($role);
88  $isMultiValued = $relation->isMultiValued();
89  }
90  return $isMultiValued;
91  }
92 }
93 ?>
NodeSerializer implementations are used to serialize Nodes into an array representation or deserializ...
deserializeValue(Node $node, $key, $value)
Deserialize a node value.
isMultiValued(Node $node, $role)
Check if a relation is multi valued.
BuildDepth values are used to define the depth when loading object trees.
Definition: BuildDepth.php:19
PersistenceFacade defines the interface for PersistenceFacade implementations.
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)
Deserialize a Node from serialized data.
setValue($name, $value, $forceSet=false, $trackChange=true)
Definition: Node.php:125
__construct(PersistenceFacade $persistenceFacade)
Constructor.