Fixed coding style.

This commit is contained in:
Jan Böhmer 2020-04-10 13:05:08 +02:00
parent a5e1f02d27
commit ae75e6844f
41 changed files with 147 additions and 163 deletions

View file

@ -106,7 +106,7 @@ class AttachmentFormType extends AbstractType
'required' => false,
'label' => 'attachment.edit.secure_file',
'mapped' => false,
'disabled' => !$this->security->isGranted('@parts_attachments.show_private'),
'disabled' => ! $this->security->isGranted('@parts_attachments.show_private'),
'attr' => [
'class' => 'form-control-sm',
],

View file

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
@ -20,7 +23,6 @@
namespace App\Form;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Form\AbstractTypeExtension;
@ -39,7 +41,6 @@ use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
* This prevents issues when the collection that is edited uses a OrderBy annotation and therefore the direction of the
* elements can change during requests.
* Must me enabled by setting reindex_enable to true in Type options.
* @package App\Form
*/
class CollectionTypeExtension extends AbstractTypeExtension
{
@ -55,26 +56,26 @@ class CollectionTypeExtension extends AbstractTypeExtension
return [CollectionType::class];
}
public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
/*$resolver->setDefault('error_mapping', function (Options $options) {
$options->
});*/
$resolver->setDefaults([
'reindex_enable' => false,
'reindex_prefix' => 'db_',
'reindex_path' => 'id',
]);
'reindex_enable' => false,
'reindex_prefix' => 'db_',
'reindex_path' => 'id',
]);
$resolver->setAllowedTypes('reindex_enable', 'bool');
$resolver->setAllowedTypes('reindex_prefix', 'string');
$resolver->setAllowedTypes('reindex_path', 'string');
}
public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options): void {
$data = $event->getData();
$config = $event->getForm()->getConfig();
//If enabled do a reindexing of the collection
@ -86,8 +87,8 @@ class CollectionTypeExtension extends AbstractTypeExtension
foreach ($data->toArray() as $key => $item) {
$id = $this->propertyAccess->getValue($item, $options['reindex_path']);
//If element has an ID then use it. otherwise use default key
$index = $id === null ? $key : $options['reindex_prefix'] . $id;
$error_mapping['[' . $key . ']'] = $index;
$index = null === $id ? $key : $options['reindex_prefix'].$id;
$error_mapping['['.$key.']'] = $index;
$reindexed_data->set($index, $item);
}
$event->setData($reindexed_data);
@ -103,9 +104,7 @@ class CollectionTypeExtension extends AbstractTypeExtension
/**
* Set the option of the form.
* This a bit hacky cause we access private properties....
* @param FormBuilder $builder
* @param string $option
* @param mixed $value
*
* @throws \ReflectionException
*/
public function setOption(FormBuilder $builder, string $option, $value): void
@ -119,4 +118,4 @@ class CollectionTypeExtension extends AbstractTypeExtension
$property->setValue($builder, $tmp);
$property->setAccessible(false);
}
}
}

View file

@ -45,9 +45,6 @@ namespace App\Form\Type;
use App\Entity\Attachments\Attachment;
use App\Entity\Attachments\AttachmentContainingDBElement;
use App\Entity\Contracts\HasMasterAttachmentInterface;
use Doctrine\ORM\EntityRepository;
use ReflectionClass;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
@ -62,36 +59,37 @@ class MasterPictureAttachmentType extends AbstractType
$resolver->setAllowedTypes('entity', HasMasterAttachmentInterface::class);
$resolver->setDefaults([
'filter' => 'picture',
'choice_translation_domain' => false,
'attr' => [
'class' => 'selectpicker',
],
'choice_attr' => function (Options $options) {
return function ($choice, $key, $value) use ($options) {
/** @var Attachment $choice */
$tmp = ['data-subtext' => $choice->getFilename() ?? 'URL'];
'filter' => 'picture',
'choice_translation_domain' => false,
'attr' => [
'class' => 'selectpicker',
],
'choice_attr' => function (Options $options) {
return function ($choice, $key, $value) use ($options) {
/** @var Attachment $choice */
$tmp = ['data-subtext' => $choice->getFilename() ?? 'URL'];
if ('picture' === $options['filter'] && ! $choice->isPicture()) {
$tmp += ['disabled' => 'disabled'];
} elseif ('3d_model' === $options['filter'] && ! $choice->is3DModel()) {
$tmp += ['disabled' => 'disabled'];
}
if ('picture' === $options['filter'] && ! $choice->isPicture()) {
$tmp += ['disabled' => 'disabled'];
} elseif ('3d_model' === $options['filter'] && ! $choice->is3DModel()) {
$tmp += ['disabled' => 'disabled'];
}
return $tmp;
};
},
'choice_label' => 'name',
'choice_loader' => function (Options $options) {
return new CallbackChoiceLoader(function () use ($options) {
$entity = $options['entity'];
if (!$entity instanceof AttachmentContainingDBElement) {
throw new \RuntimeException('$entity must have Attachments! (be of type AttachmentContainingDBElement)');
}
return $entity->getAttachments()->toArray();
});
}
]);
return $tmp;
};
},
'choice_label' => 'name',
'choice_loader' => function (Options $options) {
return new CallbackChoiceLoader(function () use ($options) {
$entity = $options['entity'];
if (! $entity instanceof AttachmentContainingDBElement) {
throw new \RuntimeException('$entity must have Attachments! (be of type AttachmentContainingDBElement)');
}
return $entity->getAttachments()->toArray();
});
},
]);
$resolver->setAllowedValues('filter', ['', 'picture', '3d_model']);
}

View file

@ -85,35 +85,35 @@ class StructuralEntityType extends AbstractType
function ($value) use ($options) {
return $this->transform($value, $options);
}, function ($value) use ($options) {
return $this->reverseTransform($value, $options);
}));
return $this->reverseTransform($value, $options);
}));
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired(['class']);
$resolver->setDefaults([
'show_fullpath_in_subtext' => true, //When this is enabled, the full path will be shown in subtext
'subentities_of' => null, //Only show entities with the given parent class
'disable_not_selectable' => false, //Disable entries with not selectable property
'choice_value' => 'id', //Use the element id as option value and for comparing items
'choice_loader' => function (Options $options) {
return new CallbackChoiceLoader(function () use ($options) {
return $this->getEntries($options);
});
},
'choice_label' => function (Options $options) {
return function ($choice, $key, $value) use ($options) {
return $this->generateChoiceLabels($choice, $key, $value, $options);
};
},
'choice_attr' => function (Options $options) {
return function ($choice, $key, $value) use ($options) {
return $this->generateChoiceAttr($choice, $key, $value, $options);
};
},
'choice_translation_domain' => false, //Don't translate the entity names
]);
'show_fullpath_in_subtext' => true, //When this is enabled, the full path will be shown in subtext
'subentities_of' => null, //Only show entities with the given parent class
'disable_not_selectable' => false, //Disable entries with not selectable property
'choice_value' => 'id', //Use the element id as option value and for comparing items
'choice_loader' => function (Options $options) {
return new CallbackChoiceLoader(function () use ($options) {
return $this->getEntries($options);
});
},
'choice_label' => function (Options $options) {
return function ($choice, $key, $value) use ($options) {
return $this->generateChoiceLabels($choice, $key, $value, $options);
};
},
'choice_attr' => function (Options $options) {
return function ($choice, $key, $value) use ($options) {
return $this->generateChoiceAttr($choice, $key, $value, $options);
};
},
'choice_translation_domain' => false, //Don't translate the entity names
]);
$resolver->setDefault('empty_message', null);