MaterializedPathRepository.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Gedmo\Tree\Document\MongoDB\Repository;
  3. use Gedmo\Exception\InvalidArgumentException,
  4. Gedmo\Tree\Strategy,
  5. Gedmo\Tree\Strategy\ODM\MongoDB\MaterializedPath,
  6. Gedmo\Tool\Wrapper\MongoDocumentWrapper;
  7. /**
  8. * The MaterializedPathRepository has some useful functions
  9. * to interact with MaterializedPath tree. Repository uses
  10. * the strategy used by listener
  11. *
  12. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @package Gedmo.Tree.Document.MongoDB.Repository
  15. * @subpackage MaterializedPathRepository
  16. * @link http://www.gediminasm.org
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. class MaterializedPathRepository extends AbstractTreeRepository
  20. {
  21. /**
  22. * Get tree query builder
  23. *
  24. * @param object Root node
  25. *
  26. * @return \Doctrine\ODM\MongoDB\Query\Builder
  27. */
  28. public function getTreeQueryBuilder($rootNode = null)
  29. {
  30. return $this->getChildrenQueryBuilder($rootNode, false, null, 'asc', true);
  31. }
  32. /**
  33. * Get tree query
  34. *
  35. * @param object Root node
  36. *
  37. * @return \Doctrine\ODM\MongoDB\Query\Query
  38. */
  39. public function getTreeQuery($rootNode = null)
  40. {
  41. return $this->getTreeQueryBuilder($rootNode)->getQuery();
  42. }
  43. /**
  44. * Get tree
  45. *
  46. * @param object Root node
  47. *
  48. * @return \Doctrine\ODM\MongoDB\Cursor
  49. */
  50. public function getTree($rootNode = null)
  51. {
  52. return $this->getTreeQuery($rootNode)->execute();
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function getRootNodesQueryBuilder($sortByField = null, $direction = 'asc')
  58. {
  59. return $this->getChildrenQueryBuilder(null, true, $sortByField, $direction);
  60. }
  61. /**
  62. * {@inheritDoc}
  63. */
  64. public function getRootNodesQuery($sortByField = null, $direction = 'asc')
  65. {
  66. return $this->getRootNodesQueryBuilder($sortByField, $direction)->getQuery();
  67. }
  68. /**
  69. * {@inheritDoc}
  70. */
  71. public function getRootNodes($sortByField = null, $direction = 'asc')
  72. {
  73. return $this->getRootNodesQuery($sortByField, $direction)->execute();
  74. }
  75. /**
  76. * {@inheritDoc}
  77. */
  78. public function getChildrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'asc', $includeNode = false)
  79. {
  80. $meta = $this->getClassMetadata();
  81. $config = $this->listener->getConfiguration($this->dm, $meta->name);
  82. $separator = preg_quote($config['path_separator']);
  83. $qb = $this->dm->createQueryBuilder()
  84. ->find($meta->name);
  85. $regex = false;
  86. if (is_object($node) && $node instanceof $meta->name) {
  87. $node = new MongoDocumentWrapper($node, $this->dm);
  88. $nodePath = preg_quote($node->getPropertyValue($config['path']));
  89. if ($direct) {
  90. $regex = sprintf('/^%s([^%s]+%s)'.($includeNode ? '?' : '').'$/',
  91. $nodePath,
  92. $separator,
  93. $separator);
  94. } else {
  95. $regex = sprintf('/^%s(.+)'.($includeNode ? '?' : '').'/',
  96. $nodePath);
  97. }
  98. } else if ($direct) {
  99. $regex = sprintf('/^([^%s]+)'.($includeNode ? '?' : '').'%s$/',
  100. $separator,
  101. $separator);
  102. }
  103. if ($regex) {
  104. $qb->field($config['path'])->equals(new \MongoRegex($regex));
  105. }
  106. $qb->sort(is_null($sortByField) ? $config['path'] : $sortByField, $direction === 'asc' ? 'asc' : 'desc');
  107. return $qb;
  108. }
  109. /**
  110. * G{@inheritDoc}
  111. */
  112. public function getChildrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'asc', $includeNode = false)
  113. {
  114. return $this->getChildrenQueryBuilder($node, $direct, $sortByField, $direction, $includeNode)->getQuery();
  115. }
  116. /**
  117. * {@inheritDoc}
  118. */
  119. public function getChildren($node = null, $direct = false, $sortByField = null, $direction = 'asc', $includeNode = false)
  120. {
  121. return $this->getChildrenQuery($node, $direct, $sortByField, $direction, $includeNode)->execute();
  122. }
  123. /**
  124. * {@inheritDoc}
  125. */
  126. public function getNodesHierarchyQueryBuilder($node = null, $direct, array $config, array $options = array(), $includeNode = false)
  127. {
  128. $sortBy = array(
  129. 'field' => null,
  130. 'dir' => 'asc'
  131. );
  132. if (isset($options['childSort'])) {
  133. $sortBy = array_merge($sortBy, $options['childSort']);
  134. }
  135. return $this->getChildrenQueryBuilder($node, $direct, $sortBy['field'], $sortBy['dir'], $includeNode);
  136. }
  137. /**
  138. * {@inheritDoc}
  139. */
  140. public function getNodesHierarchyQuery($node = null, $direct, array $config, array $options = array(), $includeNode = false)
  141. {
  142. return $this->getNodesHierarchyQueryBuilder($node, $direct, $config, $options, $includeNode)->getQuery();
  143. }
  144. /**
  145. * {@inheritDoc}
  146. */
  147. public function getNodesHierarchy($node = null, $direct, array $config, array $options = array(), $includeNode = false)
  148. {
  149. $query = $this->getNodesHierarchyQuery($node, $direct, $config, $options, $includeNode);
  150. $query->setHydrate(false);
  151. return $query->toArray();
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. protected function validate()
  157. {
  158. return $this->listener->getStrategy($this->dm, $this->getClassMetadata()->name)->getName() === Strategy::MATERIALIZED_PATH;
  159. }
  160. }