<commit_before><?php

namespace Oro\Bundle\TagBundle\Form;

use Doctrine\ORM\QueryBuilder;

use Oro\Bundle\FormBundle\Form\DataTransformer\EntityToIdTransformer;
use Oro\Bundle\TagBundle\Entity\TagManager;
use Symfony\Component\Form\Util\PropertyPath;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;

/**
 * Transforms between array of entities and array of ids
 */
class TagsTransformer extends EntityToIdTransformer
{
    protected $tagManager;

    /**
     * {@inheritdoc}
     */
    public function transform($value)
    {
        if (null === $value || array() === $value) {
            return array();
        }

        if (!(is_array($value) || $value instanceof \ArrayAccess)) {
            throw new UnexpectedTypeException($value, 'array');
        }

        $result = array();
        foreach ($value as $entity) {
            $id = $this->propertyPath->getValue($entity);
            $result[] = $id;
        }

        return $result;
    }

    /**
     * {@inheritdoc}
     */
    public function reverseTransform($values)
    {
        if (!is_array($values)) {
            $values = explode(',', $values);
        }

        $newValues = array_filter(
            $values,
            function ($item) {
                return !intval($item);
            }
        );

        $values = array_filter(
            $values,
            function ($item) {
                return intval($item);
            }
        );

        $entities = array();
        if ($values) {
            $entities = $this->loadEntitiesByIds($values);
        }

        if ($newValues) {
            $entities = array_merge($entities, $this->tagManager->loadOrCreateTags($newValues));
        }

        if (count($entities) !== count($values) + count($newValues)) {<commit_msg>BAP-1104: Create/assign tags on entities - fix empty tag when no tags assigned<commit_after><?php

namespace Oro\Bundle\TagBundle\Form;

use Doctrine\ORM\QueryBuilder;

use Oro\Bundle\FormBundle\Form\DataTransformer\EntityToIdTransformer;
use Oro\Bundle\TagBundle\Entity\TagManager;
use Symfony\Component\Form\Util\PropertyPath;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;

/**
 * Transforms between array of entities and array of ids
 */
class TagsTransformer extends EntityToIdTransformer
{
    protected $tagManager;

    /**
     * {@inheritdoc}
     */
    public function transform($value)
    {
        if (null === $value || array() === $value) {
            return array();
        }

        if (!(is_array($value) || $value instanceof \ArrayAccess)) {
            throw new UnexpectedTypeException($value, 'array');
        }

        $result = array();
        foreach ($value as $entity) {
            $id = $this->propertyPath->getValue($entity);
            $result[] = $id;
        }

        return $result;
    }

    /**
     * {@inheritdoc}
     */
    public function reverseTransform($values)
    {
        if (!is_array($values)) {
            $values = explode(',', $values);
        }

        $newValues = array_filter(
            $values,
            function ($item) {
                return !intval($item) && !empty($item);
            }
        );

        $values = array_filter(
            $values,
            function ($item) {
                return intval($item);
            }
        );

        $entities = array();
        if ($values) {
            $entities = $this->loadEntitiesByIds($values);
        }

        if ($newValues) {
            $entities = array_merge($entities, $this->tagManager->loadOrCreateTags($newValues));
        }

        if (count($entities) !== count($values) + count($newValues)) {
{'fraction_non_alphanumeric': 0.13428120063191154, 'fraction_numerical': 0.00105318588730911, 'mean_word_length': 2.119047619047619, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '3514781', 'n_tokens_mistral': 1056, 'n_tokens_neox': 1043, 'n_words': 212}