DoctrinePHPCRAdapter.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\CoreBundle\Model\Adapter;
  11. use Doctrine\Common\Persistence\ManagerRegistry;
  12. use Doctrine\ODM\PHPCR\DocumentManager;
  13. /**
  14. * This is a port of the DoctrineORMAdminBundle / ModelManager class.
  15. */
  16. class DoctrinePHPCRAdapter implements AdapterInterface
  17. {
  18. /**
  19. * @var ManagerRegistry
  20. */
  21. protected $registry;
  22. /**
  23. * @param ManagerRegistry $registry
  24. */
  25. public function __construct(ManagerRegistry $registry)
  26. {
  27. $this->registry = $registry;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function getNormalizedIdentifier($document)
  33. {
  34. if (is_scalar($document)) {
  35. throw new \RunTimeException('Invalid argument, object or null required');
  36. }
  37. if (!$document) {
  38. return;
  39. }
  40. $manager = $this->registry->getManagerForClass($document);
  41. if (!$manager instanceof DocumentManager) {
  42. return;
  43. }
  44. if (!$manager->contains($document)) {
  45. return;
  46. }
  47. $class = $manager->getClassMetadata(get_class($document));
  48. return $class->getIdentifierValue($document);
  49. }
  50. /**
  51. * Currently only the leading slash is removed.
  52. *
  53. * TODO: do we also have to encode certain characters like spaces or does that happen automatically?
  54. *
  55. * {@inheritdoc}
  56. */
  57. public function getUrlsafeIdentifier($document)
  58. {
  59. $id = $this->getNormalizedIdentifier($document);
  60. if (null !== $id) {
  61. return substr($id, 1);
  62. }
  63. return;
  64. }
  65. }