|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\MediaBundle\Form\Type; |
|
15
|
|
|
|
|
16
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
17
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
18
|
|
|
use Psr\Log\NullLogger; |
|
19
|
|
|
use Sonata\MediaBundle\Form\DataTransformer\ProviderDataTransformer; |
|
20
|
|
|
use Sonata\MediaBundle\Provider\Pool; |
|
21
|
|
|
use Symfony\Component\Form\AbstractType; |
|
22
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
23
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @final since sonata-project/media-bundle 3.21.0 |
|
27
|
|
|
* |
|
28
|
|
|
* @author Hugo Briand <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class ApiMediaType extends AbstractType implements LoggerAwareInterface |
|
31
|
|
|
{ |
|
32
|
|
|
use LoggerAwareTrait; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var Pool |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $mediaPool; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $class; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string $class |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(Pool $mediaPool, $class) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->mediaPool = $mediaPool; |
|
50
|
|
|
$this->class = $class; |
|
51
|
|
|
$this->logger = new NullLogger(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options): void |
|
55
|
|
|
{ |
|
56
|
|
|
$dataTransformer = new ProviderDataTransformer($this->mediaPool, $this->class, [ |
|
57
|
|
|
'empty_on_new' => false, |
|
58
|
|
|
]); |
|
59
|
|
|
$dataTransformer->setLogger($this->logger); |
|
60
|
|
|
|
|
61
|
|
|
$builder->addModelTransformer($dataTransformer, true); |
|
62
|
|
|
|
|
63
|
|
|
$provider = $this->mediaPool->getProvider($options['provider_name']); |
|
64
|
|
|
$provider->buildMediaType($builder); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function configureOptions(OptionsResolver $resolver): void |
|
68
|
|
|
{ |
|
69
|
|
|
$resolver->setDefaults([ |
|
70
|
|
|
'provider_name' => 'sonata.media.provider.image', |
|
71
|
|
|
'context' => 'api', |
|
72
|
|
|
]); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getParent() |
|
76
|
|
|
{ |
|
77
|
|
|
return ApiDoctrineMediaType::class; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getBlockPrefix() |
|
81
|
|
|
{ |
|
82
|
|
|
return 'sonata_media_api_form_media'; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* NEXT_MAJOR: Remove this method. |
|
87
|
|
|
* |
|
88
|
|
|
* @deprecated since sonata-project/media-bundle 3.22, to be removed in version 4.0. |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getName() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->getBlockPrefix(); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.