PHP code example of joby / toolbox

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

    

joby / toolbox example snippets


use Joby\Toolbox\Strings\Encoding;

// encode a string or binary data
$encoded = Encoding::base64url_encode($your_data);

// decode it
$decoded = Encoding::base64_url_decode($encoded);

  $array = [1, 2, 3, 4, 5];
  $shifted = ArrayFunctions::shift_n($array, 2); // Returns [1, 2]
  // $array is now [3, 4, 5]
  

  $array = [1, 2, 3, 4, 5];
  $popped = ArrayFunctions::pop_n($array, 2); // Returns [5, 4]
  // $array is now [1, 2, 3]
  

  $array = [1, 2, 3, null, 5];
  $min = ArrayFunctions::min($array); // Returns null (nulls are low by default)
  $min = ArrayFunctions::min($array, true); // Returns 1 (nulls are high)
  $max = ArrayFunctions::max($array); // Returns 5 (nulls are low by default)
  $max = ArrayFunctions::max($array, true); // Returns null (nulls are high)
  

// Basic sorting
$data = [3, 1, 4, 1, 5, 9];
Sort::sort($data, fn ($a, $b) => $a <=> $b);

// Multi-criteria sorting (even numbers first, then by value)
$data = [3, 1, 4, 1, 5, 9];
Sort::sort($data, fn ($a, $b) => $a % 2 <=> $b % 2, fn ($a, $b) => $a <=> $b);

// Reverse sorting
Sort::sort($data, Sort::reverse(fn ($a, $b) => $a <=> $b));

// Sorting objects by method results
Sort::sort($objects, Sort::compareMethods('getNumber'));

// Sorting objects by property values
Sort::sort($objects, Sort::compareProperties('itemName'));

// Sorting arrays by key values
Sort::sort($arrayOfArrays, Sort::compareArrayValues('name'));

// Sorting by callback results (e.g., string length)
Sort::sort($strings, Sort::compareCallbackResults(strlen(...)));

// Create a sorter with multiple criteria
$sorter = new Sorter(
    fn ($a, $b) => $a->priority <=> $b->priority,
    fn ($a, $b) => $a->name <=> $b->name
);

// Sort multiple arrays with the same criteria
$sorter->sort($array1);
$sorter->sort($array2);

class MyClass implements Sortable {
    public function sortByValue(): string|int|float|bool {
        return $this->priority;
    }
}

// Example using IntegerRange implementation
$range1 = new IntegerRange(1, 5);
$range2 = new IntegerRange(3, 8);

// Intersection (AND)
$intersection = $range1->booleanAnd($range2); // Range from 3 to 5

// Union (OR)
$union = $range1->booleanOr($range2); // Collection with range from 1 to 8

// Exclusive OR
$xor = $range1->booleanXor($range2); // Collection with ranges 1-3 and 5-8

// Create a collection of ranges
$collection = RangeCollection::create($range1, $range2);

// Merge overlapping and adjacent ranges
$merged = $collection->mergeRanges();