123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
-
- namespace Gedmo\Sluggable\Handler;
-
- use Doctrine\Common\Persistence\ObjectManager;
- use Gedmo\Sluggable\SluggableListener;
- use Gedmo\Sluggable\Mapping\Event\SluggableAdapter;
- use Gedmo\Tool\Wrapper\AbstractWrapper;
- use Gedmo\Exception\InvalidMappingException;
- use Doctrine\Common\Persistence\Mapping\ClassMetadata;
-
-
- class RelativeSlugHandler implements SlugHandlerInterface
- {
- const SEPARATOR = '/';
-
-
-
- protected $om;
-
-
-
- protected $sluggable;
-
-
-
- private $usedOptions;
-
-
-
- private $originalTransliterator;
-
-
-
- public function __construct(SluggableListener $sluggable)
- {
- $this->sluggable = $sluggable;
- }
-
-
-
- public function onChangeDecision(SluggableAdapter $ea, $config, $object, &$slug, &$needToChangeSlug)
- {
- $this->om = $ea->getObjectManager();
- $isInsert = $this->om->getUnitOfWork()->isScheduledForInsert($object);
- $this->usedOptions = $config['handlers'][get_called_class()];
- if (!isset($this->usedOptions['separator'])) {
- $this->usedOptions['separator'] = self::SEPARATOR;
- }
- if (!$isInsert && !$needToChangeSlug) {
- $changeSet = $ea->getObjectChangeSet($this->om->getUnitOfWork(), $object);
- if (isset($changeSet[$this->usedOptions['relationField']])) {
- $needToChangeSlug = true;
- }
- }
- }
-
-
-
- public function postSlugBuild(SluggableAdapter $ea, array &$config, $object, &$slug)
- {
- $this->originalTransliterator = $this->sluggable->getTransliterator();
- $this->sluggable->setTransliterator(array($this, 'transliterate'));
- }
-
-
-
- public static function validate(array $options, ClassMetadata $meta)
- {
- if (!$meta->isSingleValuedAssociation($options['relationField'])) {
- throw new InvalidMappingException("Unable to find slug relation through field - [{$options['relationField']}] in class - {$meta->name}");
- }
- }
-
-
-
- public function onSlugCompletion(SluggableAdapter $ea, array &$config, $object, &$slug)
- {}
-
-
-
- public function transliterate($text, $separator, $object)
- {
- $result = call_user_func_array(
- $this->originalTransliterator,
- array($text, $separator, $object)
- );
- $wrapped = AbstractWrapper::wrap($object, $this->om);
- $relation = $wrapped->getPropertyValue($this->usedOptions['relationField']);
- if ($relation) {
- $wrappedRelation = AbstractWrapper::wrap($relation, $this->om);
- $slug = $wrappedRelation->getPropertyValue($this->usedOptions['relationSlugField']);
- $result = $slug . $this->usedOptions['separator'] . $result;
- }
- $this->sluggable->setTransliterator($this->originalTransliterator);
- return $result;
- }
-
-
-
- public function handlesUrlization()
- {
- return true;
- }
- }
|