PHP code example of vaened / php-delta-orchestrator

1. Go to this page and download the library: Download vaened/php-delta-orchestrator 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/ */

    

vaened / php-delta-orchestrator example snippets


// Patch + current state
$startDate = Field::from(
    // incoming patch vs current value
    patch  : $payload->startDate,
    current: $availability->startDate,
);

$endDate = Field::from(
    patch  : $payload->endDate,
    current: $availability->endDate,
);

$orchestrator = new Orchestrator();

$orchestrator->register(
    // this action runs only if it applies, its contract is satisfied,
    // and there is an effective delta
    Action::for([$startDate, $endDate])
        ->apply(function (Field $startDate, Field $endDate): void {
            // use $field->delta(), $field->value(), $field->current()
        }),
);

$result = $orchestrator->execute();

final readonly class UpdateAvailabilityCommand
{
    public function __construct(
        public DateTimeImmutablePatchValue $startDate,
        public DateTimeImmutablePatchValue $endDate,
    ) {}
}

$input = new PatchInput(
    input: $request->all(),
);

$startDate = $input->dateTimeImmutable('start_date');
$endDate   = $input->dateTimeImmutable('end_date');

$name = StringPatchValue::from(true, 'Juan');
$email = StringPatchValue::present('[email protected]');
$timezone = StringPatchValue::missing();

$startDate = Field::from(
    patch  : $payload->startDate,
    current: $availability->startDate,
);

$endDate = Field::from(
    patch  : $payload->endDate,
    current: $availability->endDate,
)->using(comparator: DateTimeComparator::create());

$name = Field::from(patch: $payload->name, current: $current->name)
             ->transform(static fn(string $value): string => strtolower(trim($value)))
             ->using(comparator: StrictComparator::create());

$orchestrator->register(
    Action::for([$startDate, $endDate])
        ->apply(function (Field $startDate, Field $endDate): void {
            // call to application/domain service
        })
        ->when(static fn(Field ...$fields) => any($fields))
        ->describe('Update availability period'),
);

$orchestrator->register(
    Action::for([$startDate->dDate): void {
            // call to application/domain service
        })
        ->describe('Update availability period')
        ->or(static fn(ActionFailure $failure) => new DomainException(
            message : 'The action contract was not satisfied.',
            previous: $failure,
        )),
);

fields: [
    $startDate->
]

Action::for([$startDate, $endDate])->apply(
    static function (Field $startDate, Field $endDate): void {
        // ...
    },
)->when(static fn(Field ...$fields) => all($fields));

Action::for([$startDate, $endDate])->apply(
    static function (Field $startDate, Field $endDate): void {
        // ...
    },
)->when(All::isPresent());

Action::for([$startDate, $endDate])->apply(
    static function (Field $startDate, Field $endDate): void {
        // ...
    },
)->when(Boolean::from($shouldUpdateAvailability));

Action::for([$startDate, $endDate])->apply(
    static function (Field $startDate, Field $endDate): void {
        // ...
    },
)->when(Boolean::resolve(
    static function(Field $startDate, Field $endDate): bool {
        // custom guard: skip this action entirely when an external policy forbids the update
    }
));

$result = $orchestrator->execute();

if ($result->hasEffects()) {
    // persist / publish events
}

try {
    $orchestrator->execute();
} catch (ActionBehaviorNotSatisfied $failure) {
    // inspect $failure->progressUntilFailure(), $failure->field(), etc.
    $failure->rethrow();
}

$start = $startDate->effective();

present($startDate)

use function Vaened\DeltaOrchestrator\Rules\all;
use function Vaened\DeltaOrchestrator\Rules\any;

all([$startDate, $endDate]);
any([$startDate, $endDate]);

all([
    $startDate,
    any([$endDate, $publishedAt]),
]);

$action = Action::for([$startDate, $endDate])->apply(
    static function (Field $startDate, Field $endDate): void {
        // ...
    },
)->when(All::isPresent());;

$quantity = Field::from(
    patch  : $payload->quantity,
    current: $current->quantity,
)->using(comparator: NumericComparator::create());

$startDate = Field::from(
    patch  : $payload->startDate,
    current: $current->startDate,
)->using(comparator: DateTimeComparator::create());

$value = Field::from(
    patch  : $payload->value,
    current: $current->value,
)->using(comparator: LooseComparator::create());

$settings = Field::from(
    patch  : $payload->settings,
    current: $current->settings,
)->using(comparator: ArrayComparator::create());

$settings = Field::from(
    patch  : $payload->settings,
    current: $current->settings,
)->using(comparator: ArrayComparator::create(LooseComparator::create()));

new IntPatchValue(true, '20')->value();
new BoolPatchValue(true, 'true')->value();
new DateTimeImmutablePatchValue(true, '2026-04-26 10:20:30')->value();
bash
composer