PHP code example of bfabio / publiccode-parser-php

1. Go to this page and download the library: Download bfabio/publiccode-parser-php 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/ */

    

bfabio / publiccode-parser-php example snippets


use Bfabio\PublicCodeParser\Parser;

$parser = new Parser();

if ($parser->isValid('/path/to/publiccode.yml')) {
    echo "publiccode.yml is valid!\n";
} else {
    echo "publiccode.yml is NOT valid!\n";
};

use Bfabio\PublicCodeParser\Parser;
use Bfabio\PublicCodeParser\Exception\ValidationException;
use Bfabio\PublicCodeParser\Exception\ParserException;

$parser = new Parser();

try {
    $publicCode = $parser->parseFile('/path/to/publiccode.yml');

    // // or parse from string
    // $yamlContent = file_get_contents('/path/to/publiccode.yml');
    // $publicCode = $parser->parse($yamlContent);

    echo "publiccode.yml file is valid!\n";

    // Access parsed data
    echo $publicCode->getName(); // Get software name
    echo $publicCode->getDescription('it'); // Get Italian description
    echo $publicCode->getDescription('en'); // Get English description

    foreach ($publicCode->getWarnings() as $warning) {
        echo "- " . $warning . "\n";
    }

    // Structured warnings 

$parser = new Parser();

$publicCode = $parser->parseFile('/path/to/publiccode.yml');

if ($publicCode instanceof PublicCodeV0) {
    $name = $publicCode->getName();
    $url = $publicCode->getUrl();
    $landingUrl = $publicCode->getLandingUrl();

    $descriptionIt = $publicCode->getDescription('it');
    $descriptionEn = $publicCode->getDescription('en');
    $allDescriptions = $publicCode->getAllDescriptions();

    $platforms = $publicCode->getPlatforms(); // ['web', 'android', 'ios', etc.]
}

/* In the future, when v1 will be released */
/*
else if ($publicCode instanceof PublicCodeV1) {
    ...
}
*/

use Bfabio\PublicCodeParser\Parser;
use Bfabio\PublicCodeParser\ParserConfig;

// Create parser with custom config
$options = new ParserConfig();

$options->setDisableNetwork(true); // Disable remote existance checks for URLs

$parser = new Parser($options);
bash
composer