PHP code example of harp-orm / range

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

    

harp-orm / range example snippets


$range = new Range(5, 10);

// Will return 5
echo $range->getMin();

// Will return 10
echo $range->getMax();

// It also implements ArrayAccess
echo $range[0]; // 5
echo $range[1]; // 10

$range[0] = 2;
$range[1] = 9;

// And you can convert it to a short string representation
// For example for storing in the DB
echo (string) $range; // 2|9
$newRange = Range::fromString('2|9');

// You can "add" ranges together
// This will add the min and the max values
$range = new Range(5, 10);
$range->add(new Range(3, 20));
echo $range; // 8|30

// You can get a human readable version with humanize method
$range = new Range(5, 10);
echo $range->humanize(); // 5 - 10

// This is also custumizable
$range = new Range(5, 10, '%s - / - %s');
echo $range->humanize(); // 5 - / - 10

// You can add a closure to further custumize this
$range = new Range(5, 10, function ($min, $max) {
    return $min.'..'.$max;
});
echo $range->humanize(); // 5..10

$range1 = new Range(5, 10);
$range2 = new Range(2, 8);
$range3 = new Range(9, 8);

// Sum adds all of the ranges together
$range = Range::sum([$range1, $range2, $range3], '%s - %s');
echo $range; // 16|26

// Get the maximum values for the first and second value
$range = Range::merge([$range1, $range2, $range3], '%s - %s');
echo $range; // 9|10

use Harp\Harp\AbstractModel;
use Harp\Range\DaysRangeTrait;

class TestModel extends AbstractModel
{
    use DaysRangeTrait;

    public static function initialize($config)
    {
        DaysRangeTrait::initialize($config);
    }
}

$testModel = new TestModel();

// Will get you a Range object
$testModel->getDays();

$testModel->setDays(new Range(5, 10));

// Will return "5|10"
echo $testModel->days;