Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bf9f57a

Browse files
authoredMay 2, 2018
Merge pull request #434 from php-enqueue/fix-dbal-json
remove enqueue core dependency
2 parents 023d23b + 55c3bd9 commit bf9f57a

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed
 

‎pkg/dbal/DbalConsumer.php

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Doctrine\DBAL\Connection;
66
use Doctrine\DBAL\Types\Type;
7-
use Enqueue\Util\JSON;
87
use Interop\Queue\InvalidMessageException;
98
use Interop\Queue\PsrConsumer;
109
use Interop\Queue\PsrMessage;

‎pkg/dbal/DbalProducer.php

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Enqueue\Dbal;
44

55
use Doctrine\DBAL\Types\Type;
6-
use Enqueue\Util\JSON;
76
use Interop\Queue\Exception;
87
use Interop\Queue\InvalidDestinationException;
98
use Interop\Queue\InvalidMessageException;

‎pkg/dbal/JSON.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
}

0 commit comments

Comments
 (0)
Please sign in to comment.