PHP code example of ecourty / okf

1. Go to this page and download the library: Download ecourty/okf 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/ */

    

ecourty / okf example snippets


use Ecourty\Okf\Okf;

$okf = new Okf();

$bundle = $okf->read('/path/to/bundle');

foreach ($bundle->getConcepts() as $concept) {
    echo $concept->getId() . ': ' . $concept->getFrontmatter()->getTitle() . "\n";
}

$orders = $bundle->getConcept('tables/orders');
echo $orders->getBody();

$okf->write($bundle, '/path/to/output');

$concept = $bundle->getConcept('tables/orders'); // throws ConceptNotFoundException if missing

$frontmatter = $concept->getFrontmatter();
$frontmatter->getType();        // 'BigQuery Table'
$frontmatter->getTitle();       // 'Customer Orders'
$frontmatter->getTags();        // ['sales', 'orders', 'revenue']
$frontmatter->getExtra();       // ['owner_team' => 'data-platform', ...] — producer-defined keys, preserved as-is

$concept->getBody();            // the Markdown body, as a string

$rootIndex = $bundle->getIndex();          // index.md at the bundle root, or null if absent
$tablesIndex = $bundle->getIndex('tables'); // tables/index.md, or null if absent

$rootIndex?->getOkfVersion();              // the declared OKF version, if any (root index.md only)
$rootIndex?->getBody();                    // raw Markdown

$log = $bundle->getLog();                  // log.md at the bundle root, or null if absent
$log?->getBody();

foreach ($okf->iterateConcepts('/path/to/bundle') as $concept) {
    // ...
}

use Ecourty\Okf\Model\ConceptDocument;
use Ecourty\Okf\Model\Frontmatter;

$original = $bundle->getConcept('tables/orders');

$updated = new ConceptDocument(
    id: $original->getId(),
    path: $original->getPath(),
    frontmatter: new Frontmatter(
        type: $original->getFrontmatter()->getType(),
        title: $original->getFrontmatter()->getTitle(),
        tags: [...$original->getFrontmatter()->getTags(), 'reviewed'],
        extra: $original->getFrontmatter()->getExtra(),
    ),
    body: $original->getBody(),
);

$okf->getWriter()->writeConcept('/path/to/bundle', $updated);

use Ecourty\Okf\Model\ConceptDocument;
use Ecourty\Okf\Model\Frontmatter;

$concept = new ConceptDocument(
    id: 'metrics/daily-active-users',
    path: 'metrics/daily-active-users.md',
    frontmatter: new Frontmatter(
        type: 'Metric',
        title: 'Daily Active Users',
        description: 'Count of distinct users who performed at least one action in a day.',
        tags: ['growth', 'engagement'],
    ),
    body: "# Definition\n\nCounted from the `events` table, deduplicated by `user_id` per UTC day.\n",
);

$okf->getWriter()->writeConcept('/path/to/bundle', $concept);

use Ecourty\Okf\Validator\ConceptDocumentValidator;
use Ecourty\Okf\Validator\ValidationMode;

$validator = new ConceptDocumentValidator();

// Lenient (default): only `type` is ->getViolations($concept, ValidationMode::Strict);

// Or throw directly:
$validator->validate($concept, ValidationMode::Strict); // throws InvalidDocumentException

foreach ($bundle->getConcepts() as $concept) {
    $violations = $validator->getViolations($concept, ValidationMode::Lenient);

    foreach ($violations as $violation) {
        echo "{$concept->getId()}: {$violation}\n";
    }
}

$frontmatter = $concept->getFrontmatter();
$frontmatter->getExtra(); // every key not covered by the OKF spec, as given in the source YAML
$frontmatter->toArray();  // known + extra keys, ready to round-trip back to YAML

use Ecourty\Okf\Exception\BundleNotFoundException;
use Ecourty\Okf\Exception\ConceptNotFoundException;
use Ecourty\Okf\Exception\FrontmatterParseException;
use Ecourty\Okf\Exception\InvalidDocumentException;

try {
    $bundle = $okf->read('/path/to/bundle');
    $concept = $bundle->getConcept('tables/missing');
} catch (BundleNotFoundException $e) {
    echo $e->getPath();
} catch (ConceptNotFoundException $e) {
    echo $e->getConceptId();
} catch (FrontmatterParseException $e) {
    echo $e->getSource() . ': ' . $e->getReason(); // e.g. unterminated "---" block
} catch (InvalidDocumentException $e) {
    echo $e->getConceptId() . ': ' . implode(', ', $e->getViolations());
}

use Ecourty\Okf\Filesystem\FilesystemInterface;
use Ecourty\Okf\Okf;
use Ecourty\Okf\Parser\BundleParser;
use Ecourty\Okf\Writer\BundleWriter;

final class MyFilesystem implements FilesystemInterface
{
    // exists(), read(), write(), listFiles()
}

$filesystem = new MyFilesystem();

$okf = new Okf(
    parser: new BundleParser($filesystem),
    writer: new BundleWriter($filesystem),
);
bash
php examples/01-read-and-modify.php
php examples/02-validate-bundle.php
php examples/03-create-new-concept.php