1
+ <?php
2
+ /**
3
+ * MIT License
4
+ *
5
+ * Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ namespace doganoo \PHPUtil \Crypto ;
27
+
28
+ /**
29
+ * Class Base64
30
+ * @package doganoo\PHPUtil\Crypto
31
+ */
32
+ class Base64 {
33
+
34
+ /** @var null|mixed $data */
35
+ private $ data = null ;
36
+ /** @var null|array $mimeType */
37
+ private $ mimeType = null ;
38
+
39
+ /**
40
+ * Base64 constructor.
41
+ * @param null $data
42
+ */
43
+ public function __construct ($ data = null ) {
44
+ $ this ->setData ($ data );
45
+ }
46
+
47
+ /**
48
+ * @param $data
49
+ */
50
+ public function setData ($ data ): void {
51
+ $ this ->data = $ data ;
52
+ }
53
+
54
+ /**
55
+ * @return mixed|null
56
+ */
57
+ public function getData () {
58
+ return $ this ->data ;
59
+ }
60
+
61
+ /**
62
+ * @return string
63
+ */
64
+ public function encode (): string {
65
+ $ data = base64_encode ($ this ->getData ());
66
+ $ this ->setData ($ data );
67
+ return $ this ->getData ();
68
+ }
69
+
70
+ /**
71
+ * @param bool $strict
72
+ * @return mixed|null
73
+ */
74
+ public function decode (bool $ strict = true ){
75
+ $ data = base64_decode ($ this ->getData (), $ strict );
76
+ $ this ->setData ($ data );
77
+ return $ this ->getData ();
78
+ }
79
+
80
+ /**
81
+ * @param $data
82
+ * @return bool
83
+ */
84
+ public static function isBase64 ($ data ):bool {
85
+ if (null === $ data ) return false ;
86
+ if (!is_string ($ data )) return false ;
87
+
88
+ $ decoded = base64_decode ($ data );
89
+ $ encoded = base64_encode ($ decoded );
90
+
91
+ return $ data === $ encoded ;
92
+
93
+ }
94
+
95
+ /**
96
+ * @return array|null
97
+ */
98
+ public function getMimeType ():?array {
99
+ if (!Base64::isBase64 ($ this ->getData ())) return null ;
100
+
101
+ $ decoded = $ this ->decode ();
102
+ $ handle = finfo_open ();
103
+
104
+ $ mimeType = finfo_buffer (
105
+ $ handle
106
+ , $ decoded
107
+ , FILEINFO_MIME_TYPE
108
+ );
109
+
110
+ $ this ->mimeType = [$ mimeType ];
111
+
112
+ return $ this ->mimeType ;
113
+ }
114
+
115
+ }
0 commit comments