Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdClass instead of AssocArray #4 #150

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -29,9 +29,6 @@
use CycloneDX\Core\Models\Component;
use CycloneDX\Core\Serialization\JSON\_BaseNormalizer;

/**
* @psalm-type Dependency = array{ref: string, dependsOn?: non-empty-list<string>}
*/
class DependenciesNormalizer extends _BaseNormalizer
{
use NullAssertionTrait;
Expand All @@ -40,9 +37,9 @@ 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>
* @psalm-return list<object>
*/
public function normalize(Bom $bom): array
{
Expand Down Expand Up @@ -73,17 +70,15 @@ public function normalize(Bom $bom): array
return $dependencies;
}

/**
* @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 +88,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 @@ -45,7 +45,7 @@ class ExternalReferenceNormalizer extends _BaseNormalizer
*
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function normalize(ExternalReference $externalReference): array
public function normalize(ExternalReference $externalReference): object
{
$url = $externalReference->getUrl();
if (false === IriFormats::iriReference($url)) {
Expand All @@ -62,7 +62,7 @@ public function normalize(ExternalReference $externalReference): array
}
}

return array_filter(
return (object) array_filter(
[
'type' => $type,
'url' => $url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,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 @@ -45,12 +45,9 @@ public function normalize(ExternalReferenceRepository $repo): array
$externalReferences = [];
foreach ($repo->getItems() as $externalReference) {
try {
$item = $normalizer->normalize($externalReference);
$externalReferences[] = $normalizer->normalize($externalReference);
} catch (DomainException|UnexpectedValueException) {
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 @@ -37,33 +37,33 @@ 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) $id
* @SuppressWarnings(PHPMD.StaticAccess)
*/
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()];
$url = $license->getUrl();

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
Loading