Skip to content

Commit

Permalink
Simplify by extracting variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean85 committed Nov 12, 2024
1 parent 4a89cd3 commit 3975fa1
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Sluggable/SluggableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,12 @@ private function generateSlug(SluggableAdapter $ea, object $object): void
}

// cut slug if exceeded in length
if (isset($mapping['length']) && strlen($slug) > ($mapping->length ?? $mapping['length'])) {
$slug = substr($slug, 0, $mapping->length ?? $mapping['length']);
$length = $mapping->length ?? $mapping['length'] ?? null;
if (null !== $length && strlen($slug) > $length) {
$slug = substr($slug, 0, $length);
}

if (isset($mapping['nullable']) && ($mapping->nullable ?? $mapping['nullable']) && 0 === strlen($slug)) {
if (($mapping->nullable ?? $mapping['nullable'] ?? false) && 0 === strlen($slug)) {
$slug = null;
}

Expand Down Expand Up @@ -546,11 +547,12 @@ private function makeUniqueSlug(SluggableAdapter $ea, object $object, string $pr
}

$mapping = $meta->getFieldMapping($config['slug']);
if (isset($mapping['length']) && strlen($generatedSlug) > ($mapping->length ?? $mapping['length'])) {
$length = $mapping->length ?? $mapping['length'] ?? null;
if (null !== $length && strlen($generatedSlug) > $length) {
$generatedSlug = substr(
$generatedSlug,
0,
($mapping->length ?? $mapping['length']) - (strlen($uniqueSuffix) + strlen($config['separator']))
$length - (strlen($uniqueSuffix) + strlen($config['separator']))
);
$this->exponent = strlen($uniqueSuffix) - 1;
if (substr($generatedSlug, -strlen($config['separator'])) == $config['separator']) {
Expand Down

0 comments on commit 3975fa1

Please sign in to comment.