NodeSortkeyComparator.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;
12 
14 
15 /**
16  * NodeSortkeyComparator is used to compare nodes by their sortkey
17  * in relation to a given node.
18  *
19  * The following example shows the usage:
20  *
21  * @code
22  * // sort all child nodes of a node by their sortkey
23  * // regardless of the type
24  * $node = $persistenceFacade->load(ObjectId::parse($oidStr), 1);
25  * $children = $node->getChildren();
26  * $comparator = new NodeSortkeyComparator($node, $children);
27  * usort($children, [$comparator, 'compare']);
28  * @endcode
29  *
30  * @author ingo herwig <ingo@wemove.com>
31  */
33 
34  private $referenceNode;
35  private $oidRoleMap = [];
36 
37  /**
38  * Constructor
39  * @param $referenceNode Node instance to which the other nodes are related
40  * @param $nodeList Array of Node instances, which should be sorted in relation
41  * to the reference node
42  * @note If the role of a node to sort is not be defined, the comparator
43  * uses the first relation that has the type of the given node
44  */
45  public function __construct(Node $referenceNode, array $nodeList) {
46  $this->referenceNode = $referenceNode;
47  // map oids to roles for faster access
48  foreach ($nodeList as $curNode) {
49  $curRole = $referenceNode->getNodeRelation($curNode);
50  if ($curRole != null) {
51  $this->oidRoleMap[$curNode->getOID()->__toString()] = $curRole->getOtherRole();
52  }
53  }
54  }
55 
56  /**
57  * Compare function for sorting Nodes in the given relation
58  * @param $a First Node instance
59  * @param $b First Node instance
60  * @return -1, 0 or 1 whether a is less, equal or greater than b
61  * in respect of the criteria
62  */
63  public function compare(Node $a, Node $b) {
64  $valA = $this->getSortkeyValue($a);
65  $valB = $this->getSortkeyValue($b);
66  if ($valA == $valB) { return 0; }
67  return ($valA > $valB) ? 1 : -1;
68  }
69 
70  /**
71  * Get the sortkey value of a node in the given relation
72  * @param $node Node
73  * @return Number
74  */
75  protected function getSortkeyValue(Node $node) {
76  // if no role is defined for a or b, the sortkey is
77  // determined from the type of the reference node
78  $defaultRole = $this->referenceNode->getType();
79  $referenceMapper = $this->referenceNode->getMapper();
80  $mapper = $node->getMapper();
81 
82  if ($referenceMapper && $mapper) {
83 
84  $referenceRole = $defaultRole;
85  // get the sortkey of the node for the relation to the reference node,
86  // if a role is defined
87  $oidStr = $node->getOID()->__toString();
88  if (isset($this->oidRoleMap[$oidStr])) {
89  $nodeRole = $this->oidRoleMap[$oidStr];
90  $relationDesc = $referenceMapper->getRelation($nodeRole);
91  $referenceRole = $relationDesc->getThisRole();
92  }
93  $sortkeyDef = $mapper->getSortkey($referenceRole);
94  return $node->getValue($sortkeyDef['sortFieldName']);
95  }
96  return 0;
97  }
98 }
99 ?>
getSortkeyValue(Node $node)
Get the sortkey value of a node in the given relation.
getValue($name)
Definition: Node.php:97
getNodeRelation($object)
Get the relation description for a given node.
Definition: Node.php:720
__construct(Node $referenceNode, array $nodeList)
Constructor.
NodeSortkeyComparator is used to compare nodes by their sortkey in relation to a given node.
Node adds the concept of relations to PersistentObject.
Definition: Node.php:34
Node related interfaces and classes.
Definition: namespaces.php:26
compare(Node $a, Node $b)
Compare function for sorting Nodes in the given relation.