1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Base64 File package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\Base64FileBundle\Form\Extension; |
13
|
|
|
|
14
|
|
|
use Ivory\Base64FileBundle\Form\DataTransformer\Base64FileTransformer; |
15
|
|
|
use Symfony\Component\Form\AbstractType; |
16
|
|
|
use Symfony\Component\Form\AbstractTypeExtension; |
17
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FileType; |
18
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
19
|
|
|
use Symfony\Component\OptionsResolver\Options; |
20
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author GeLo <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class Base64FileExtension extends AbstractTypeExtension |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
private $base64; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param bool $base64 |
34
|
|
|
*/ |
35
|
117 |
|
public function __construct($base64) |
36
|
|
|
{ |
37
|
117 |
|
$this->base64 = $base64; |
38
|
117 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
90 |
|
public function buildForm(FormBuilderInterface $builder, array $options) |
44
|
|
|
{ |
45
|
90 |
|
if ($options['base64']) { |
46
|
81 |
|
$builder->addViewTransformer(new Base64FileTransformer()); |
47
|
63 |
|
} |
48
|
90 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
99 |
|
public function configureOptions(OptionsResolver $resolver) |
54
|
|
|
{ |
55
|
|
|
$resolver |
56
|
99 |
|
->setDefaults([ |
57
|
99 |
|
'base64' => $this->base64, |
58
|
99 |
|
'data_class' => function (Options $options, $value) { |
59
|
99 |
|
return !$options['base64'] ? $value : null; |
60
|
99 |
|
}, |
61
|
77 |
|
]) |
62
|
99 |
|
->setAllowedTypes('base64', 'bool'); |
63
|
99 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
99 |
|
public function getExtendedType() |
69
|
|
|
{ |
70
|
99 |
|
return method_exists(AbstractType::class, 'getBlockPrefix') ? FileType::class : 'file'; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|