forked from wallabag/wallabag
Add a form to create tagging rules
This commit is contained in:
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Wallabag\CoreBundle\Form\DataTransformer;
|
||||
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||
|
||||
class StringToListTransformer implements DataTransformerInterface
|
||||
{
|
||||
private $separator;
|
||||
|
||||
public function __construct($separator = ',')
|
||||
{
|
||||
$this->separator = $separator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a list to a string.
|
||||
*
|
||||
* @param array|null $list
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function transform($list)
|
||||
{
|
||||
if (null === $list) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return implode($this->separator, $list);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a string to a list.
|
||||
*
|
||||
* @param string $string
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public function reverseTransform($string)
|
||||
{
|
||||
if (!$string) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return array_filter(array_map('trim', explode($this->separator, $string)));
|
||||
}
|
||||
}
|
||||
38
src/Wallabag/CoreBundle/Form/Type/TaggingRuleType.php
Normal file
38
src/Wallabag/CoreBundle/Form/Type/TaggingRuleType.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Wallabag\CoreBundle\Form\Type;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
use Wallabag\CoreBundle\Form\DataTransformer\StringToListTransformer;
|
||||
|
||||
class TaggingRuleType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->add('rule', 'text', array('required' => true))
|
||||
->add('save', 'submit')
|
||||
;
|
||||
|
||||
$tagsField = $builder
|
||||
->create('tags', 'text')
|
||||
->addModelTransformer(new StringToListTransformer(','));
|
||||
|
||||
$builder->add($tagsField);
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver->setDefaults(array(
|
||||
'data_class' => 'Wallabag\CoreBundle\Entity\TaggingRule',
|
||||
));
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'tagging_rule';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user