Skip to content

Commit

Permalink
add json helper and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anton committed Oct 23, 2014
1 parent b2a9220 commit d4ecf4f
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 99 deletions.
262 changes: 166 additions & 96 deletions src/Kachit/Helper/JsonHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,167 +12,122 @@ class JsonHelper {
/**
* @var bool
*/
protected $isPhp54 = false;
protected $isPhp54Support = false;

/**
* @var bool
*/
protected $isPhp55 = false;
protected $isPhp55Support = false;

/**
* @var array
*/
protected $optionsDecode = [
JSON_BIGINT_AS_STRING => false,
];
protected $optionsDecode = [];

/**
* @var bool
*/
protected $decodeAsArray = true;
protected $decodeAsArray = true;

/**
* @var int
*/
protected $decodeDepth = 512;
protected $decodeDepth = 512;

/**
* @var array
*/
protected $optionsEncode = [
JSON_HEX_TAG => false,
JSON_HEX_AMP => false,
JSON_HEX_APOS => false,
JSON_HEX_QUOT => false,
JSON_FORCE_OBJECT => false,
JSON_NUMERIC_CHECK => false,
JSON_BIGINT_AS_STRING => false,
JSON_PRETTY_PRINT => false,
JSON_UNESCAPED_SLASHES => false,
JSON_UNESCAPED_UNICODE => false,
];
protected $optionsEncode = [];

/**
* Init
*/
public function __construct() {
$this->isPhp54 = $this->phpVersionCompare('5.4.0');
$this->isPhp55 = $this->phpVersionCompare('5.5.0');
$this->initDecodeOptions();
$this->initEncodeOptions();
}
public function __construct() {
$this->isPhp54Support = $this->phpVersionCompare('5.4.0');
$this->isPhp55Support = $this->phpVersionCompare('5.5.0');
$this->optionsDecode = $this->getDecodeOptions();
$this->optionsEncode = $this->getEncodeOptions();
}

/**
* Decode json string to value
*
* @param $jsonString
* @param string $jsonString
* @return mixed
*/
public function decode($jsonString) {
return json_decode($jsonString, $this->decodeAsArray, $this->decodeDepth, $this->getDecodeOptions());
}
public function decode($jsonString) {
return json_decode($jsonString, $this->decodeAsArray, $this->decodeDepth, $this->generateDecodeOptions());
}

/**
* Encode value to json string
*
* @param $value
* @param mixed $value
* @return string
*/
public function encode($value) {
return json_encode($value, $this->getEncodeOptions());
}

/**
* Get decode options
*
* @return int
*/
protected function getDecodeOptions() {
$options = 0;
return $options;
}

/**
* Get encode options
*
* @return int
*/
protected function getEncodeOptions() {
$options = 0;
return $options;
}
public function encode($value) {
$value = $this->prepareValueForEncode($value);
return json_encode($value, $this->generateEncodeOptions());
}

/**
* initDecodeOptions
* SSet encode option JSON_HEX_TAG
*
* @return void
* @param bool $value
* @return $this
*/
protected function initDecodeOptions() {
$options = [
JSON_BIGINT_AS_STRING => false,
];
$this->optionsDecode = $options;
}
public function setEncodeOptionHexTag($value = true) {
return $this->setEncodeValue(JSON_HEX_TAG, $value);
}

/**
* initEncodeOptions
* SSet encode option JSON_HEX_AMP
*
* @return void
* @param bool $value
* @return $this
*/
protected function initEncodeOptions() {
$options = [
JSON_HEX_TAG => false,
JSON_HEX_AMP => false,
JSON_HEX_APOS => false,
JSON_HEX_QUOT => false,
JSON_FORCE_OBJECT => false,
JSON_NUMERIC_CHECK => false,
];
$this->optionsEncode = $options;
}
public function setEncodeOptionHexAmp($value = true) {
return $this->setEncodeValue(JSON_HEX_AMP, $value);
}

/**
* setNeedJsonHexTag
* Set encode option JSON_HEX_APOS
*
* @param bool $value
* @return $this
*/
public function setNeedJsonHexTag($value = true) {
return $this->setEncodeValue(JSON_HEX_TAG, $value);
}
public function setEncodeOptionHexApos($value = true) {
return $this->setEncodeValue(JSON_HEX_APOS, $value);
}

/**
* Set encode value
* Set encode option JSON_HEX_QUOT
*
* @param int $optionKey
* @param bool $value
* @return $this
*/
protected function setEncodeValue($optionKey, $value) {
$this->optionsEncode[$optionKey] = (bool)$value;
return $this;
}
public function setEncodeOptionHexQuot($value = true) {
return $this->setEncodeValue(JSON_HEX_QUOT, $value);
}

/**
* Set decode value
* Set encode option JSON_FORCE_OBJECT
*
* @param int $optionKey
* @param bool $value
* @return $this
*/
protected function setDecodeValue($optionKey, $value) {
$this->optionsDecode[$optionKey] = (bool)$value;
return $this;
public function setEncodeOptionForceObject($value = true) {
return $this->setEncodeValue(JSON_FORCE_OBJECT, $value);
}

/**
* Php version compare
* Set encode option JSON_NUMERIC_CHECK
*
* @param string $version
* @return bool
* @param bool $value
* @return $this
*/
protected function phpVersionCompare($version) {
return version_compare(PHP_VERSION, $version) >= 0;
public function setEncodeOptionNumericCheck($value = true) {
return $this->setEncodeValue(JSON_NUMERIC_CHECK, $value);
}

/**
Expand Down Expand Up @@ -205,5 +160,120 @@ public function setDecodeDepth($decodeDepth) {
$this->decodeDepth = (int)$decodeDepth;
return $this;
}
}
?>

/**
* Get decode options
*
* @return int
*/
protected function generateDecodeOptions() {
return $this->generateOptions($this->optionsDecode);
}

/**
* Generate options list
*
* @param array $optionsList
* @return int
*/
protected function generateOptions(array $optionsList) {
$options = 0;
foreach ($optionsList as $option => $state) {
if ($state) {
$options = $options | $option;
}
}
return $options;
}

/**
* Get encode options
*
* @return int
*/
protected function generateEncodeOptions() {
return $this->generateOptions($this->optionsEncode);
}

/**
* Get decode options
*
* @return array
*/
protected function getDecodeOptions() {
$options = [
JSON_BIGINT_AS_STRING => false,
];
return $options;
}

/**
* Get encode options
*
* @return array
*/
protected function getEncodeOptions() {
$options = [
JSON_HEX_TAG => false,
JSON_HEX_AMP => false,
JSON_HEX_APOS => false,
JSON_HEX_QUOT => false,
JSON_FORCE_OBJECT => false,
JSON_NUMERIC_CHECK => false,
];
if ($this->isPhp54Support) {
$options[JSON_BIGINT_AS_STRING] = false;
$options[JSON_PRETTY_PRINT] = true;
$options[JSON_UNESCAPED_SLASHES] = true;
$options[JSON_UNESCAPED_UNICODE] = true;
}
return $options;
}

/**
* Set encode value
*
* @param int $optionKey
* @param bool $value
* @return $this
*/
protected function setEncodeValue($optionKey, $value) {
$this->optionsEncode[$optionKey] = (bool)$value;
return $this;
}

/**
* Set decode value
*
* @param int $optionKey
* @param bool $value
* @return $this
*/
protected function setDecodeValue($optionKey, $value) {
$this->optionsDecode[$optionKey] = (bool)$value;
return $this;
}

/**
* Php version compare
*
* @param string $version
* @return bool
*/
protected function phpVersionCompare($version) {
return version_compare(PHP_VERSION, $version) >= 0;
}

/**
* Prepare value for encode
*
* @param mixed $value
* @return array
*/
protected function prepareValueForEncode($value) {
if (!is_array($value) || !is_object($value)) {
$value = (array)$value;
}
return $value;
}
}

0 comments on commit d4ecf4f

Please sign in to comment.