PHP code example of alcidesrc / sequence

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

    

alcidesrc / sequence example snippets


$result = Sequence::run(FirstTask::class)
	->then(SecondTask::class)
	...
	->then(LastTask::class)
	->startWith('payload');

class InvokableIncrementCounter
{
    public function __invoke(array $payload): array
    {
        $payload['counter']++;
        return $payload;
    }
}

$result = Sequence::run(InvokableIncrementCounter::class)
    ->startWith(['counter' => 0]);

echo json_encode($result);
// {"counter":1}

class IncrementTask implements TaskInterface
{
    //...
    
    public function handle(mixed $payload = null): mixed
    {
        $payload['counter']++;
        return $payload;
    }
}

$result = Sequence::run(IncrementTask::class)
    ->startWith(['counter' => 0]);

echo json_encode($result);
// {"counter":1}

class IncrementCounter
{
    public function add(array $payload): array
    {
        $payload['counter']++;
        return $payload;
    }
}

$result = Sequence::run([IncrementCounter::class, 'add'])
    ->startWith(['counter' => 0]);

// OR

$result = Sequence::run([new IncrementCounter(), 'add'])
    ->startWith(['counter' => 0]);

echo json_encode($result);
// {"counter":1}

class IncrementCounter
{
    public static function add(array $payload): array
    {
        $payload['counter']++;
        return $payload;
    }
}

$result = Sequence::run([IncrementCounter::class, 'add'])
    ->startWith(['counter' => 0]);

echo json_encode($result);
// {"counter":1}

$closure = function (array $payload): array {
    $payload['counter']++;
    return $payload;
};

$result = Sequence::run($closure)
    ->startWith(['counter' => 0]);

echo json_encode($result);
// {"counter":1}

$closure = function (array $payload): array {
    $payload['counter']++;
    return $payload;
};

$sequence = Sequence::run($closure)->then($closure);

$result = Sequence::run($sequence)
    ->startWith(['counter' => 0]);

echo json_encode($result);
// {"counter":2}

$result = Sequence::run($closure)			// 1st execution => $counter is 1
	->then(InvokableIncrementCounter::class)	// 2nd execution => $counter is 2
	->then([IncrementCounter::class, 'increment'])	// 3rd execution => $counter is 3
	->then(IncrementTask::class)			// 4th execution => $counter is 4
	->startWith(['counter' => 0]);

echo json_encode($result);
// {"counter":4}