<commit_before><?php

/*
 * This file is part of the Sonata package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 */

namespace Sonata\AdminBundle\Form\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Doctrine\Common\Util\ClassUtils;
use RuntimeException;

/**
 * Transform object to ID and property label
 *
 * @author <NAME> <<EMAIL>>
 */
class ModelToIdPropertyTransformer implements DataTransformerInterface
{
    protected $modelManager;

    protected $className;

    protected $property;

    protected $multiple;

    protected $toStringCallback;

    /**
     * @param ModelManagerInterface $modelManager
     * @param string                $className
     * @param string                $property
     */
    public function __construct(ModelManagerInterface $modelManager, $className, $property, $multiple=false, $toStringCallback=null)
    {
        $this->modelManager     = $modelManager;
        $this->className        = $className;
        $this->property         = $property;
        $this->multiple         = $multiple;
        $this->toStringCallback = $toStringCallback;
    }

    /**
     * {@inheritDoc}
     */
    public function reverseTransform($value)
    {
        $collection = $this->modelManager->getModelCollectionInstance($this->className);

        if (empty($value) || empty($value['identifiers'])) {
            if (!$this->multiple) {
                return null;
            } else {
                return $collection;
            }
        }

        if (!$this->multiple) {
             return $this->modelManager->find($this->className, current($value['identifiers']));
        }

        foreach ($value['identifiers'] as $id) {
            $collection->add($this->modelManager->find($this->className, $id));
        }

        return $collection;
    }

    /**
     * {@inheritDoc}
     */
    public function transform($entityOrCollection)
    {
        $result = array('identifiers' => array(), 'labels' => array());

        if (!$entityOrCollection) {
            return $result;
        }
        if (substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
            $collection = array($entityOrCollection);
        } elseif ($entityOrCollection instanceof \ArrayAccess) {
            $collection = $entityOrCollection;
        } else {
            $collection = array($entityOrCollection);
        }

        if (empty($this->property)) {
            throw new RuntimeException('Please define "property" parameter.');
        }

        foreach ($collection as $entity) {
            $id  = current($this->modelManager->getIdentifierValues($entity));

            if ($this->toStringCallback !== null) {
                if (!is_callable($this->toStringCallback)) {
                    throw new RuntimeException('Callback in "to_string_callback" option doesn`t contain callable function.');
                }

                $label = call_user_func($this->toStringCallback, $entity, $this->property);
            } else {
                try {
                    $label = (string) $entity;<commit_msg>Updated logic and added Exception messages when an incorrect configuration is used.<commit_after><?php

/*
 * This file is part of the Sonata package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 */

namespace Sonata\AdminBundle\Form\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Doctrine\Common\Util\ClassUtils;
use RuntimeException;

/**
 * Transform object to ID and property label
 *
 * @author <NAME> <<EMAIL>>
 */
class ModelToIdPropertyTransformer implements DataTransformerInterface
{
    protected $modelManager;

    protected $className;

    protected $property;

    protected $multiple;

    protected $toStringCallback;

    /**
     * @param ModelManagerInterface $modelManager
     * @param string                $className
     * @param string                $property
     */
    public function __construct(ModelManagerInterface $modelManager, $className, $property, $multiple=false, $toStringCallback=null)
    {
        $this->modelManager     = $modelManager;
        $this->className        = $className;
        $this->property         = $property;
        $this->multiple         = $multiple;
        $this->toStringCallback = $toStringCallback;
    }

    /**
     * {@inheritDoc}
     */
    public function reverseTransform($value)
    {
        $collection = $this->modelManager->getModelCollectionInstance($this->className);

        if (empty($value) || empty($value['identifiers'])) {
            if (!$this->multiple) {
                return null;
            } else {
                return $collection;
            }
        }

        if (!$this->multiple) {
             return $this->modelManager->find($this->className, current($value['identifiers']));
        }

        foreach ($value['identifiers'] as $id) {
            $collection->add($this->modelManager->find($this->className, $id));
        }

        return $collection;
    }

    /**
     * {@inheritDoc}
     */
    public function transform($entityOrCollection)
    {
        $result = array('identifiers' => array(), 'labels' => array());

        if (!$entityOrCollection) {
            return $result;
        }
        if ($this->multiple) {
            if (substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
                throw new \InvalidArgumentException('A multiple selection must be passed a collection not a single value.');
            } elseif ($entityOrCollection instanceof \ArrayAccess) {
                $collection = $entityOrCollection;
            } else {
                throw new \InvalidArgumentException('A multiple selection must be passed a collection not a single value.');
            }
        } else {
            if (substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
                $collection = array($entityOrCollection);
            } elseif ($entityOrCollection instanceof \ArrayAccess) {
                throw new \InvalidArgumentException('A single selection must be passed a single value not a collection.');
            } else {
                $collection = array($entityOrCollection);
            }
        }

        if (empty($this->property)) {
            throw new RuntimeException('Please define "property" parameter.');
        }

        foreach ($collection as $entity) {
            $id  = current($this->modelManager->getIdentifierValues($entity));

            if ($this->toStringCallback !== null) {
                if (!is_callable($this->toStringCallback)) {
                    throw new RuntimeException('Callback in "to_string_callback" option doesn`t contain callable function.');
                }

                $label = call_user_func($this->toStringCallback, $entity, $this->property);
            } else {
                try {
                    $label = (string) $entity;
{'fraction_non_alphanumeric': 0.12110868377935555, 'fraction_numerical': 0.00040961223375204805, 'mean_word_length': 2.3040144339197113, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 74, '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': '7145718', 'n_tokens_mistral': 1831, 'n_tokens_neox': 1784, 'n_words': 441}