<commit_before><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\ContentBundle\Slug;

use Darvin\Utils\Slug\SlugHandlerInterface;
use Doctrine\ORM\EntityManager;

/**
 * Unique slug handler
 */
class UniqueSlugHandler implements SlugHandlerInterface
{
    /**
     * {@inheritdoc}
     */
    public function handle(&$slug, &$suffix, EntityManager $em)
    {
        $similarSlugs = $this->getSlugMapItemRepository($em)->getSimilarSlugs($slug);

        if (empty($similarSlugs) || !in_array($slug, $similarSlugs)) {
            return;
        }

        $originalSlug = $slug;
        $index = 0;

        do {
            $index++;
            $slug = $originalSlug.'-'.$index;
        } while (in_array($slug, $similarSlugs));

        $suffix.='-'.$index;
    }<commit_msg>Implement sluggable manager in utils bundle.<commit_after><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\ContentBundle\Slug;

use Darvin\Utils\Sluggable\SlugHandlerInterface;
use Doctrine\ORM\EntityManager;

/**
 * Unique slug handler
 */
class UniqueSlugHandler implements SlugHandlerInterface
{
    /**
     * {@inheritdoc}
     */
    public function handle(&$slug, &$suffix, EntityManager $em)
    {
        $similarSlugs = $this->getSlugMapItemRepository($em)->getSimilarSlugs($slug);

        if (empty($similarSlugs) || !in_array($slug, $similarSlugs)) {
            return;
        }

        $originalSlug = $slug;
        $index = 0;

        do {
            $index++;
            $slug = $originalSlug.'-'.$index;
        } while (in_array($slug, $similarSlugs));

        $suffix.='-'.$index;
    }
{'fraction_non_alphanumeric': 0.13932038834951457, 'fraction_numerical': 0.0048543689320388345, 'mean_word_length': 2.7404718693284935, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2196739', 'n_tokens_mistral': 651, 'n_tokens_neox': 629, 'n_words': 145}