Skip to content

Commit

Permalink
stdClass instead of AssocArray #4
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 Sep 28, 2022
1 parent c7b5362 commit cadf8d9
Show file tree
Hide file tree
Showing 27 changed files with 145 additions and 159 deletions.
12 changes: 4 additions & 8 deletions src/Core/Serialization/JSON/Normalizers/BomNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ class BomNormalizer extends _BaseNormalizer

private const BOM_FORMAT = 'CycloneDX';

public function normalize(Bom $bom): array
public function normalize(Bom $bom): object
{
$factory = $this->getNormalizerFactory();

return array_filter(
return (object) array_filter(
[
'bomFormat' => self::BOM_FORMAT,
'specVersion' => $factory->getSpec()->getVersion(),
Expand All @@ -55,19 +55,15 @@ public function normalize(Bom $bom): array
);
}

private function normalizeMetadata(Metadata $metadata): ?array
private function normalizeMetadata(Metadata $metadata): ?object
{
$factory = $this->getNormalizerFactory();

if (false === $factory->getSpec()->supportsMetadata()) {
return null;
}

$data = $factory->makeForMetadata()->normalize($metadata);

return empty($data)
? null
: $data;
return $factory->makeForMetadata()->normalize($metadata);
}

private function normalizeExternalReferences(Bom $bom): ?array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ComponentNormalizer extends _BaseNormalizer
/**
* @throws DomainException if component has unsupported type
*/
public function normalize(Component $component): array
public function normalize(Component $component): object
{
$spec = $this->getNormalizerFactory()->getSpec();

Expand All @@ -63,7 +63,7 @@ public function normalize(Component $component): array
? $component->getBomRef()->getValue()
: null;

return array_filter(
return (object) array_filter(
[
'bom-ref' => $bomRef,
'type' => $type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
class ComponentRepositoryNormalizer extends _BaseNormalizer
{
/**
* @psalm-return list<mixed>
* @return object[]
*
* @psalm-return list<object>
*/
public function normalize(ComponentRepository $repo): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
use CycloneDX\Core\Serialization\JSON\_BaseNormalizer;

/**
* @psalm-type Dependency = array{ref: string, dependsOn?: non-empty-list<string>}
* @psalm-type Dependency = object{ref: string, dependsOn?: non-empty-list<string>}
*/
class DependenciesNormalizer extends _BaseNormalizer
{
Expand All @@ -40,7 +40,7 @@ class DependenciesNormalizer extends _BaseNormalizer
* Only named {@see \CycloneDX\Core\Models\BomRef BomRefs} will be taken into account.
* Make sure to use the {@see \CycloneDX\Core\Serialization\BomRefDiscriminator} before calling.
*
* @return array[]
* @return object[]
*
* @psalm-return list<Dependency>
*/
Expand Down Expand Up @@ -76,14 +76,15 @@ public function normalize(Bom $bom): array
/**
* @psalm-return Dependency|null
*/
private function normalizeDependency(BomRef $componentRef, BomRef ...$dependencyRefs): ?array
private function normalizeDependency(BomRef $componentRef, BomRef ...$dependencyRefs): ?object
{
$componentRefValue = $componentRef->getValue();
if (null === $componentRefValue) {
return null;
}

$dep = ['ref' => $componentRefValue];
$dep = (object) [];
$dep->ref = $componentRefValue;

$deps = [];
foreach ($dependencyRefs as $dependencyRef) {
Expand All @@ -93,7 +94,7 @@ private function normalizeDependency(BomRef $componentRef, BomRef ...$dependency
}
}
if (!empty($deps)) {
$dep['dependsOn'] = $deps;
$dep->dependsOn = $deps;
}

return $dep;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ExternalReferenceNormalizer extends _BaseNormalizer
/**
* @throws DomainException when the type was not supported by the spec
*/
public function normalize(ExternalReference $externalReference): array
public function normalize(ExternalReference $externalReference): object
{
$spec = $this->getNormalizerFactory()->getSpec();
$type = $externalReference->getType();
Expand All @@ -52,7 +52,7 @@ public function normalize(ExternalReference $externalReference): array
}
}

return array_filter(
return (object) array_filter(
[
'type' => $type,
'url' => $externalReference->getUrl(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
class ExternalReferenceRepositoryNormalizer extends _BaseNormalizer
{
/**
* @return array[]
* @return object[]
*
* @psalm-return list<array>
* @psalm-return list<object>
*/
public function normalize(ExternalReferenceRepository $repo): array
{
Expand All @@ -43,12 +43,9 @@ public function normalize(ExternalReferenceRepository $repo): array
$externalReferences = [];
foreach ($repo->getItems() as $externalReference) {
try {
$item = $normalizer->normalize($externalReference);
$externalReferences[] = $normalizer->normalize($externalReference);
} catch (\DomainException) {
continue;
}
if (false === empty($item)) {
$externalReferences[] = $item;
/* pass */
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
*/
class HashDictionaryNormalizer extends _BaseNormalizer
{
/**
* @return object[]
*
* @psalm-return list<object>
*/
public function normalize(HashDictionary $repo): array
{
$hashes = [];
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Serialization/JSON/Normalizers/HashNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class HashNormalizer extends _BaseNormalizer
/**
* @throws DomainException
*/
public function normalize(string $algorithm, string $content): array
public function normalize(string $algorithm, string $content): object
{
$spec = $this->getNormalizerFactory()->getSpec();
if (false === $spec->isSupportedHashAlgorithm($algorithm)) {
Expand All @@ -44,7 +44,7 @@ public function normalize(string $algorithm, string $content): array
throw new DomainException("Invalid hash content: $content", 2);
}

return [
return (object) [
'alg' => $algorithm,
'content' => $content,
];
Expand Down
10 changes: 5 additions & 5 deletions src/Core/Serialization/JSON/Normalizers/LicenseNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,31 @@ class LicenseNormalizer extends _BaseNormalizer
{
use NullAssertionTrait;

public function normalize(LicenseExpression|DisjunctiveLicenseWithId|DisjunctiveLicenseWithName $license): array
public function normalize(LicenseExpression|DisjunctiveLicenseWithId|DisjunctiveLicenseWithName $license): object
{
return $license instanceof LicenseExpression
? $this->normalizeExpression($license)
: $this->normalizeDisjunctive($license);
}

private function normalizeExpression(LicenseExpression $license): array
private function normalizeExpression(LicenseExpression $license): object
{
// TODO: IMPLEMENTED IF NEEDED: may throw, if not supported by the spec
// $this->getNormalizerFactory()->getSpec()->supportsLicenseExpression()

return ['expression' => $license->getExpression()];
return (object) ['expression' => $license->getExpression()];
}

/**
* @SuppressWarnings(PHPMD.ShortVariable)
*/
private function normalizeDisjunctive(DisjunctiveLicenseWithId|DisjunctiveLicenseWithName $license): array
private function normalizeDisjunctive(DisjunctiveLicenseWithId|DisjunctiveLicenseWithName $license): object
{
[$id, $name] = $license instanceof DisjunctiveLicenseWithId
? [$license->getId(), null]
: [null, $license->getName()];

return ['license' => array_filter(
return (object) ['license' => (object) array_filter(
[
'id' => $id,
'name' => $name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
*/
class LicenseRepositoryNormalizer extends _BaseNormalizer
{
/**
* @return object[]
*
* @psalm-return list<object>
*/
public function normalize(LicenseRepository $repo): array
{
return array_map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class MetadataNormalizer extends _BaseNormalizer
{
use NullAssertionTrait;

public function normalize(Metadata $metadata): array
public function normalize(Metadata $metadata): object
{
return array_filter(
return (object) array_filter(
[
// timestamp
'tools' => $this->normalizeTools($metadata->getTools()),
Expand All @@ -58,7 +58,7 @@ private function normalizeTools(ToolRepository $tools): ?array
: $this->getNormalizerFactory()->makeForToolRepository()->normalize($tools);
}

private function normalizeComponent(?Component $component): ?array
private function normalizeComponent(?Component $component): ?object
{
if (null === $component) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Serialization/JSON/Normalizers/ToolNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class ToolNormalizer extends _BaseNormalizer
{
use NullAssertionTrait;

public function normalize(Tool $tool): array
public function normalize(Tool $tool): object
{
return array_filter(
return (object) array_filter(
[
'vendor' => $tool->getVendor(),
'name' => $tool->getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
class ToolRepositoryNormalizer extends _BaseNormalizer
{
/**
* @return array[]
* @return object[]
*
* @psalm-return list<array>
* @psalm-return list<object>
*/
public function normalize(ToolRepository $repo): array
{
Expand All @@ -43,12 +43,9 @@ public function normalize(ToolRepository $repo): array
$tools = [];
foreach ($repo->getItems() as $tool) {
try {
$item = $normalizer->normalize($tool);
$tools[] = $normalizer->normalize($tool);
} catch (\DomainException) {
continue;
}
if (false === empty($item)) {
$tools[] = $item;
/* pass */
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/Core/Serialization/JsonSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,26 @@ class JsonSerializer extends BaseSerializer
Version::v1dot4 => 'http://cyclonedx.org/schema/bom-1.4.schema.json',
];

private function getSchemaBase(): array
private function setSchemaBase(object $struct): void
{
$schema = self::SCHEMA[$this->getSpec()->getVersion()] ?? null;

return null === $schema
? [] // @codeCoverageIgnore
: ['$schema' => $schema];
if (null !== $schema) {
$struct->{'$schema'} = $schema;
}
}

/**
* @throws DomainException if something was not supported
*/
protected function normalize(Bom $bom): string
{
$schemaBase = $this->getSchemaBase();
$data = (new JSON\NormalizerFactory($this->getSpec()))
->makeForBom()
->normalize($bom);

$json = json_encode(array_merge($schemaBase, $data), self::NORMALIZE_OPTIONS);
$this->setSchemaBase($data);

$json = json_encode($data, self::NORMALIZE_OPTIONS);
\assert(false !== $json); // as option JSON_THROW_ON_ERROR is expected to be set
\assert('' !== $json);

Expand Down

0 comments on commit cadf8d9

Please sign in to comment.