Skip to content

Commit

Permalink
Narrow Form and Mapper API
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed Aug 9, 2020
1 parent 2cd3d69 commit e3328bc
Show file tree
Hide file tree
Showing 42 changed files with 182 additions and 180 deletions.
2 changes: 0 additions & 2 deletions src/Admin/AbstractAdmin.php
Expand Up @@ -1747,8 +1747,6 @@ public function isCurrentChild(): bool

/**
* Returns the current child admin instance.
*
* @return AdminInterface|null the current child admin instance
*/
public function getCurrentChildAdmin(): ?AdminInterface
{
Expand Down
7 changes: 1 addition & 6 deletions src/Admin/AdminInterface.php
Expand Up @@ -135,12 +135,7 @@ public function getSecurityInformation(): array;

public function setParentFieldDescription(FieldDescriptionInterface $parentFieldDescription): void;

/**
* Get parent field description.
*
* @return FieldDescriptionInterface The parent field description
*/
public function getParentFieldDescription(): ?FieldDescriptionInterface;
public function getParentFieldDescription(): FieldDescriptionInterface;

/**
* Returns true if the Admin is linked to a parent FieldDescription.
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/FieldDescriptionRegistryInterface.php
Expand Up @@ -23,7 +23,7 @@ interface FieldDescriptionRegistryInterface
/**
* Return FormFieldDescription.
*/
public function getFormFieldDescription(string $name): ?FieldDescriptionInterface;
public function getFormFieldDescription(string $name): FieldDescriptionInterface;

public function hasFormFieldDescription(string $name): bool;

Expand Down
4 changes: 2 additions & 2 deletions src/Datagrid/DatagridMapper.php
Expand Up @@ -118,15 +118,15 @@ final public function keys(): array
return array_keys($this->datagrid->getFilters());
}

public function remove(string $key): self
public function remove(string $key): BaseMapper
{
$this->admin->removeFilterFieldDescription($key);
$this->datagrid->removeFilter($key);

return $this;
}

public function reorder(array $keys): self
public function reorder(array $keys): BaseMapper
{
$this->datagrid->reorderFilters($keys);

Expand Down
4 changes: 2 additions & 2 deletions src/Datagrid/ListMapper.php
Expand Up @@ -156,7 +156,7 @@ public function has(string $key): bool
return $this->list->has($key);
}

public function remove(string $key): self
public function remove(string $key): BaseMapper
{
$this->admin->removeListFieldDescription($key);
$this->list->remove($key);
Expand All @@ -169,7 +169,7 @@ final public function keys(): array
return array_keys($this->list->getElements());
}

public function reorder(array $keys): self
public function reorder(array $keys): BaseMapper
{
$this->list->reorder($keys);

Expand Down
70 changes: 41 additions & 29 deletions src/Form/ChoiceList/ModelChoiceLoader.php
Expand Up @@ -17,21 +17,24 @@
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Sonata\Doctrine\Adapter\AdapterInterface;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\Exception\RuntimeException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\PropertyAccess\PropertyPath;

/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
final class ModelChoiceLoader implements ChoiceLoaderInterface
{
/**
* @var string[]
*/
public $identifier;

/**
* @var \Sonata\AdminBundle\Model\ModelManagerInterface
* @var ModelManagerInterface
*/
private $modelManager;

Expand All @@ -40,70 +43,69 @@ final class ModelChoiceLoader implements ChoiceLoaderInterface
*/
private $class;

/**
* @var string|null
*/
private $property;

/**
* @var mixed
*/
private $query;

private $choices;

/**
* @var PropertyPath
* @var object[]
*/
private $propertyPath;
private $choices;

/**
* @var PropertyAccessorInterface
*/
private $propertyAccessor;

/**
* @var ChoiceListInterface|null
*/
private $choiceList;

/**
* @param string $class
* @param string|null $property
* @param mixed|null $query
* @param array $choices
* @param mixed $query
* @param object[] $choices
*/
public function __construct(
ModelManagerInterface $modelManager,
$class,
$property = null,
string $class,
?string $property = null,
$query = null,
$choices = [],
array $choices = [],
?PropertyAccessorInterface $propertyAccessor = null
) {
$this->modelManager = $modelManager;
$this->class = $class;
$this->property = $property;
$this->query = $query;
$this->choices = $choices;
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();

$this->identifier = $this->modelManager->getIdentifierFieldNames($this->class);

// The property option defines, which property (path) is used for
// displaying entities as strings
if ($property) {
$this->propertyPath = new PropertyPath($property);
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
}
}

public function loadChoiceList($value = null)
public function loadChoiceList($value = null): ChoiceListInterface
{
if (!$this->choiceList) {
if ($this->query) {
$entities = $this->modelManager->executeQuery($this->query);
} elseif (\is_array($this->choices) && \count($this->choices) > 0) {
} elseif (\count($this->choices) > 0) {
$entities = $this->choices;
} else {
$entities = $this->modelManager->findBy($this->class);
}

$choices = [];
foreach ($entities as $key => $model) {
if ($this->propertyPath) {
foreach ($entities as $model) {
if (null !== $this->property) {
// If the property option was given, use it
$valueObject = $this->propertyAccessor->getValue($model, $this->propertyPath);
$valueObject = $this->propertyAccessor->getValue($model, $this->property);
} else {
// Otherwise expect a __toString() method in the entity
try {
Expand Down Expand Up @@ -143,20 +145,30 @@ public function loadChoiceList($value = null)
return $this->choiceList;
}

public function loadChoicesForValues(array $values, $value = null)
/**
* @param string[] $values
*
* @return object[]
*/
public function loadChoicesForValues(array $values, $value = null): array
{
return $this->loadChoiceList($value)->getChoicesForValues($values);
}

public function loadValuesForChoices(array $choices, $value = null)
/**
* @param object[] $choices
*
* @return string[]
*/
public function loadValuesForChoices(array $choices, $value = null): array
{
return $this->loadChoiceList($value)->getValuesForChoices($choices);
}

/**
* @param object $model
* @return mixed[]
*/
private function getIdentifierValues($model): array
private function getIdentifierValues(object $model): array
{
try {
return $this->modelManager->getIdentifierValues($model);
Expand Down
5 changes: 1 addition & 4 deletions src/Form/DataTransformer/ArrayToModelTransformer.php
Expand Up @@ -31,10 +31,7 @@ final class ArrayToModelTransformer implements DataTransformerInterface
*/
private $className;

/**
* @param string $className
*/
public function __construct(ModelManagerInterface $modelManager, $className)
public function __construct(ModelManagerInterface $modelManager, string $className)
{
$this->modelManager = $modelManager;
$this->className = $className;
Expand Down
28 changes: 11 additions & 17 deletions src/Form/DataTransformer/ModelToIdPropertyTransformer.php
Expand Up @@ -49,18 +49,12 @@ final class ModelToIdPropertyTransformer implements DataTransformerInterface
*/
private $toStringCallback;

/**
* @param string $className
* @param string $property
* @param bool $multiple
* @param callable|null $toStringCallback
*/
public function __construct(
ModelManagerInterface $modelManager,
$className,
$property,
$multiple = false,
$toStringCallback = null
string $className,
string $property,
bool $multiple = false,
?callable $toStringCallback = null
) {
$this->modelManager = $modelManager;
$this->className = $className;
Expand All @@ -69,6 +63,9 @@ public function __construct(
$this->toStringCallback = $toStringCallback;
}

/**
* @throws \UnexpectedValueException
*/
public function reverseTransform($value)
{
$collection = $this->modelManager->getModelCollectionInstance($this->className);
Expand Down Expand Up @@ -100,6 +97,9 @@ public function reverseTransform($value)
return $collection;
}

/**
* @throws \InvalidArgumentException
*/
public function transform($entityOrCollection)
{
$result = [];
Expand Down Expand Up @@ -140,20 +140,14 @@ public function transform($entityOrCollection)
}
}

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

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

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

$label = ($this->toStringCallback)($model, $this->property);
} else {
try {
Expand Down
5 changes: 1 addition & 4 deletions src/Form/DataTransformer/ModelToIdTransformer.php
Expand Up @@ -31,10 +31,7 @@ final class ModelToIdTransformer implements DataTransformerInterface
*/
private $className;

/**
* @param string $className
*/
public function __construct(ModelManagerInterface $modelManager, $className)
public function __construct(ModelManagerInterface $modelManager, string $className)
{
$this->modelManager = $modelManager;
$this->className = $className;
Expand Down
11 changes: 5 additions & 6 deletions src/Form/DataTransformer/ModelsToArrayTransformer.php
Expand Up @@ -17,7 +17,6 @@
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Sonata\Doctrine\Adapter\AdapterInterface;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\RuntimeException;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;

Expand All @@ -36,9 +35,6 @@ final class ModelsToArrayTransformer implements DataTransformerInterface
*/
private $class;

/**
* @throws RuntimeException
*/
public function __construct(ModelManagerInterface $modelManager, string $class)
{
$this->modelManager = $modelManager;
Expand All @@ -61,6 +57,9 @@ public function transform($collection)
return $array;
}

/**
* @throws UnexpectedTypeException
*/
public function reverseTransform($keys)
{
if (!\is_array($keys)) {
Expand Down Expand Up @@ -90,9 +89,9 @@ public function reverseTransform($keys)
}

/**
* @param object $model
* @return mixed[]
*/
private function getIdentifierValues($model): array
private function getIdentifierValues(object $model): array
{
try {
return $this->modelManager->getIdentifierValues($model);
Expand Down
2 changes: 1 addition & 1 deletion src/Form/EventListener/MergeCollectionListener.php
Expand Up @@ -33,7 +33,7 @@ public function __construct(ModelManagerInterface $modelManager)
$this->modelManager = $modelManager;
}

public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
FormEvents::SUBMIT => ['onBind', 10],
Expand Down
13 changes: 7 additions & 6 deletions src/Form/Extension/ChoiceTypeExtension.php
Expand Up @@ -31,17 +31,18 @@ public function configureOptions(OptionsResolver $resolver): void
$resolver->setDefined($optionalOptions);
}

/**
* @param array<string, mixed> $options
*/
public function buildView(FormView $view, FormInterface $form, array $options): void
{
$view->vars['sortable'] = \array_key_exists('sortable', $options) && $options['sortable'];
}

public function getExtendedType()
{
return ChoiceType::class;
}

public static function getExtendedTypes()
/**
* @return string[]
*/
public static function getExtendedTypes(): array
{
return [ChoiceType::class];
}
Expand Down

0 comments on commit e3328bc

Please sign in to comment.