-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #434 from php-enqueue/fix-dbal-json
remove enqueue core dependency
- Loading branch information
Showing
3 changed files
with
59 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |