File tree 3 files changed +59
-2
lines changed
3 files changed +59
-2
lines changed Original file line number Diff line number Diff line change 4
4
5
5
use Doctrine \DBAL \Connection ;
6
6
use Doctrine \DBAL \Types \Type ;
7
- use Enqueue \Util \JSON ;
8
7
use Interop \Queue \InvalidMessageException ;
9
8
use Interop \Queue \PsrConsumer ;
10
9
use Interop \Queue \PsrMessage ;
Original file line number Diff line number Diff line change 3
3
namespace Enqueue \Dbal ;
4
4
5
5
use Doctrine \DBAL \Types \Type ;
6
- use Enqueue \Util \JSON ;
7
6
use Interop \Queue \Exception ;
8
7
use Interop \Queue \InvalidDestinationException ;
9
8
use Interop \Queue \InvalidMessageException ;
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Enqueue \Dbal ;
4
+
5
+ class JSON
6
+ {
7
+ /**
8
+ * @param string $string
9
+ *
10
+ * @throws \InvalidArgumentException
11
+ *
12
+ * @return array
13
+ */
14
+ public static function decode ($ string )
15
+ {
16
+ if (!is_string ($ string )) {
17
+ throw new \InvalidArgumentException (sprintf (
18
+ 'Accept only string argument but got: "%s" ' ,
19
+ is_object ($ string ) ? get_class ($ string ) : gettype ($ string )
20
+ ));
21
+ }
22
+
23
+ // PHP7 fix - empty string and null cause syntax error
24
+ if (empty ($ string )) {
25
+ return null ;
26
+ }
27
+
28
+ $ decoded = json_decode ($ string , true );
29
+ if (JSON_ERROR_NONE !== json_last_error ()) {
30
+ throw new \InvalidArgumentException (sprintf (
31
+ 'The malformed json given. Error %s and message %s ' ,
32
+ json_last_error (),
33
+ json_last_error_msg ()
34
+ ));
35
+ }
36
+
37
+ return $ decoded ;
38
+ }
39
+
40
+ /**
41
+ * @param mixed $value
42
+ *
43
+ * @return string
44
+ */
45
+ public static function encode ($ value )
46
+ {
47
+ $ encoded = json_encode ($ value , JSON_UNESCAPED_UNICODE );
48
+
49
+ if (JSON_ERROR_NONE !== json_last_error ()) {
50
+ throw new \InvalidArgumentException (sprintf (
51
+ 'Could not encode value into json. Error %s and message %s ' ,
52
+ json_last_error (),
53
+ json_last_error_msg ()
54
+ ));
55
+ }
56
+
57
+ return $ encoded ;
58
+ }
59
+ }
You can’t perform that action at this time.
0 commit comments