Guest User

Untitled

a guest
May 26th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. <?php
  2.  
  3. namespace DoctrineExtensions\Hierarchical;
  4.  
  5. use Doctrine\ORM\EntityManager,
  6. Doctrine\ORM\Mapping\ClassMetadata,
  7. Doctrine\ORM\PersistentCollection;
  8.  
  9.  
  10. abstract class AbstractManager
  11. {
  12. /**
  13. * @var Doctrine\ORM\EntityManager
  14. */
  15. protected $em;
  16.  
  17. /**
  18. * @var Doctrine\ORM\Mapping\ClassMetadata
  19. */
  20. protected $classMeta;
  21.  
  22. /**
  23. * __construct
  24. *
  25. * @param Doctrine\ORM\EntityManager $em
  26. * @param Doctrine\ORM\Mapping\ClassMetadata $meta
  27. * @return void
  28. */
  29. public function __construct(EntityManager $em, ClassMetadata $meta)
  30. {
  31. $this->em = $em;
  32. $this->classMeta = $em->getClassMetadata(get_class($entity));
  33. }
  34.  
  35. /**
  36. * EntityManager accessor
  37. *
  38. * @return Doctrine\ORM\EntityManager
  39. */
  40. public function getEntityManager()
  41. {
  42. return $this->em;
  43. }
  44.  
  45. /**
  46. * ClassMetadata accessor
  47. *
  48. * @return Doctrine\ORM\Mapping\ClassMetadata
  49. */
  50. public function getClassMeta()
  51. {
  52. return $this->classMeta;
  53. }
  54.  
  55. /**
  56. * Decorates a collection of entities as Nodes
  57. *
  58. * @param Traversable|array $input
  59. * @return Traversable|array
  60. */
  61. public function getNodes($input)
  62. {
  63. if ($input instanceof PersistentCollection) {
  64. // Return instance of ArrayCollection instead of PersistentCollection
  65. $hm = $this;
  66. return $input->unwrap()->map(
  67. function ($node) use ($hm) {
  68. return $hm->getNode($node);
  69. }
  70. );
  71. } elseif (is_array($input) || $input instanceof Traversable) {
  72. foreach ($input as $key => $entity) {
  73. $input[$key] = $this->getNode($entity);
  74. }
  75. return $input;
  76. }
  77.  
  78. throw new \InvalidArgumentException(
  79. 'Input to getNodes should be a PersistentCollection or a ' .
  80. 'Traversable/array, ' . gettype($input) . ' provided.'
  81. );
  82. }
  83.  
  84. /**
  85. * Decorates the entity with the appropriate Node decorator
  86. *
  87. * @param mixed $entity
  88. * @return DoctrineExtensions\Hierarchical\Node
  89. */
  90. abstract public function getNode($entity);
  91.  
  92. /**
  93. * Adds the entity as a root node
  94. *
  95. * Decorates via getNode() as needed
  96. *
  97. * @param mixed $entity
  98. * @return DoctrineExtensions\Hierarchical\Node
  99. */
  100. abstract public function addRoot($entity);
  101. }
Add Comment
Please, Sign In to add comment