Skip to content

Commit

Permalink
robust
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck committed Oct 8, 2022
1 parent ce641eb commit 68c7fb1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/Core/Serialization/BaseSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ final public function serialize(Bom $bom, ?bool $prettyPrint = null): string
}

/**
* Normalize an BOM to the data structure that {@see realSerialize} can handle.
* Normalize a {@see Bom} to the data structure that {@see realSerialize} can handle.
*
* @throws Exception
*
Expand All @@ -94,13 +94,13 @@ final public function serialize(Bom $bom, ?bool $prettyPrint = null): string
abstract protected function realNormalize(Bom $bom);

/**
* Serialize a {@see realNormalize normalized} version of {@see Bom}.
* Serialize a {@see realNormalize normalized} version of a {@see Bom}.
*
* @throws Exception
*
* @psalm-param TNormalizedBom $normalizedBom
*
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
abstract protected function realSerialize($normalizedBom, ?bool $pretty): string;
abstract protected function realSerialize($normalizedBom, ?bool $prettyPrint): string;
}
27 changes: 15 additions & 12 deletions src/Core/Serialization/JsonSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,17 @@ public function __construct(
*/
protected function realNormalize(Bom $bom): array
{
return $this->normalizerFactory
$normalizedBom = $this->normalizerFactory
->makeForBom()
->normalize($bom);

/** @var string|null $schema */
$schema = self::SCHEMA[$this->normalizerFactory->getSpec()->getVersion()] ?? null;
if (null !== $schema) {
$normalizedBom['$schema'] = $schema;
}

return $normalizedBom;
}

/**
Expand All @@ -128,18 +136,13 @@ protected function realNormalize(Bom $bom): array
*
* @psalm-return non-empty-string
*/
protected function realSerialize($normalizedBom, ?bool $pretty): string
protected function realSerialize($normalizedBom, ?bool $prettyPrint): string
{
/** @var string|null $schema */
$schema = self::SCHEMA[$this->normalizerFactory->getSpec()->getVersion()] ?? null;
if (null !== $schema) {
$normalizedBom['$schema'] = $schema;
}

$jsonEncodeFlags = $this->jsonEncodeFlags;
if (true === $pretty) {
$jsonEncodeFlags |= \JSON_PRETTY_PRINT;
}
$jsonEncodeFlags = match ($prettyPrint) {
null => $this->jsonEncodeFlags,
true => $this->jsonEncodeFlags | \JSON_PRETTY_PRINT,
false => $this->jsonEncodeFlags & ~\JSON_PRETTY_PRINT,
};

$json = json_encode($normalizedBom, $jsonEncodeFlags);
\assert(false !== $json); // as option JSON_THROW_ON_ERROR is expected to be set
Expand Down
6 changes: 3 additions & 3 deletions src/Core/Serialization/XmlSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function realNormalize(Bom $bom): DOMElement
*
* @psalm-return non-empty-string
*/
protected function realSerialize($normalizedBom, ?bool $pretty): string
protected function realSerialize($normalizedBom, ?bool $prettyPrint): string
{
$document = new DOMDocument($this->xmlVersion, $this->xmlEncoding);
$document->appendChild(
Expand All @@ -83,8 +83,8 @@ protected function realSerialize($normalizedBom, ?bool $pretty): string
)
);

if (true === $pretty) {
$document->formatOutput = true;
if (null !== $prettyPrint) {
$document->formatOutput = $prettyPrint;
}

// option LIBXML_NOEMPTYTAG might lead to errors in consumers
Expand Down

0 comments on commit 68c7fb1

Please sign in to comment.