PHP code example of phillipsdata / priority-schedule

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

    

phillipsdata / priority-schedule example snippets


use PhillipsData\PrioritySchedule\RoundRobin;

// Create our buckets
$a = new stdClass();
$a->name = 'A';
$a->count = 0;

$b = new stdClass();
$b->name = 'B';
$b->count = 2;

$c = new stdClass();
$c->name = 'C';
$c->count = 4;

// Initialize the priority schedule with a custom comparator
$rr = new RoundRobin();
$rr->setCallback(function ($x, $y) {
    if ($x->count === $y->count) {
        return 0;
    }
    // we want low items first so they have higher (1) priority
    return $x->count < $y->count
        ? 1
        : -1;
});

// Add items to the schedule
$rr->insert($a);
$rr->insert($b);
$rr->insert($c);

// Fetch items
foreach ($rr as $item) {
    echo $item->name . " (" . ++$item->count . ")\n";

    if ($item->count < 5) {
        $rr->insert($item);
    }
}

use PhillipsData\PrioritySchedule\FirstAvailable;

// Create our buckets
$a = new stdClass();
$a->name = 'A';
$a->count = 0;

$b = new stdClass();
$b->name = 'B';
$b->count = 2;

$c = new stdClass();
$c->name = 'C';
$c->count = 4;

// Initialize the priority schedule with a custom filter
$fa = new FirstAvailable();
$fa->setCallback(function ($item) {
    return $item->count > 0;
});

// Add items to the schedule
$fa->insert($a);
$fa->insert($b);
$fa->insert($c);

foreach ($fa as $item) {
    echo $item->name . " (" . --$item->count . ")\n";

    if ($item->count > 0) {
        $fa->insert($item);
    }
}