<commit_before><?php

namespace MW\Bundle\TagAdminBundle\Form\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineExtensions\Taggable\TagManager;

class TextToTagTransformer implements DataTransformerInterface
{

    /**
     * @var TagManager
     */
    private $tagManager;

    /**
     * @param ObjectManager $om
     */
    public function __construct(TagManager $tagManager)
    {
        $this->tagManager = $tagManager;
    }

    public function transform($object)
    {
        if (null === $object) {
            return $object;
        }

        $this->tagManager->loadTagging($object);

        $object->setTagsPlain(implode(' ', $object->getTags()->toArray()));

        return $object;
    }

    public function reverseTransform($object)
    {
        if (!$object) {
            return $object;
        }

        $tags_plain = $object->getTagsPlain();
        $tag_list = explode(' ', $tags_plain);
        $tags = $object->getTags()->toArray();
        foreach ($tag_list as $value) {
            $tag = $this->tagManager->loadOrCreateTag($value);
            if (!in_array($tag, $tags)) {
                $this->tagManager->addTag($tag, $object);
            }
        }

        foreach ($tags as $tag) {
            if (!in_array($tag->__toString(), $tag_list)) {
                $this->tagManager->removeTag($tag, $object);
            }
        }

        return $object;
    }

    // Todo: Move this out into a manager class
    public function postPersist($object)<commit_msg>Use comma as separator instead of space<commit_after><?php

namespace MW\Bundle\TagAdminBundle\Form\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineExtensions\Taggable\TagManager;

class TextToTagTransformer implements DataTransformerInterface
{

    /**
     * @var TagManager
     */
    private $tagManager;

    /**
     * @param ObjectManager $om
     */
    public function __construct(TagManager $tagManager)
    {
        $this->tagManager = $tagManager;
    }

    public function transform($object)
    {
        if (null === $object) {
            return $object;
        }

        $this->tagManager->loadTagging($object);

        $object->setTagsPlain(implode(', ', $object->getTags()->toArray()));

        return $object;
    }

    public function reverseTransform($object)
    {
        if (!$object) {
            return $object;
        }

        $tags_plain = $object->getTagsPlain();
        $tag_list = array_unique(preg_split('/ *[,\n\r] */', $tags_plain, NULL, PREG_SPLIT_NO_EMPTY));
        $tags = $object->getTags()->toArray();
        foreach ($tag_list as $value) {
            $tag = $this->tagManager->loadOrCreateTag($value);
            if (!in_array($tag, $tags)) {
                $this->tagManager->addTag($tag, $object);
            }
        }

        foreach ($tags as $tag) {
            if (!in_array($tag->__toString(), $tag_list)) {
                $this->tagManager->removeTag($tag, $object);
            }
        }

        return $object;
    }

    // Todo: Move this out into a manager class
    public function postPersist($object)
{'fraction_non_alphanumeric': 0.13665743305632502, 'fraction_numerical': 0.0, 'mean_word_length': 2.289473684210526, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5386291', 'n_tokens_mistral': 956, 'n_tokens_neox': 928, 'n_words': 180}