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\DataTransformer; |
15
|
|
|
|
16
|
|
|
use Psr\Log\LoggerAwareInterface; |
17
|
|
|
use Psr\Log\LoggerAwareTrait; |
18
|
|
|
use Psr\Log\NullLogger; |
19
|
|
|
use Sonata\MediaBundle\Model\MediaInterface; |
20
|
|
|
use Sonata\MediaBundle\Provider\Pool; |
21
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @final since sonata-project/media-bundle 3.21.0 |
25
|
|
|
*/ |
26
|
|
|
class ProviderDataTransformer implements DataTransformerInterface, LoggerAwareInterface |
27
|
|
|
{ |
28
|
|
|
use LoggerAwareTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Pool |
32
|
|
|
*/ |
33
|
|
|
protected $pool; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $options; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $class |
42
|
|
|
*/ |
43
|
|
|
public function __construct(Pool $pool, $class, array $options = []) |
44
|
|
|
{ |
45
|
|
|
$this->pool = $pool; |
46
|
|
|
$this->options = $this->getOptions($options); |
47
|
|
|
$this->class = $class; |
|
|
|
|
48
|
|
|
|
49
|
|
|
$this->logger = new NullLogger(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function transform($value) |
53
|
|
|
{ |
54
|
|
|
if (null === $value) { |
55
|
|
|
return new $this->class(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $value; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function reverseTransform($media) |
62
|
|
|
{ |
63
|
|
|
if (!$media instanceof MediaInterface) { |
64
|
|
|
return $media; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$binaryContent = $media->getBinaryContent(); |
68
|
|
|
|
69
|
|
|
// no binary |
70
|
|
|
if (empty($binaryContent)) { |
71
|
|
|
// and no media id |
72
|
|
|
if (null === $media->getId() && $this->options['empty_on_new']) { |
73
|
|
|
return; |
74
|
|
|
} elseif ($media->getId()) { |
75
|
|
|
return $media; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$media->setProviderStatus(MediaInterface::STATUS_PENDING); |
79
|
|
|
$media->setProviderReference(MediaInterface::MISSING_BINARY_REFERENCE); |
80
|
|
|
|
81
|
|
|
return $media; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// create a new media to avoid erasing other media or not ... |
85
|
|
|
$newMedia = $this->options['new_on_update'] ? new $this->class() : $media; |
86
|
|
|
|
87
|
|
|
$newMedia->setProviderName($media->getProviderName()); |
88
|
|
|
$newMedia->setContext($media->getContext()); |
89
|
|
|
$newMedia->setBinaryContent($binaryContent); |
90
|
|
|
|
91
|
|
|
if (!$newMedia->getProviderName() && $this->options['provider']) { |
92
|
|
|
$newMedia->setProviderName($this->options['provider']); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (!$newMedia->getContext() && $this->options['context']) { |
96
|
|
|
$newMedia->setContext($this->options['context']); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$provider = $this->pool->getProvider($newMedia->getProviderName()); |
100
|
|
|
|
101
|
|
|
try { |
102
|
|
|
$provider->transform($newMedia); |
103
|
|
|
} catch (\Throwable $e) { |
104
|
|
|
// #1107 We must never throw an exception here. |
105
|
|
|
// An exception here would prevent us to provide meaningful errors through the Form |
106
|
|
|
// Error message inspired from Monolog\ErrorHandler |
107
|
|
|
$this->logger->error( |
108
|
|
|
sprintf('Caught Exception %s: "%s" at %s line %s', \get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()), |
109
|
|
|
['exception' => $e] |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $newMedia; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Define the default options for the DataTransformer. |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
protected function getOptions(array $options) |
122
|
|
|
{ |
123
|
|
|
return array_merge([ |
124
|
|
|
'provider' => false, |
125
|
|
|
'context' => false, |
126
|
|
|
'empty_on_new' => true, |
127
|
|
|
'new_on_update' => true, |
128
|
|
|
], $options); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: