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\DataTransformer; |
13
|
|
|
|
14
|
|
|
use Ivory\Base64FileBundle\Model\Base64FileInterface; |
15
|
|
|
use Ivory\Base64FileBundle\Model\UploadedBase64File; |
16
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
17
|
|
|
use Symfony\Component\Form\Exception\TransformationFailedException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author GeLo <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Base64FileTransformer implements DataTransformerInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
135 |
|
public function transform($value) |
28
|
|
|
{ |
29
|
135 |
|
if ($value === null) { |
30
|
|
|
return [ |
31
|
105 |
|
'value' => null, |
32
|
98 |
|
'name' => null, |
33
|
98 |
|
'mimeType' => null, |
34
|
98 |
|
'size' => null, |
35
|
98 |
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
60 |
|
if (!$value instanceof Base64FileInterface) { |
39
|
15 |
|
throw new TransformationFailedException(sprintf( |
40
|
15 |
|
'Expected an "Ivory\Base64Bundle\Model\Base64FileInterface", got "%s".', |
41
|
15 |
|
$this->getVariableType($value) |
42
|
14 |
|
)); |
43
|
|
|
} |
44
|
|
|
|
45
|
45 |
|
$uploadedFile = $value instanceof UploadedBase64File; |
46
|
|
|
|
47
|
|
|
return [ |
48
|
45 |
|
'value' => $value->getData(true, false), |
49
|
45 |
|
'name' => $uploadedFile ? $value->getClientOriginalName() : null, |
50
|
45 |
|
'mimeType' => $uploadedFile ? $value->getClientMimeType() : $value->getMimeType(), |
51
|
45 |
|
'size' => $uploadedFile ? $value->getClientSize() : $value->getSize(), |
52
|
42 |
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
105 |
|
public function reverseTransform($value) |
59
|
|
|
{ |
60
|
105 |
|
if (empty($value)) { |
61
|
15 |
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
90 |
|
if (!is_array($value)) { |
65
|
15 |
|
throw new TransformationFailedException(sprintf( |
66
|
15 |
|
'Expected an array, got "%s".', |
67
|
15 |
|
$this->getVariableType($value) |
68
|
14 |
|
)); |
69
|
|
|
} |
70
|
|
|
|
71
|
75 |
|
if (!isset($value['name'])) { |
72
|
15 |
|
throw new TransformationFailedException('Missing base 64 file name.'); |
73
|
|
|
} |
74
|
|
|
|
75
|
60 |
|
if (!isset($value['value'])) { |
76
|
15 |
|
throw new TransformationFailedException('Missing base 64 file value.'); |
77
|
14 |
|
} |
78
|
|
|
|
79
|
|
|
try { |
80
|
45 |
|
return new UploadedBase64File( |
81
|
45 |
|
$value['value'], |
82
|
45 |
|
$value['name'], |
83
|
45 |
|
isset($value['mimeType']) ? $value['mimeType'] : null, |
84
|
45 |
|
isset($value['size']) ? $value['size'] : null |
85
|
42 |
|
); |
86
|
15 |
|
} catch (\Exception $e) { |
87
|
15 |
|
throw new TransformationFailedException($e->getMessage()); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param mixed $variable |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
30 |
|
private function getVariableType($variable) |
97
|
|
|
{ |
98
|
30 |
|
return is_object($variable) ? get_class($variable) : gettype($variable); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|