<commit_before><?php
/**
 * @author    <NAME>     <<EMAIL>>
 * @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\ImageBundle\Namer;

use Darvin\Utils\Transliteratable\TransliteratorInterface;
use Vich\UploaderBundle\Mapping\PropertyMapping;
use Vich\UploaderBundle\Naming\NamerInterface;

/**
 * File namer
 */
class FileNamer implements NamerInterface
{
    /**
     * @var \Darvin\Utils\Transliteratable\TransliteratorInterface
     */
    protected $transliterator;

    /**
     * @param \Darvin\Utils\Transliteratable\TransliteratorInterface $transliterator Transliterator
     */
    public function __construct(TransliteratorInterface $transliterator)
    {
        $this->transliterator = $transliterator;
    }

    /**
     * {@inheritdoc}
     */
    public function name($object, PropertyMapping $mapping)
    {
        $uploadDir = $mapping->getUploadDestination().DIRECTORY_SEPARATOR.$mapping->getUploadDir($object);

        /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
        $file = $mapping->getFile($object);

        $name = preg_replace(sprintf('/\.%s$/', $file->getClientOriginalExtension()), '', $file->getClientOriginalName());

        $name = str_replace('.', '_', $name);

        $name = $this->transliterator->transliterate($name, true, ['_'], '_').'.'.$file->guessExtension();

        $name = $this->makeNameUnique($name, $uploadDir);

        return $name;
    }

    /**
     * @param string $name      File name
     * @param string $uploadDir Upload directory
     *
     * @return string
     */
    protected function makeNameUnique($name, $uploadDir)
    {
        $suffix = $name;

        $prefix = 0;

        while (is_file($uploadDir.DIRECTORY_SEPARATOR.$name)) {
            $prefix++;
            $name = $prefix.'_'.$suffix;
        }

        return $name;
    }
}
<commit_msg>Change file namer logic.<commit_after><?php
/**
 * @author    <NAME>     <<EMAIL>>
 * @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\ImageBundle\Namer;

use Darvin\Utils\Transliteratable\TransliteratorInterface;
use Vich\UploaderBundle\Mapping\PropertyMapping;
use Vich\UploaderBundle\Naming\NamerInterface;

/**
 * File namer
 */
class FileNamer implements NamerInterface
{
    /**
     * @var \Darvin\Utils\Transliteratable\TransliteratorInterface
     */
    protected $transliterator;

    /**
     * @param \Darvin\Utils\Transliteratable\TransliteratorInterface $transliterator Transliterator
     */
    public function __construct(TransliteratorInterface $transliterator)
    {
        $this->transliterator = $transliterator;
    }

    /**
     * {@inheritdoc}
     */
    public function name($object, PropertyMapping $mapping)
    {
        $uploadDir = $mapping->getUploadDestination().DIRECTORY_SEPARATOR.$mapping->getUploadDir($object);

        /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
        $file = $mapping->getFile($object);

        $extension = $file->guessExtension();

        $name = preg_replace(sprintf('/\.%s$/', $file->getClientOriginalExtension()), '', $file->getClientOriginalName());
        $name = str_replace('.', '-', $name);
        $name = $this->transliterator->transliterate($name);
        $name = $this->makeNameUnique($name, $uploadDir, $extension);

        return $name.'.'.$extension;
    }

    /**
     * @param string $name      File name
     * @param string $uploadDir Upload directory
     * @param string $extension File extension
     *
     * @return string
     */
    protected function makeNameUnique($name, $uploadDir, $extension)
    {
        $prefix = $name;

        $suffix = 0;

        while (is_file(sprintf('%s%s%s.%s', $uploadDir, DIRECTORY_SEPARATOR, $name, $extension))) {
            $suffix++;
            $name = $prefix.'-'.$suffix;
        }

        return $name;
    }
}

{'fraction_non_alphanumeric': 0.14205476246197052, 'fraction_numerical': 0.002340276152586005, 'mean_word_length': 2.9247015610651976, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 35, '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': '3469837', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1229, 'n_words': 250}