Skip to content
Various range classes
Branch: master
Clone or download
Pull request Compare This branch is 2 commits behind develop.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
test
.gitattributes
.gitignore
.travis.yml
LICENSE
README.md
composer.json
phpunit.xml.dist

README.md

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();
        }
    }
}
You can’t perform that action at this time.