Skip to content

Commit

Permalink
Added sluggable strategy CheckOnlyStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
tg666 committed Oct 9, 2019
1 parent 65b3183 commit be7bcef
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/Strategy/CheckOnlyStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace SixtyEightPublishers\DoctrineSluggable\Strategy;

use SixtyEightPublishers;

/**
* Check only, slug field must be set manually.
*
* $options = array()
*/
final class CheckOnlyStrategy extends SixtyEightPublishers\DoctrineSluggable\AbstractAdjustableObject implements ISluggableStrategy
{
/**
* {@inheritdoc}
*/
public function doInsert(SixtyEightPublishers\DoctrineSluggable\Definition\SluggableDefinition $definition, SixtyEightPublishers\DoctrineSluggable\EntityAdapter\IEntityAdapter $adapter): void
{
$this->checkUnique($definition, $adapter);
}

/**
* {@inheritdoc}
*/
public function doUpdate(SixtyEightPublishers\DoctrineSluggable\Definition\SluggableDefinition $definition, SixtyEightPublishers\DoctrineSluggable\EntityAdapter\IEntityAdapter $adapter): void
{
$fieldName = $definition->getFieldName();
$changes = $adapter->getEntityManager()->getUnitOfWork()->getEntityChangeSet($adapter->getEntity());

if (!isset($changes[$fieldName]) || $changes[$fieldName][0] === $changes[$fieldName][1]) {
return;
}

$this->checkUnique($definition, $adapter);
}

/**
* @param \SixtyEightPublishers\DoctrineSluggable\Definition\SluggableDefinition $definition
* @param \SixtyEightPublishers\DoctrineSluggable\EntityAdapter\IEntityAdapter $adapter
*
* @return void
*/
private function checkUnique(SixtyEightPublishers\DoctrineSluggable\Definition\SluggableDefinition $definition, SixtyEightPublishers\DoctrineSluggable\EntityAdapter\IEntityAdapter $adapter): void
{
$fieldName = $definition->getFieldName();
$slug = $adapter->getValue($fieldName);

if (!$definition->getUniquer()->isUnique($slug, $adapter, $definition->getFinder())) {
throw new SixtyEightPublishers\DoctrineSluggable\Exception\UniqueSlugException($slug, $adapter->getEntity(), $fieldName);
}

# add slug between persisted
$definition->getFinder()->addPersistedSlug($adapter, $fieldName, $slug);
}
}

0 comments on commit be7bcef

Please sign in to comment.