PHP code example of fissible / verdict

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

    

fissible / verdict example snippets


refundOrder($orderId);

use Fissible\Verdict\Actions\ActionContext;
use Fissible\Verdict\Actions\ActionEnvelope;
use Fissible\Verdict\Actions\AuthorizedAction;
use Fissible\Verdict\Capabilities\Capability;
use Fissible\Verdict\Facades\Verdict;
use Fissible\Verdict\Targets\ExecutionTargetPolicy;
use Laravel\Ai\Tools\Request;

Verdict::capability(
    Capability::usingPolicyForContextTarget(
        name: 'orders.refund',
        ability: 'refund',
        // The resolver receives only application context — the proposal is not in
        // scope, so an injected argument cannot redirect which order is refunded.
        resolveTarget: fn (ActionContext $context): Order => Order::findOrFail(
            $context->metadata['order_id'],
        ),
    )
        ->executionTarget(ExecutionTargetPolicy::refresh(
            name: 'order-primary-key',
            identityUsing: fn (ActionEnvelope $envelope, Order $order): array => [
                'order_id' => $order->getKey(),
            ],
            refreshUsing: fn (ActionEnvelope $envelope, Order $order): Order => Order::findOrFail(
                $order->getKey(),
            ),
        ))
        ->executeUsing(function (AuthorizedAction $action): string {
            app(RefundService::class)->issue($action->target);

            return 'Refund issued.';
        }),
);

$tool = Verdict::bound(
    definition: new RefundOrder,
    capability: 'orders.refund',
    context: function (Request $request): ActionContext {
        // $request carries model-supplied arguments. Build context from
        // application state the model cannot influence. SupportSession stands in
        // for your own source of truth about this conversation — session state, a
        // conversation record, a database row — anything the model never writes.
        return new ActionContext(
            actor: auth()->user(),
            metadata: ['order_id' => app(SupportSession::class)->activeOrderId()],
        );
    },
);

resolveTarget: fn (ActionEnvelope $envelope): Order => $envelope->context->actor
    ->orders()
    ->findOrFail($envelope->proposal->arguments['order_id']),

namespace App\Capabilities\Orders;

use Fissible\Verdict\Capabilities\Capability;
use Fissible\Verdict\Contracts\DefinesCapability;

final class RefundCapability implements DefinesCapability
{
    public static function make(): Capability
    {
        return Capability::usingPolicy(
            name: 'orders.refund',
            ability: 'refund',
            resolveTarget: $resolveOrder,
        )
            ->executionTarget($currentOrder)
            ->

namespace Tests\Feature\Capabilities\Orders;

use Fissible\Verdict\Testing\CapabilitySecurityTestKit;
use Fissible\Verdict\VerdictManager;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;

final class RefundCapabilityTest extends TestCase
{
    #[Test]
    public function denies_capability_without_executing(): void
    {
        $this->markTestIncomplete('TODO: register RefundCapability::make(), provide a denied envelope, and assert policy observation plus no side effects.');

        CapabilitySecurityTestKit::for(app(VerdictManager::class), 'orders.refund')
            ->assertPolicyDenial($deniedEnvelope, $assertPolicyWasApplied, $assertNoSideEffects);
    }

    #[Test]
    public function uses_a_refreshed_capability_target(): void
    {
        $this->markTestIncomplete('TODO: provide proposal and refreshed target fixtures plus an executor side-effect assertion.');

        CapabilitySecurityTestKit::for(app(VerdictManager::class), 'orders.refund')
            ->assertRefreshedTargetSubstitution($permittedEnvelope, $assertRefreshedSideEffects);
    }
}