PHP code example of datafoodconsortium / connector

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

    

datafoodconsortium / connector example snippets


$connector->import("/path/to/measures.json");
$connector->import("/path/to/facets.json");
$connector->import("/path/to/productTypes.json");
$connector->import("/path/to/vocabulary.json");

// Example of a facet
$fruit = $connector->fetch("dfc-f:Fruit");

// Example of an measure
$kilogram = $connector->fetch("dfc-m:Kilogram");

// Example of a product type
$tomato = $connector->fetch("dfc-pt:RoundTomato");

$quantity = new QuantitativeValue( 
    connector: $connector, // You have to pass a reference to the connector.
    value: 1.2, 
    unit: $kilogram
);

$allergenCharacteristic = new AllergenCharacteristic( 
    connector: $connector, // You have to pass a reference to the connector.
    value: 1, 
    unit: $kilogram, 
    allergenDimension: $connector->fetch("dfc-m:Peanuts"); 
);

$nutrientCharacteristic = new NutrientCharacteristic( 
    connector: $connector, // You have to pass a reference to the connector.
    value: 10, 
    unit: $gram, 
    nutrientDimension: $connector->fetch("dfc-m:Calcium")
);

$physicalCharacteristic = new PhysicalCharacteristic({ 
    connector: $connector, // You have to pass a reference to the connector.
    value: 100, 
    unit: $gram, 
    physicalDimension: $connector->fetch("dfc-m:Weight") 
});

$catalogItem = new CatalogItem({ 
    connector: $connector, // You have to pass a reference to the connector.
    semanticId: "http://myplatform.com/catalogItem" 
});

$suppliedProduct = new SuppliedProduct({
    connector: onnector, // You have to pass a reference to the connector.
    semanticId: "http://myplatform.com/tomato",
    description: "Awesome tomato",
    productType: $connector->fetch("dfc-pt:RoundTomato"), 
    quantity: $quantity,
    totalTheoreticalStock: 2.23,
    alcoholPercentage: 0, 
    lifetime: "a week", 
    claims: [$connector->fetch("dfc-f:NoAddedSugar")], 
    usageOrStorageConditions: "free text", 
    allergenCharacteristics: [$allergenCharacteristic],
    nutrientCharacteristics: [$nutrientCharacteristic],
    physicalCharacteristics: [$physicalCharacteristic],
    geographicalOrigin: $connector->fetch("dfc-f:CentreValLoire"),
    catalogItems: [$catalogItem], 
    certifications: [$connector->fetch("dfc-f:OrganicAB"), $connector->fetch("dfc-f:OrganicEU")],
    natureOrigin: [$connector->fetch("dfc-f:PlantOrigin")],
    partOrigin: [$connector->fetch("dfc-f:Fruit")]
});

$suppliedProduct->getDescription();

$addresses = $person->getLocalizations();

// Set the quantity of the product
$suppliedProduct->setQuantity(new QuantitiveValue(connector: $connector, unit: $kilogram, value: 2.6));

// Add a new certification to the product
$suppliedProduct->addCertification($connector->fetch("dfc-f:AocFR"));

$connector->export([$suppliedProduct]));

$objects = $connector->import($jsonAsAString));

function getDefaultfetchFunction(string $semanticObjectId): string {
  $opts = array('http' => array('method' => "GET", 'header' => "Accept: application/ld+json"));
  $context = stream_context_create($opts);
  return file_get_contents($semanticObjectId, false, $context);
}

// Set the function that will fetch the referenced objects when importing data.
Connector::setFetchFunction(\Closure $fetch);

// Set the object used to create new instances.
Connector::setFactory(IFactory $factory);

$connector = new Connector();
$connector->import("./test/thesaurus/measures.json");
$connector->import("./test/thesaurus/vocabulary.json");

$quantity = new Quantity(
    connector: $connector,
    unit: $connector->fetch("dfc-m:Kilogram"),
    value: 1.0
);

$consumptionFlow = new PlannedConsumptionFlow(
    connector: $connector,
    semanticId: "http://example.org/consumptionFlow",
    quantity: $quantity
);

$productionFlow = new PlannedProductionFlow(
    connector: $connector,
    semanticId: "http://example.org/productionFlow",
    quantity: $quantity
);

$transformation = new PlannedTransformation(
    connector: $connector, 
    semanticId: "http://example.org/transformation",
    consumptionFlow: $consumptionFlow,
    productionFlow: $productionFlow,
    transformationType: $connector->fetch("dfc-v:combine")
);

echo $connector->export([$transformation, $consumptionFlow, $productionFlow]);