PHP code example of lambda-twelve / one-record-laravel

1. Go to this page and download the library: Download lambda-twelve/one-record-laravel 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/ */

    

lambda-twelve / one-record-laravel example snippets


use LambdaTwelve\OneRecord\Api\Permission;
use LambdaTwelve\OneRecord\Model\Builder\ObjectBuilder;
use LambdaTwelve\OneRecord\Rdf\Iri;
use LambdaTwelve\OneRecord\Server\DataHolder;
use LambdaTwelve\OneRecord\Server\GrantAccessPolicy;
use LambdaTwelve\OneRecord\Server\ServerConfig;
use LambdaTwelve\OneRecord\Server\Spi\AccessPolicy;
use LambdaTwelve\OneRecord\Vocabulary\Generated\Cargo;

$config = app(ServerConfig::class);
$holder = app(DataHolder::class);

$piece = ObjectBuilder::of(Cargo::Piece)
    ->set(Cargo::goodsDescription, 'Perishables')
    ->build($config->logisticsObjectIri('piece-1'));

$holder->create($piece);

// Let a partner read it: the default policy is the SDK's grant-based one, and
// with the database driver its grants live in one_record_grants.
$policy = app(AccessPolicy::class);
if ($policy instanceof GrantAccessPolicy) {
    $policy->allow(new Iri('https://partner.example/logistics-objects/partner'), $piece->iri, [Permission::GetLogisticsObject]);
}

use LambdaTwelve\OneRecord\Laravel\OneRecord;

OneRecord::routes(['middleware' => ['api'], 'domain' => '1r.example.com', 'name' => 'one-record.']);
OneRecord::tokenRoutes();   // the token endpoint and JWKS document, when enabled

'auth' => [
    'driver' => 'jwt',
    'audience' => env('ONE_RECORD_AUDIENCE'),       // optional aud check
    'issuers' => [
        'https://auth.partner.example' => ['keys' => [$pem]],               // static PEM(s), or ['kid' => $pem]
        'https://auth.other.example'   => ['jwks' => true],                 // {issuer}/.well-known/jwks.json
        'https://auth.third.example'   => ['jwks' => 'https://.../jwks'],   // explicit JWKS URL
    ],
],

use LambdaTwelve\OneRecord\Server\Event\LogisticsEventReceived;

Event::listen(LogisticsEventReceived::class, function (LogisticsEventReceived $event) {
    // $event->event is the SDK's LogisticsEvent, $event->postedBy the partner
});

use LambdaTwelve\OneRecord\Laravel\Notifications\DeliveryFailed;
use LambdaTwelve\OneRecord\Laravel\Notifications\DeliveryRejected;
use LambdaTwelve\OneRecord\Laravel\Notifications\NotificationDeliverer;
use LambdaTwelve\OneRecord\Laravel\Storage\Database\PendingNotification;

final class PartnerDeliverer implements NotificationDeliverer
{
    public function deliver(PendingNotification $pending): void
    {
        $endpoint = $pending->endpoint;                    // suggested from the recipient IRI, may be null
        $document = $pending->outbound->notification->toJsonLd();
        // POST $document to the partner with its token; throw DeliveryFailed to retry,
        // DeliveryRejected to give up. Built on the SDK's client, let its verdict decide:
        // DeliveryVerdict::of($e) === DeliveryVerdict::Retry ? DeliveryFailed : DeliveryRejected.
    }
}

$this->app->bind(NotificationDeliverer::class, PartnerDeliverer::class);
sh
php artisan vendor:publish --tag=one-record-config
php artisan vendor:publish --tag=one-record-migrations   # optional; they load from the package otherwise
php artisan migrate