Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* 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 Sonata\AdminBundle\Model\ModelManagerInterface;
use Symfony\Component\Form\DataTransformerInterface;
/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
class ArrayToModelTransformer implements DataTransformerInterface
{
/**
* @var ModelManagerInterface
*/
protected $modelManager;
/**
* @var string
*/
protected $className;
/**
* @param ModelManagerInterface $modelManager
* @param string $className
*/
public function __construct(ModelManagerInterface $modelManager, $className)
{
$this->modelManager = $modelManager;
$this->className = $className;
}
/**
* {@inheritdoc}
*/
public function reverseTransform($array)
{
// when the object is created the form return an array
// one the object is persisted, the edit $array is the user instance
if ($array instanceof $this->className) {
return $array;
}
$instance = new $this->className();
if (!is_array($array)) {
return $instance;
}
return $this->modelManager->modelReverseTransform($this->className, $array);
}
/**
* {@inheritdoc}
*/
public function transform($value)
{
return $value;
}
}

View File

@@ -0,0 +1,115 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* 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 Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
/**
* NEXT_MAJOR: remove this class when dropping Symfony < 2.7 support.
*
* This class should be used with Symfony <2.7 only and will be deprecated with 3.0.
*
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
class LegacyModelsToArrayTransformer implements DataTransformerInterface
{
/**
* @var ModelChoiceList
*/
protected $choiceList;
/**
* @param ModelChoiceList $choiceList
*/
public function __construct(ModelChoiceList $choiceList)
{
if (interface_exists('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')) {
@trigger_error(
'The '.__CLASS__.' class is deprecated since 3.11, to be removed in 4.0. '.
'Use Sonata\AdminBundle\Form\DataTransformer\ModelsToArrayTransformer instead.',
E_USER_DEPRECATED
);
}
$this->choiceList = $choiceList;
}
/**
* {@inheritdoc}
*/
public function transform($collection)
{
if (null === $collection) {
return [];
}
$array = [];
if (count($this->choiceList->getIdentifier()) > 1) {
// load all choices
$availableEntities = $this->choiceList->getEntities();
foreach ($collection as $entity) {
// identify choices by their collection key
$key = array_search($entity, $availableEntities);
$array[] = $key;
}
} else {
foreach ($collection as $entity) {
$array[] = current($this->choiceList->getIdentifierValues($entity));
}
}
return $array;
}
/**
* {@inheritdoc}
*/
public function reverseTransform($keys)
{
$collection = $this->choiceList->getModelManager()->getModelCollectionInstance(
$this->choiceList->getClass()
);
if (!$collection instanceof \ArrayAccess) {
throw new UnexpectedTypeException($collection, '\ArrayAccess');
}
if ('' === $keys || null === $keys) {
return $collection;
}
if (!is_array($keys)) {
throw new UnexpectedTypeException($keys, 'array');
}
$notFound = [];
// optimize this into a SELECT WHERE IN query
foreach ($keys as $key) {
if ($entity = $this->choiceList->getEntity($key)) {
$collection[] = $entity;
} else {
$notFound[] = $key;
}
}
if (count($notFound) > 0) {
throw new TransformationFailedException(sprintf('The entities with keys "%s" could not be found', implode('", "', $notFound)));
}
return $collection;
}
}

View File

@@ -0,0 +1,157 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* 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 Doctrine\Common\Util\ClassUtils;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Symfony\Component\Form\DataTransformerInterface;
/**
* Transform object to ID and property label.
*
* @author Andrej Hudec <pulzarraider@gmail.com>
*/
class ModelToIdPropertyTransformer implements DataTransformerInterface
{
/**
* @var ModelManagerInterface
*/
protected $modelManager;
/**
* @var string
*/
protected $className;
/**
* @var string
*/
protected $property;
/**
* @var bool
*/
protected $multiple;
/**
* @var callable|null
*/
protected $toStringCallback;
/**
* @param ModelManagerInterface $modelManager
* @param string $className
* @param string $property
* @param bool $multiple
* @param null $toStringCallback
*/
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)) {
if ($this->multiple) {
return $collection;
}
return;
}
if (!$this->multiple) {
return $this->modelManager->find($this->className, $value);
}
if (!is_array($value)) {
throw new \UnexpectedValueException(sprintf('Value should be array, %s given.', gettype($value)));
}
foreach ($value as $key => $id) {
if ($key === '_labels') {
continue;
}
$collection->add($this->modelManager->find($this->className, $id));
}
return $collection;
}
/**
* {@inheritdoc}
*/
public function transform($entityOrCollection)
{
$result = [];
if (!$entityOrCollection) {
return $result;
}
if ($this->multiple) {
$isArray = is_array($entityOrCollection);
if (!$isArray && 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. Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true" is set for many-to-many or one-to-many relations.');
} elseif ($isArray || ($entityOrCollection instanceof \ArrayAccess)) {
$collection = $entityOrCollection;
} else {
throw new \InvalidArgumentException('A multiple selection must be passed a collection not a single value. Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true" is set for many-to-many or one-to-many relations.');
}
} else {
if (substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
$collection = [$entityOrCollection];
} elseif ($entityOrCollection instanceof \ArrayAccess) {
throw new \InvalidArgumentException('A single selection must be passed a single value not a collection. Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true" is set for many-to-many or one-to-many relations.');
} else {
$collection = [$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;
} catch (\Exception $e) {
throw new \RuntimeException(sprintf("Unable to convert the entity %s to String, entity must have a '__toString()' method defined", ClassUtils::getClass($entity)), 0, $e);
}
}
$result[] = $id;
$result['_labels'][] = $label;
}
return $result;
}
}

View File

@@ -0,0 +1,65 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* 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 Sonata\AdminBundle\Model\ModelManagerInterface;
use Symfony\Component\Form\DataTransformerInterface;
/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
class ModelToIdTransformer implements DataTransformerInterface
{
/**
* @var ModelManagerInterface
*/
protected $modelManager;
/**
* @var string
*/
protected $className;
/**
* @param ModelManagerInterface $modelManager
* @param string $className
*/
public function __construct(ModelManagerInterface $modelManager, $className)
{
$this->modelManager = $modelManager;
$this->className = $className;
}
/**
* {@inheritdoc}
*/
public function reverseTransform($newId)
{
if (empty($newId) && !in_array($newId, ['0', 0], true)) {
return;
}
return $this->modelManager->find($this->className, $newId);
}
/**
* {@inheritdoc}
*/
public function transform($entity)
{
if (empty($entity)) {
return;
}
return $this->modelManager->getNormalizedIdentifier($entity);
}
}

View File

@@ -0,0 +1,217 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* 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 Doctrine\Common\Util\ClassUtils;
use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceLoader;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Sonata\CoreBundle\Model\Adapter\AdapterInterface;
use Symfony\Component\Form\ChoiceList\LazyChoiceList;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\RuntimeException;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
class ModelsToArrayTransformer implements DataTransformerInterface
{
/**
* @var ModelManagerInterface
*/
protected $modelManager;
/**
* @var string
*/
protected $class;
/**
* @var ModelChoiceList
*
* @deprecated since 3.12, to be removed in 4.0
* NEXT_MAJOR: remove this property
*/
protected $choiceList;
/**
* ModelsToArrayTransformer constructor.
*
* @param ModelChoiceList|LazyChoiceList|ModelChoiceLoader $choiceList
* @param ModelManagerInterface $modelManager
* @param $class
*
* @throws RuntimeException
*/
public function __construct($choiceList, $modelManager, $class = null)
{
/*
NEXT_MAJOR: Remove condition , magic methods, legacyConstructor() method, $choiceList property and argument
__construct() signature should be : public function __construct(ModelManager $modelManager, $class)
*/
$args = func_get_args();
if (func_num_args() == 3) {
$this->legacyConstructor($args);
} else {
$this->modelManager = $args[0];
$this->class = $args[1];
}
}
/**
* @internal
*/
public function __get($name)
{
if ('choiceList' === $name) {
$this->triggerDeprecation();
}
return $this->$name;
}
/**
* @internal
*/
public function __set($name, $value)
{
if ('choiceList' === $name) {
$this->triggerDeprecation();
}
$this->$name = $value;
}
/**
* @internal
*/
public function __isset($name)
{
if ('choiceList' === $name) {
$this->triggerDeprecation();
}
return isset($this->$name);
}
/**
* @internal
*/
public function __unset($name)
{
if ('choiceList' === $name) {
$this->triggerDeprecation();
}
unset($this->$name);
}
/**
* {@inheritdoc}
*/
public function transform($collection)
{
if (null === $collection) {
return [];
}
$array = [];
foreach ($collection as $key => $entity) {
$id = implode(AdapterInterface::ID_SEPARATOR, $this->getIdentifierValues($entity));
$array[] = $id;
}
return $array;
}
/**
* {@inheritdoc}
*/
public function reverseTransform($keys)
{
if (!is_array($keys)) {
throw new UnexpectedTypeException($keys, 'array');
}
$collection = $this->modelManager->getModelCollectionInstance($this->class);
$notFound = [];
// optimize this into a SELECT WHERE IN query
foreach ($keys as $key) {
if ($entity = $this->modelManager->find($this->class, $key)) {
$collection[] = $entity;
} else {
$notFound[] = $key;
}
}
if (count($notFound) > 0) {
throw new TransformationFailedException(sprintf('The entities with keys "%s" could not be found', implode('", "', $notFound)));
}
return $collection;
}
/**
* Simulates the old constructor for BC.
*
* @param array $args
*
* @throws RuntimeException
*/
private function legacyConstructor($args)
{
$choiceList = $args[0];
if (!$choiceList instanceof ModelChoiceList
&& !$choiceList instanceof ModelChoiceLoader
&& !$choiceList instanceof LazyChoiceList) {
throw new RuntimeException('First param passed to ModelsToArrayTransformer should be instance of
ModelChoiceLoader or ModelChoiceList or LazyChoiceList');
}
$this->choiceList = $choiceList;
$this->modelManager = $args[1];
$this->class = $args[2];
}
/**
* @param object $entity
*
* @return array
*/
private function getIdentifierValues($entity)
{
try {
return $this->modelManager->getIdentifierValues($entity);
} catch (\Exception $e) {
throw new \InvalidArgumentException(sprintf('Unable to retrieve the identifier values for entity %s', ClassUtils::getClass($entity)), 0, $e);
}
}
/**
* @internal
*/
private function triggerDeprecation()
{
@trigger_error(sprintf(
'Using the "%s::$choiceList" property is deprecated since version 3.12 and will be removed in 4.0.',
__CLASS__),
E_USER_DEPRECATED)
;
}
}