AbstractTreeRepository.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace Gedmo\Tree\Document\MongoDB\Repository;
  3. use Doctrine\ODM\MongoDB\DocumentRepository,
  4. Doctrine\ODM\MongoDB\DocumentManager,
  5. Doctrine\ODM\MongoDB\Mapping\ClassMetadata,
  6. Doctrine\ODM\MongoDB\UnitOfWork,
  7. Gedmo\Tree\RepositoryUtils,
  8. Gedmo\Tree\RepositoryUtilsInterface,
  9. Gedmo\Tree\RepositoryInterface;
  10. abstract class AbstractTreeRepository extends DocumentRepository implements RepositoryInterface
  11. {
  12. /**
  13. * Tree listener on event manager
  14. *
  15. * @var AbstractTreeListener
  16. */
  17. protected $listener = null;
  18. /**
  19. * Repository utils
  20. */
  21. protected $repoUtils = null;
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function __construct(DocumentManager $em, UnitOfWork $uow, ClassMetadata $class)
  26. {
  27. parent::__construct($em, $uow, $class);
  28. $treeListener = null;
  29. foreach ($em->getEventManager()->getListeners() as $listeners) {
  30. foreach ($listeners as $listener) {
  31. if ($listener instanceof \Gedmo\Tree\TreeListener) {
  32. $treeListener = $listener;
  33. break;
  34. }
  35. }
  36. if ($treeListener) {
  37. break;
  38. }
  39. }
  40. if (is_null($treeListener)) {
  41. throw new \Gedmo\Exception\InvalidMappingException('This repository can be attached only to ODM MongoDB tree listener');
  42. }
  43. $this->listener = $treeListener;
  44. if (!$this->validate()) {
  45. throw new \Gedmo\Exception\InvalidMappingException('This repository cannot be used for tree type: ' . $treeListener->getStrategy($em, $class->name)->getName());
  46. }
  47. $this->repoUtils = new RepositoryUtils($this->dm, $this->getClassMetadata(), $this->listener, $this);
  48. }
  49. /**
  50. * Sets the RepositoryUtilsInterface instance
  51. *
  52. * @param \Gedmo\Tree\RepositoryUtilsInterface $repoUtils
  53. *
  54. * @return $this
  55. */
  56. public function setRepoUtils(RepositoryUtilsInterface $repoUtils)
  57. {
  58. $this->repoUtils = $repoUtils;
  59. return $this;
  60. }
  61. /**
  62. * Returns the RepositoryUtilsInterface instance
  63. *
  64. * @return \Gedmo\Tree\RepositoryUtilsInterface|null
  65. */
  66. public function getRepoUtils()
  67. {
  68. return $this->repoUtils;
  69. }
  70. /**
  71. * {@inheritDoc}
  72. */
  73. public function childrenHierarchy($node = null, $direct = false, array $options = array(), $includeNode = false)
  74. {
  75. return $this->repoUtils->childrenHierarchy($node, $direct, $options);
  76. }
  77. /**
  78. * {@inheritDoc}
  79. */
  80. public function buildTree(array $nodes, array $options = array())
  81. {
  82. return $this->repoUtils->buildTree($nodes, $options);
  83. }
  84. /**
  85. * {@inheritDoc}
  86. */
  87. public function buildTreeArray(array $nodes)
  88. {
  89. return $this->repoUtils->buildTreeArray($nodes);
  90. }
  91. /**
  92. * Checks if current repository is right
  93. * for currently used tree strategy
  94. *
  95. * @return bool
  96. */
  97. abstract protected function validate();
  98. /**
  99. * Get all root nodes query builder
  100. *
  101. * @param string - Sort by field
  102. * @param string - Sort direction ("asc" or "desc")
  103. *
  104. * @return \Doctrine\MongoDB\Query\Builder - QueryBuilder object
  105. */
  106. abstract public function getRootNodesQueryBuilder($sortByField = null, $direction = 'asc');
  107. /**
  108. * Get all root nodes query
  109. *
  110. * @param string - Sort by field
  111. * @param string - Sort direction ("asc" or "desc")
  112. *
  113. * @return \Doctrine\MongoDB\Query\Query - Query object
  114. */
  115. abstract public function getRootNodesQuery($sortByField = null, $direction = 'asc');
  116. /**
  117. * Returns a QueryBuilder configured to return an array of nodes suitable for buildTree method
  118. *
  119. * @param object $node - Root node
  120. * @param bool $direct - Obtain direct children?
  121. * @param array $config - Metadata configuration
  122. * @param array $options - Options
  123. * @param boolean $includeNode - Include node in results?
  124. *
  125. * @return \Doctrine\MongoDB\Query\Builder - QueryBuilder object
  126. */
  127. abstract public function getNodesHierarchyQueryBuilder($node = null, $direct, array $config, array $options = array(), $includeNode = false);
  128. /**
  129. * Returns a Query configured to return an array of nodes suitable for buildTree method
  130. *
  131. * @param object $node - Root node
  132. * @param bool $direct - Obtain direct children?
  133. * @param array $config - Metadata configuration
  134. * @param array $options - Options
  135. * @param boolean $includeNode - Include node in results?
  136. *
  137. * @return \Doctrine\MongoDB\Query\Query - Query object
  138. */
  139. abstract public function getNodesHierarchyQuery($node = null, $direct, array $config, array $options = array(), $includeNode = false);
  140. /**
  141. * Get list of children followed by given $node. This returns a QueryBuilder object
  142. *
  143. * @param object $node - if null, all tree nodes will be taken
  144. * @param boolean $direct - true to take only direct children
  145. * @param string $sortByField - field name to sort by
  146. * @param string $direction - sort direction : "ASC" or "DESC"
  147. * @param bool $includeNode - Include the root node in results?
  148. *
  149. * @return \Doctrine\MongoDB\Query\Builder - QueryBuilder object
  150. */
  151. abstract public function getChildrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false);
  152. /**
  153. * Get list of children followed by given $node. This returns a Query
  154. *
  155. * @param object $node - if null, all tree nodes will be taken
  156. * @param boolean $direct - true to take only direct children
  157. * @param string $sortByField - field name to sort by
  158. * @param string $direction - sort direction : "ASC" or "DESC"
  159. * @param bool $includeNode - Include the root node in results?
  160. *
  161. * @return \Doctrine\MongoDB\Query\Query - Query object
  162. */
  163. abstract public function getChildrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false);
  164. }