RelativeSlugHandler.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Gedmo\Sluggable\Handler;
  3. use Doctrine\Common\Persistence\ObjectManager;
  4. use Gedmo\Sluggable\SluggableListener;
  5. use Gedmo\Sluggable\Mapping\Event\SluggableAdapter;
  6. use Gedmo\Tool\Wrapper\AbstractWrapper;
  7. use Gedmo\Exception\InvalidMappingException;
  8. use Doctrine\Common\Persistence\Mapping\ClassMetadata;
  9. /**
  10. * Sluggable handler which should be used in order to prefix
  11. * a slug of related object. For instance user may belong to a company
  12. * in this case user slug could look like 'company-name/user-firstname'
  13. * where path separator separates the relative slug
  14. *
  15. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. class RelativeSlugHandler implements SlugHandlerInterface
  19. {
  20. const SEPARATOR = '/';
  21. /**
  22. * @var ObjectManager
  23. */
  24. protected $om;
  25. /**
  26. * @var SluggableListener
  27. */
  28. protected $sluggable;
  29. /**
  30. * Used options
  31. *
  32. * @var array
  33. */
  34. private $usedOptions;
  35. /**
  36. * Callable of original transliterator
  37. * which is used by sluggable
  38. *
  39. * @var callable
  40. */
  41. private $originalTransliterator;
  42. /**
  43. * $options = array(
  44. * 'separator' => '/',
  45. * 'relationField' => 'something',
  46. * 'relationSlugField' => 'slug'
  47. * )
  48. * {@inheritDoc}
  49. */
  50. public function __construct(SluggableListener $sluggable)
  51. {
  52. $this->sluggable = $sluggable;
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function onChangeDecision(SluggableAdapter $ea, $config, $object, &$slug, &$needToChangeSlug)
  58. {
  59. $this->om = $ea->getObjectManager();
  60. $isInsert = $this->om->getUnitOfWork()->isScheduledForInsert($object);
  61. $this->usedOptions = $config['handlers'][get_called_class()];
  62. if (!isset($this->usedOptions['separator'])) {
  63. $this->usedOptions['separator'] = self::SEPARATOR;
  64. }
  65. if (!$isInsert && !$needToChangeSlug) {
  66. $changeSet = $ea->getObjectChangeSet($this->om->getUnitOfWork(), $object);
  67. if (isset($changeSet[$this->usedOptions['relationField']])) {
  68. $needToChangeSlug = true;
  69. }
  70. }
  71. }
  72. /**
  73. * {@inheritDoc}
  74. */
  75. public function postSlugBuild(SluggableAdapter $ea, array &$config, $object, &$slug)
  76. {
  77. $this->originalTransliterator = $this->sluggable->getTransliterator();
  78. $this->sluggable->setTransliterator(array($this, 'transliterate'));
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. public static function validate(array $options, ClassMetadata $meta)
  84. {
  85. if (!$meta->isSingleValuedAssociation($options['relationField'])) {
  86. throw new InvalidMappingException("Unable to find slug relation through field - [{$options['relationField']}] in class - {$meta->name}");
  87. }
  88. }
  89. /**
  90. * {@inheritDoc}
  91. */
  92. public function onSlugCompletion(SluggableAdapter $ea, array &$config, $object, &$slug)
  93. {}
  94. /**
  95. * Transliterates the slug and prefixes the slug
  96. * by relative one
  97. *
  98. * @param string $text
  99. * @param string $separator
  100. * @param object $object
  101. * @return string
  102. */
  103. public function transliterate($text, $separator, $object)
  104. {
  105. $result = call_user_func_array(
  106. $this->originalTransliterator,
  107. array($text, $separator, $object)
  108. );
  109. $wrapped = AbstractWrapper::wrap($object, $this->om);
  110. $relation = $wrapped->getPropertyValue($this->usedOptions['relationField']);
  111. if ($relation) {
  112. $wrappedRelation = AbstractWrapper::wrap($relation, $this->om);
  113. $slug = $wrappedRelation->getPropertyValue($this->usedOptions['relationSlugField']);
  114. $result = $slug . $this->usedOptions['separator'] . $result;
  115. }
  116. $this->sluggable->setTransliterator($this->originalTransliterator);
  117. return $result;
  118. }
  119. /**
  120. * {@inheritDoc}
  121. */
  122. public function handlesUrlization()
  123. {
  124. return true;
  125. }
  126. }