Skip to content

Commit

Permalink
Updated: Error handler for StringObject data parameter
Browse files Browse the repository at this point in the history
Updated: Some documentations
  • Loading branch information
uuur86 committed Jan 5, 2024
1 parent 2408851 commit a5f8bb8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
9 changes: 5 additions & 4 deletions src/Data/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,10 @@ public function get(string $path)
}

/**
* Query data with given path
* Queries the data with the given path.
*
* @param string $path string path of the requested value - default null
*
* @return mixed
* @param string|null $path The path of the requested value. Default is null.
* @return mixed Returns the queried data or null if not found.
*/
public function query(?string $path = null)
{
Expand All @@ -125,10 +124,12 @@ public function query(?string $path = null)

$data = $this;

// Advances in the data structure based on the given key and next key.
while (false !== ($key = current($path_arr))) {
$next_key = next($path_arr);
$next_key = $next_key !== false ? (string) $next_key : false;

// Retrieves sub-datasets based on the '*' wildcard key.
if ($key === '*') {
return $data->getCols($next_key);
}
Expand Down
22 changes: 15 additions & 7 deletions src/StringObjects.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,23 @@ public function __construct(object $obj, array $options = [])
public static function instance($data, array $options = [])
{
if (is_string($data)) {
$data = json_decode($data);
}
$decodedData = json_decode($data);

if (! is_object($data)) {
try {
$data = (object) $data;
} catch (Exception) {
throw new Exception("Input data is not valid!\r\n" . print_r($data, true), 23);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception("JSON decoding error: " . json_last_error_msg(), 22);
}

$data = $decodedData;
} elseif (! is_array($data) && ! is_object($data)) {
throw new Exception("Input data is neither an object nor an array.", 24);
}

if (is_array($data)) {
$data = (object) $data;
}

if (!is_object($data)) {
throw new Exception("Input data is not a valid object!\r\n" . print_r($data, true), 23);
}

return new self($data, $options);
Expand Down

0 comments on commit a5f8bb8

Please sign in to comment.