json string to associative array
This commit is contained in:
parent
518a72b422
commit
b61d90216e
@ -337,7 +337,7 @@ class Utils
|
||||
/**
|
||||
* jsonToArray
|
||||
*
|
||||
* @param stdClass|array $json
|
||||
* @param stdClass|array|string $json
|
||||
* @param int $depth
|
||||
* @return array
|
||||
*/
|
||||
@ -374,8 +374,15 @@ class Utils
|
||||
}
|
||||
}
|
||||
}
|
||||
} elseif (is_string($json)) {
|
||||
$json = json_decode($json, true);
|
||||
|
||||
if (JSON_ERROR_NONE !== json_last_error()) {
|
||||
throw new InvalidArgumentException('json_decode error: ' . json_last_error_msg());
|
||||
}
|
||||
return $json;
|
||||
} else {
|
||||
throw new InvalidArgumentException('jsonToArray json must be array or stdClass.');
|
||||
throw new InvalidArgumentException('The json param to jsonToArray must be array or stdClass or string.');
|
||||
}
|
||||
return $json;
|
||||
}
|
||||
|
@ -366,6 +366,10 @@ class UtilsTest extends TestCase
|
||||
$jsonArrayDepth2 = Utils::jsonToArray($jsonArrayDepth1, 2);
|
||||
|
||||
$this->assertEquals($jsonArrayDepth2, $jsonAssoc);
|
||||
|
||||
$jsonArray = Utils::jsonToArray($this->testJsonMethodString);
|
||||
|
||||
$this->assertEquals($jsonArray, $jsonAssoc);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user