RepositoryUtils.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\Persistence\Mapping\ClassMetadata;
  4. use Doctrine\Common\Persistence\ObjectManager;
  5. class RepositoryUtils implements RepositoryUtilsInterface
  6. {
  7. /** @var \Doctrine\Common\Persistence\Mapping\ClassMetadata */
  8. protected $meta;
  9. /** @var \Gedmo\Tree\TreeListener */
  10. protected $listener;
  11. /** @var \Doctrine\Common\Persistence\ObjectManager */
  12. protected $om;
  13. /** @var \Gedmo\Tree\RepositoryInterface */
  14. protected $repo;
  15. public function __construct(ObjectManager $om, ClassMetadata $meta, $listener, $repo)
  16. {
  17. $this->om = $om;
  18. $this->meta = $meta;
  19. $this->listener = $listener;
  20. $this->repo = $repo;
  21. }
  22. public function getClassMetadata()
  23. {
  24. return $this->meta;
  25. }
  26. /**
  27. * {@inheritDoc}
  28. */
  29. public function childrenHierarchy($node = null, $direct = false, array $options = array(), $includeNode = false)
  30. {
  31. $meta = $this->getClassMetadata();
  32. $config = $this->listener->getConfiguration($this->om, $meta->name);
  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, $config, $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. } elseif (!count($nestedTree)) {
  80. return '';
  81. }
  82. $build = function($tree) use (&$build, &$options) {
  83. $output = is_string($options['rootOpen']) ? $options['rootOpen'] : $options['rootOpen']($tree);
  84. foreach ($tree as $node) {
  85. $output .= is_string($options['childOpen']) ? $options['childOpen'] : $options['childOpen']($node);
  86. $output .= $options['nodeDecorator']($node);
  87. if (count($node['__children']) > 0) {
  88. $output .= $build($node['__children']);
  89. }
  90. $output .= $options['childClose'];
  91. }
  92. return $output . $options['rootClose'];
  93. };
  94. return $build($nestedTree);
  95. }
  96. /**
  97. * {@inheritDoc}
  98. */
  99. public function buildTreeArray(array $nodes)
  100. {
  101. $meta = $this->getClassMetadata();
  102. $config = $this->listener->getConfiguration($this->om, $meta->name);
  103. $nestedTree = array();
  104. $l = 0;
  105. if (count($nodes) > 0) {
  106. // Node Stack. Used to help building the hierarchy
  107. $stack = array();
  108. foreach ($nodes as $child) {
  109. $item = $child;
  110. $item['__children'] = array();
  111. // Number of stack items
  112. $l = count($stack);
  113. // Check if we're dealing with different levels
  114. while($l > 0 && $stack[$l - 1][$config['level']] >= $item[$config['level']]) {
  115. array_pop($stack);
  116. $l--;
  117. }
  118. // Stack is empty (we are inspecting the root)
  119. if ($l == 0) {
  120. // Assigning the root child
  121. $i = count($nestedTree);
  122. $nestedTree[$i] = $item;
  123. $stack[] = &$nestedTree[$i];
  124. } else {
  125. // Add child to parent
  126. $i = count($stack[$l - 1]['__children']);
  127. $stack[$l - 1]['__children'][$i] = $item;
  128. $stack[] = &$stack[$l - 1]['__children'][$i];
  129. }
  130. }
  131. }
  132. return $nestedTree;
  133. }
  134. }