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