RepositoryUtils.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\Persistence\Mapping\ClassMetadata;
  4. use Doctrine\Common\Persistence\ObjectManager;
  5. use Gedmo\Exception\InvalidArgumentException;
  6. class RepositoryUtils implements RepositoryUtilsInterface
  7. {
  8. /** @var \Doctrine\Common\Persistence\Mapping\ClassMetadata */
  9. protected $meta;
  10. /** @var \Gedmo\Tree\TreeListener */
  11. protected $listener;
  12. /** @var \Doctrine\Common\Persistence\ObjectManager */
  13. protected $om;
  14. /** @var \Gedmo\Tree\RepositoryInterface */
  15. protected $repo;
  16. public function __construct(ObjectManager $om, ClassMetadata $meta, $listener, $repo)
  17. {
  18. $this->om = $om;
  19. $this->meta = $meta;
  20. $this->listener = $listener;
  21. $this->repo = $repo;
  22. }
  23. public function getClassMetadata()
  24. {
  25. return $this->meta;
  26. }
  27. /**
  28. * {@inheritDoc}
  29. */
  30. public function childrenHierarchy($node = null, $direct = false, array $options = array(), $includeNode = false)
  31. {
  32. $meta = $this->getClassMetadata();
  33. if ($node !== null) {
  34. if ($node instanceof $meta->name) {
  35. $wrapperClass = $this->om instanceof \Doctrine\ORM\EntityManager ?
  36. '\Gedmo\Tool\Wrapper\EntityWrapper' :
  37. '\Gedmo\Tool\Wrapper\MongoDocumentWrapper';
  38. $wrapped = new $wrapperClass($node, $this->om);
  39. if (!$wrapped->hasValidIdentifier()) {
  40. throw new InvalidArgumentException("Node is not managed by UnitOfWork");
  41. }
  42. }
  43. } else {
  44. $includeNode = true;
  45. }
  46. // Gets the array of $node results. It must be ordered by depth
  47. $nodes = $this->repo->getNodesHierarchy($node, $direct, $options, $includeNode);
  48. return $this->buildTree($nodes, $options);
  49. }
  50. /**
  51. * {@inheritDoc}
  52. */
  53. public function buildTree(array $nodes, array $options = array())
  54. {
  55. $meta = $this->getClassMetadata();
  56. $nestedTree = $this->repo->buildTreeArray($nodes);
  57. $default = array(
  58. 'decorate' => false,
  59. 'rootOpen' => '<ul>',
  60. 'rootClose' => '</ul>',
  61. 'childOpen' => '<li>',
  62. 'childClose' => '</li>',
  63. 'nodeDecorator' => function ($node) use ($meta) {
  64. // override and change it, guessing which field to use
  65. if ($meta->hasField('title')) {
  66. $field = 'title';
  67. } elseif ($meta->hasField('name')) {
  68. $field = 'name';
  69. } else {
  70. throw new InvalidArgumentException("Cannot find any representation field");
  71. }
  72. return $node[$field];
  73. }
  74. );
  75. $options = array_merge($default, $options);
  76. // If you don't want any html output it will return the nested array
  77. if (!$options['decorate']) {
  78. return $nestedTree;
  79. }
  80. if (!count($nestedTree)) {
  81. return '';
  82. }
  83. $build = function($tree) use (&$build, &$options) {
  84. $output = is_string($options['rootOpen']) ? $options['rootOpen'] : $options['rootOpen']($tree);
  85. foreach ($tree as $node) {
  86. $output .= is_string($options['childOpen']) ? $options['childOpen'] : $options['childOpen']($node);
  87. $output .= $options['nodeDecorator']($node);
  88. if (count($node['__children']) > 0) {
  89. $output .= $build($node['__children']);
  90. }
  91. $output .= $options['childClose'];
  92. }
  93. return $output . $options['rootClose'];
  94. };
  95. return $build($nestedTree);
  96. }
  97. /**
  98. * {@inheritDoc}
  99. */
  100. public function buildTreeArray(array $nodes)
  101. {
  102. $meta = $this->getClassMetadata();
  103. $config = $this->listener->getConfiguration($this->om, $meta->name);
  104. $nestedTree = array();
  105. $l = 0;
  106. if (count($nodes) > 0) {
  107. // Node Stack. Used to help building the hierarchy
  108. $stack = array();
  109. foreach ($nodes as $child) {
  110. $item = $child;
  111. $item['__children'] = array();
  112. // Number of stack items
  113. $l = count($stack);
  114. // Check if we're dealing with different levels
  115. while($l > 0 && $stack[$l - 1][$config['level']] >= $item[$config['level']]) {
  116. array_pop($stack);
  117. $l--;
  118. }
  119. // Stack is empty (we are inspecting the root)
  120. if ($l == 0) {
  121. // Assigning the root child
  122. $i = count($nestedTree);
  123. $nestedTree[$i] = $item;
  124. $stack[] = &$nestedTree[$i];
  125. } else {
  126. // Add child to parent
  127. $i = count($stack[$l - 1]['__children']);
  128. $stack[$l - 1]['__children'][$i] = $item;
  129. $stack[] = &$stack[$l - 1]['__children'][$i];
  130. }
  131. }
  132. }
  133. return $nestedTree;
  134. }
  135. }