Skip to content

texdc/range

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

range

Oh, give me a home where the buffalo roam
Where the deer and the antelope play
Where seldom is heard a discouraging word
And the skies are not cloudy all day

-- Home on the Range

The inspiration for this library came from a discussion by Martin Fowler.

Software License Build Status Coverage Status

install

Composer:

composer require texdc/range @stable

examples

Range

Ranges provide simple validation and iteration.

use texdc\range\DateRange;

$dateRange = new DateRange(new DateTime, new DateTime('+1 month'));

assert($dateRange->includes(new DateTime('+3 days')));

echo $dateRange->getSpan()->days;

foreach ($dateRange as $day) {
    echo $day->format('l, F jS, Y');
}

Ranges can also be compared against each other.

use texdc\range\IntegerRange;

$range1 = new IntegerRange(1, 5);
$range2 = new IntegerRange(8, 3);
$range3 = new IntegerRange(5, 8);

assert($range1->overlaps($range2));
assert($range2->isContraryTo($range1));
assert($range3->abuts($range2));
assert($range1->begins(IntegerRange::merge($range1, $range3)));
assert($range3->ends(IntegerRange::combine([$range1, $range3])));

See the tests for more comparisons!

Enablement

Enablements leverage a range for more robust alternatives to simple boolean flags.

use texdc\range\DateEnablement;

class DatedBannerAd extends AbstractBannerAd
{
    /**
     * @var DateEnablement
     */
    private $enablement;
    
    // ...
    
    public function render()
    {
        if ($this->enablement->isEnabled()) {
            return parent::render();
        }
    }
}