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\AbstractTypeExtension; |
16
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
17
|
|
|
use Symfony\Component\OptionsResolver\Options; |
18
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
19
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolverInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author GeLo <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class Base64FileExtension extends AbstractTypeExtension |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
private $base64; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param bool $base64 |
33
|
|
|
*/ |
34
|
195 |
|
public function __construct($base64) |
35
|
|
|
{ |
36
|
195 |
|
$this->base64 = $base64; |
37
|
195 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
150 |
|
public function buildForm(FormBuilderInterface $builder, array $options) |
43
|
|
|
{ |
44
|
150 |
|
if ($options['base64']) { |
45
|
135 |
|
$builder->addViewTransformer(new Base64FileTransformer()); |
46
|
126 |
|
} |
47
|
150 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
143 |
|
public function setDefaultOptions(OptionsResolverInterface $resolver) |
53
|
|
|
{ |
54
|
143 |
|
$this->configureOptions($resolver); |
55
|
143 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
165 |
|
public function configureOptions(OptionsResolver $resolver) |
61
|
|
|
{ |
62
|
165 |
|
$resolver->setDefaults([ |
63
|
165 |
|
'base64' => $this->base64, |
64
|
165 |
|
'data_class' => function (Options $options, $value) { |
65
|
165 |
|
return !$options['base64'] ? $value : null; |
66
|
165 |
|
}, |
67
|
154 |
|
]); |
68
|
|
|
|
69
|
165 |
|
if (method_exists($resolver, 'setDefault')) { |
70
|
154 |
|
$resolver->setAllowedTypes('base64', 'bool'); |
71
|
143 |
|
} else { |
72
|
11 |
|
$resolver->setAllowedTypes(array('base64' => 'bool')); |
|
|
|
|
73
|
|
|
} |
74
|
165 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
165 |
|
public function getExtendedType() |
80
|
|
|
{ |
81
|
165 |
|
if (method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) { |
82
|
33 |
|
return 'Symfony\Component\Form\Extension\Core\Type\FileType'; |
83
|
|
|
} |
84
|
|
|
|
85
|
132 |
|
return 'file'; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
This check looks for function calls that miss required arguments.