PHP code example of sbominator / transformatron

1. Go to this page and download the library: Download sbominator/transformatron library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sbominator / transformatron example snippets




use SBOMinator\Transformatron\Converter;
use SBOMinator\Transformatron\Exception\ConversionException;
use SBOMinator\Transformatron\Exception\ValidationException;

// Create a converter instance
$converter = new Converter();

// Convert SPDX to CycloneDX
try {
    $spdxJson = file_get_contents('path/to/spdx-sbom.json');
    $result = $converter->convertSpdxToCyclonedx($spdxJson);
    
    // Get the converted content
    $cyclonedxJson = $result->getContent();
    
    // Write to file
    file_put_contents('path/to/output-cyclonedx.json', $cyclonedxJson);
    
    // Check for any warnings
    if ($result->hasWarnings()) {
        echo "Conversion completed with warnings:\n";
        foreach ($result->getWarnings() as $warning) {
            echo "- {$warning}\n";
        }
    }
} catch (ValidationException $e) {
    echo "Validation error: " . $e->getMessage() . "\n";
    print_r($e->getValidationErrors());
} catch (ConversionException $e) {
    echo "Conversion error: " . $e->getMessage() . "\n";
    echo "Source format: " . $e->getSourceFormat() . "\n";
    echo "Target format: " . $e->getTargetFormat() . "\n";
}

// Convert CycloneDX to SPDX
try {
    $cyclonedxJson = file_get_contents('path/to/cyclonedx-sbom.json');
    $result = $converter->convertCyclonedxToSpdx($cyclonedxJson);
    
    // Get the converted content
    $spdxJson = $result->getContent();
    
    // Write to file
    file_put_contents('path/to/output-spdx.json', $spdxJson);
} catch (ValidationException | ConversionException $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

$json = file_get_contents('path/to/unknown-format-sbom.json');

// Detect the format
$sourceFormat = $converter->detectFormat($json);
if ($sourceFormat) {
    echo "Detected format: " . $sourceFormat . "\n";
}

// Convert to target format using auto-detection
try {
    $targetFormat = Converter::FORMAT_CYCLONEDX; // or Converter::FORMAT_SPDX
    $result = $converter->convert($json, $targetFormat);
    
    echo "Successfully converted to " . $result->getFormat() . "\n";
} catch (ValidationException | ConversionException $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

$result = $converter->convertSpdxToCyclonedx($spdxJson);

// Check if conversion was successful
if ($result->isSuccessful()) {
    echo "Conversion successful\n";
} else {
    echo "Conversion completed with errors\n";
}

// Get all warnings
foreach ($result->getWarnings() as $warning) {
    echo "Warning: {$warning}\n";
}

// Get all errors (including non-critical ones)
foreach ($result->getErrors() as $error) {
    echo "Error ({$error->getSeverity()}): {$error->getMessage()}\n";
}

// Get a summary of the conversion
$summary = $result->getSummary();
print_r($summary);

// Access the converted content as an array
$contentArray = $result->getContentAsArray();

use SBOMinator\Transformatron\Factory\ConverterFactory;

// Create a converter factory
$factory = new ConverterFactory();

// Get a specific converter
$spdxToCycloneDxConverter = $factory->createConverter(
    Converter::FORMAT_SPDX, 
    Converter::FORMAT_CYCLONEDX
);

// Or by conversion path
$cycloneDxToSpdxConverter = $factory->createConverterForPath('cyclonedx-to-spdx');

// Use the converter directly
$result = $spdxToCycloneDxConverter->convert($spdxJson);

// Create a converter instance
$converter = new Converter();

// Use the converter methods
$result = $converter->convert($json, Converter::FORMAT_SPDX);