2017 © Pedro Peláez
 

library range

An object representing 2 integer values

image

harp-orm/range

An object representing 2 integer values

  • Thursday, August 28, 2014
  • by hkdobrev
  • Repository
  • 2 Watchers
  • 1 Stars
  • 35 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Range

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version, (*1)

An object representing 2 integer values, (*2)

Usage

This is a object representing two values - min and max, that can be stored string (as "first_value|second_value"). You can set / retrieve it as string or as an array like this:, (*3)

$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

Aggregate Methods

There are several methods for working with multiple ranges:, (*4)

$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

Using with models

There is a trait that adds methods for working with ranges to a model. To persist the data, it adds the "days" property to the model., (*5)

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;

License

Copyright (c) 2014, Clippings Ltd. Developed by Ivan Kerin, (*6)

Under BSD-3-Clause license, read LICENSE file., (*7)

The Versions

28/08 2014

dev-master

9999999-dev

An object representing 2 integer values

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

28/08 2014

0.2.1

0.2.1.0

An object representing 2 integer values

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

23/07 2014

0.2.0

0.2.0.0

An object representing 2 integer values

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

17/07 2014

0.1.1

0.1.1.0

An object representing 2 integer values

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0
  • harp-orm/validator ~0.2.1

 

The Development Requires

17/07 2014

0.1.0

0.1.0.0

An object representing 2 integer values

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires