/vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Document/MongoDB/Repository/MaterializedPathRepository.php

https://bitbucket.org/iiic/iszp · PHP · 210 lines · 113 code · 31 blank · 66 comment · 11 complexity · 52e32a9beeba2cfec246084609a80752 MD5 · raw file

  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 childCount($node = null, $direct = false)
  79. {
  80. $meta = $this->getClassMetadata();
  81. if (is_object($node)) {
  82. if (!($node instanceof $meta->name)) {
  83. throw new InvalidArgumentException("Node is not related to this repository");
  84. }
  85. $wrapped = new MongoDocumentWrapper($node, $this->dm);
  86. if (!$wrapped->hasValidIdentifier()) {
  87. throw new InvalidArgumentException("Node is not managed by UnitOfWork");
  88. }
  89. }
  90. $qb = $this->getChildrenQueryBuilder($node, $direct);
  91. $qb->count();
  92. return (int) $qb->getQuery()->execute();
  93. }
  94. /**
  95. * {@inheritDoc}
  96. */
  97. public function getChildrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'asc', $includeNode = false)
  98. {
  99. $meta = $this->getClassMetadata();
  100. $config = $this->listener->getConfiguration($this->dm, $meta->name);
  101. $separator = preg_quote($config['path_separator']);
  102. $qb = $this->dm->createQueryBuilder()
  103. ->find($meta->name);
  104. $regex = false;
  105. if (is_object($node) && $node instanceof $meta->name) {
  106. $node = new MongoDocumentWrapper($node, $this->dm);
  107. $nodePath = preg_quote($node->getPropertyValue($config['path']));
  108. if ($direct) {
  109. $regex = sprintf('/^%s([^%s]+%s)'.($includeNode ? '?' : '').'$/',
  110. $nodePath,
  111. $separator,
  112. $separator);
  113. } else {
  114. $regex = sprintf('/^%s(.+)'.($includeNode ? '?' : '').'/',
  115. $nodePath);
  116. }
  117. } else if ($direct) {
  118. $regex = sprintf('/^([^%s]+)'.($includeNode ? '?' : '').'%s$/',
  119. $separator,
  120. $separator);
  121. }
  122. if ($regex) {
  123. $qb->field($config['path'])->equals(new \MongoRegex($regex));
  124. }
  125. $qb->sort(is_null($sortByField) ? $config['path'] : $sortByField, $direction === 'asc' ? 'asc' : 'desc');
  126. return $qb;
  127. }
  128. /**
  129. * G{@inheritDoc}
  130. */
  131. public function getChildrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'asc', $includeNode = false)
  132. {
  133. return $this->getChildrenQueryBuilder($node, $direct, $sortByField, $direction, $includeNode)->getQuery();
  134. }
  135. /**
  136. * {@inheritDoc}
  137. */
  138. public function getChildren($node = null, $direct = false, $sortByField = null, $direction = 'asc', $includeNode = false)
  139. {
  140. return $this->getChildrenQuery($node, $direct, $sortByField, $direction, $includeNode)->execute();
  141. }
  142. /**
  143. * {@inheritDoc}
  144. */
  145. public function getNodesHierarchyQueryBuilder($node = null, $direct = false, array $options = array(), $includeNode = false)
  146. {
  147. $sortBy = array(
  148. 'field' => null,
  149. 'dir' => 'asc'
  150. );
  151. if (isset($options['childSort'])) {
  152. $sortBy = array_merge($sortBy, $options['childSort']);
  153. }
  154. return $this->getChildrenQueryBuilder($node, $direct, $sortBy['field'], $sortBy['dir'], $includeNode);
  155. }
  156. /**
  157. * {@inheritDoc}
  158. */
  159. public function getNodesHierarchyQuery($node = null, $direct = false, array $options = array(), $includeNode = false)
  160. {
  161. return $this->getNodesHierarchyQueryBuilder($node, $direct, $options, $includeNode)->getQuery();
  162. }
  163. /**
  164. * {@inheritDoc}
  165. */
  166. public function getNodesHierarchy($node = null, $direct = false, array $options = array(), $includeNode = false)
  167. {
  168. $query = $this->getNodesHierarchyQuery($node, $direct, $options, $includeNode);
  169. $query->setHydrate(false);
  170. return $query->toArray();
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. protected function validate()
  176. {
  177. return $this->listener->getStrategy($this->dm, $this->getClassMetadata()->name)->getName() === Strategy::MATERIALIZED_PATH;
  178. }
  179. }