Skip to content

Commit

Permalink
Merge pull request #434 from php-enqueue/fix-dbal-json
Browse files Browse the repository at this point in the history
remove enqueue core dependency
  • Loading branch information
makasim authored May 2, 2018
2 parents cea4d84 + 8d5dbe3 commit eaf9267
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
1 change: 0 additions & 1 deletion pkg/dbal/DbalConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Types\Type;
use Enqueue\Util\JSON;
use Interop\Queue\InvalidMessageException;
use Interop\Queue\PsrConsumer;
use Interop\Queue\PsrMessage;
Expand Down
1 change: 0 additions & 1 deletion pkg/dbal/DbalProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Enqueue\Dbal;

use Doctrine\DBAL\Types\Type;
use Enqueue\Util\JSON;
use Interop\Queue\Exception;
use Interop\Queue\InvalidDestinationException;
use Interop\Queue\InvalidMessageException;
Expand Down
59 changes: 59 additions & 0 deletions pkg/dbal/JSON.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Enqueue\Dbal;

class JSON
{
/**
* @param string $string
*
* @throws \InvalidArgumentException
*
* @return array
*/
public static function decode($string)
{
if (!is_string($string)) {
throw new \InvalidArgumentException(sprintf(
'Accept only string argument but got: "%s"',
is_object($string) ? get_class($string) : gettype($string)
));
}

// PHP7 fix - empty string and null cause syntax error
if (empty($string)) {
return null;
}

$decoded = json_decode($string, true);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException(sprintf(
'The malformed json given. Error %s and message %s',
json_last_error(),
json_last_error_msg()
));
}

return $decoded;
}

/**
* @param mixed $value
*
* @return string
*/
public static function encode($value)
{
$encoded = json_encode($value, JSON_UNESCAPED_UNICODE);

if (JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException(sprintf(
'Could not encode value into json. Error %s and message %s',
json_last_error(),
json_last_error_msg()
));
}

return $encoded;
}
}

0 comments on commit eaf9267

Please sign in to comment.