Skip to content

Commit

Permalink
refactor!: Throw on JSON failures
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufkandemir committed Apr 3, 2022
1 parent 70fc06a commit a4deb87
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/MicrodataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ public function __construct(MicrodataDOMDocument $dom, callable $absoluteUriHand
/**
* Extracts and converts microdata to associative array.
*
* @throws \JsonException
*
* @return mixed[]
*/
public function toArray(): array
{
// Somewhat hacky way to convert deep objects
return json_decode(json_encode($this->extractMicrodata()), true);
return json_decode(json_encode($this->extractMicrodata(), \JSON_THROW_ON_ERROR), true, flags: \JSON_THROW_ON_ERROR);
}

/**
Expand All @@ -55,15 +57,15 @@ public function toObject(): stdClass
}

/**
* Extracts and converts microdata to json using \json_encode().
* Extracts and converts microdata to JSON using \json_encode().
*
* @return false|string
* @see json_encode() to description of parameters
*
* @see \json_encode() to description of parameters and return values
* @throws \JsonException
*/
public function toJSON(int $options = 0, int $depth = 512): bool|string
public function toJSON(int $options = 0, int $depth = 512): string
{
return json_encode($this->extractMicrodata(), $options, $depth);
return json_encode($this->extractMicrodata(), $options | \JSON_THROW_ON_ERROR, $depth);
}

protected function extractMicrodata(): stdClass
Expand Down

0 comments on commit a4deb87

Please sign in to comment.