[DoctrineBundle] fixed CS

This commit is contained in:
Fabien Potencier 2010-11-03 18:43:50 +01:00
parent 4fc10310ef
commit ca485eb4ce

View File

@ -4,6 +4,7 @@ namespace Symfony\Bundle\DoctrineBundle\Form\ValueTransformer;
use Symfony\Component\Form\ValueTransformer\BaseValueTransformer;
use Symfony\Component\Form\ValueTransformer\TransformationFailedException;
use Doctrine\Common\Collections\Collection;
/*
* This file is part of the Symfony framework.
@ -15,7 +16,7 @@ use Symfony\Component\Form\ValueTransformer\TransformationFailedException;
*/
/**
* Transforms an instance of Doctrine\Common\Collections\Colletion into a string of unique names.
* Transforms an instance of Doctrine\Common\Collections\Collection into a string of unique names.
*
* Use-Cases for this transformer include: List of Tag-Names, List Of Group/User-Names or the like.
*
@ -39,17 +40,17 @@ class CollectionToStringTransformer extends BaseValueTransformer
{
$this->addOption('trim', true);
$this->addOption('separator', ',');
$this->addOption('explodeCallback', 'explode');
$this->addOption('implodeCallback', 'implode');
$this->addOption('createInstanceCallback', null);
$this->addOption('explode_callback', 'explode');
$this->addOption('implode_callback', 'implode');
$this->addOption('create_instance_callback', null);
$this->addRequiredOption('em');
$this->addRequiredOption('className');
$this->addRequiredOption('fieldName');
$this->addRequiredOption('class_name');
$this->addRequiredOption('field_name');
}
/**
* @param string $value
* @param Doctrine\Common\Collections\Collection $collection
* @param string $value
* @param Collection $collection
*/
public function reverseTransform($value, $collection)
{
@ -59,7 +60,7 @@ class CollectionToStringTransformer extends BaseValueTransformer
return $collection;
}
$callback = $this->getOption('explodeCallback');
$callback = $this->getOption('explode_callback');
$values = call_user_func($callback, $this->getOption('separator'), $value);
if ($this->getOption('trim') === true) {
@ -68,14 +69,14 @@ class CollectionToStringTransformer extends BaseValueTransformer
/* @var $em Doctrine\ORM\EntityManager */
$em = $this->getOption('em');
$className = $this->getOption('className');
$className = $this->getOption('class_name');
$reflField = $em->getClassMetadata($className)
->getReflectionProperty($this->getOption('fieldName'));
->getReflectionProperty($this->getOption('field_name'));
// 1. removing elements that are not yet present anymore
foreach ($collection AS $object) {
$uniqueIdent = $reflField->getValue($object);
$key = \array_search($uniqueIdent, $values);
$key = array_search($uniqueIdent, $values);
if ($key === false) {
$collection->removeElement($object);
} else {
@ -86,15 +87,15 @@ class CollectionToStringTransformer extends BaseValueTransformer
// 2. add elements that are known to the EntityManager but newly connected, query them from the repository
if (count($values)) {
$dql = "SELECT o FROM " . $className . " o WHERE o." . $this->getOption('fieldName') . " IN (";
$dql = sprintf('SELECT o FROM %s o WHERE o.%s IN (', $className, $this->getOption('field_name'));
$query = $em->createQuery();
$needles = array();
$i = 0;
foreach ($values AS $val) {
$query->setParameter(++$i, $val);
$needles[] = "?" . $i;
$needles[] = '?'.$i;
}
$dql .= implode(",", $needles) . ")";
$dql .= implode(',', $needles).')';
$query->setDql($dql);
$newElements = $query->getResult();
@ -102,24 +103,22 @@ class CollectionToStringTransformer extends BaseValueTransformer
$collection->add($object);
$uniqueIdent = $reflField->getValue($object);
$key = \array_search($uniqueIdent, $values);
$key = array_search($uniqueIdent, $values);
unset($values[$key]);
}
}
// 3. new elements that are not in the repository have to be created and persisted then attached:
if (count($values)) {
$callback = $this->getOption('createInstanceCallback');
if (!$callback || !\is_callable($callback)) {
throw new TransformationFailedException("Cannot transform list of identifiers, because a new ".
"element was detected and it is unknown how to create an instance of this element.");
$callback = $this->getOption('create_instance_callback');
if (!$callback || !is_callable($callback)) {
throw new TransformationFailedException('Cannot transform list of identifiers, because a new element was detected and it is unknown how to create an instance of this element.');
}
foreach ($values AS $newValue) {
$newInstance = \call_user_func($callback, $newValue);
$newInstance = call_user_func($callback, $newValue);
if (!($newInstance instanceof $className)) {
throw new TransformationFailedException("Error while trying to create a new instance for ".
"the identifier '" . $newValue . "'. No new instance was created.");
throw new TransformationFailedException(sprintf('Error while trying to create a new instance for the identifier "%s". No new instance was created.', $newValue));
}
$collection->add($newInstance);
$em->persist($newInstance);
@ -132,20 +131,21 @@ class CollectionToStringTransformer extends BaseValueTransformer
/**
* Transform a Doctrine Collection into a string of identifies with a separator.
*
* @param Doctrine\Common\Collections\Collection $value
* @param Collection $value
* @return string
*/
public function transform($value)
{
$values = array();
$em = $this->getOption('em');
$reflField = $em->getClassMetadata($this->getOption('className'))
->getReflectionProperty($this->getOption('fieldName'));
$reflField = $em->getClassMetadata($this->getOption('class_name'))
->getReflectionProperty($this->getOption('field_name'));
foreach ($value AS $object) {
$values[] = $reflField->getValue($object);
}
$callback = $this->getOption('implodeCallback');
$callback = $this->getOption('implode_callback');
return call_user_func($callback, $this->getOption('separator'), $values);
}
}
}