-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added sluggable strategy
CheckOnlyStrategy
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |