Zend Framework 2 Number Range class mini proposal

I have been working on a little piece of code on the weekend lately and i thought it would be useful to have a numeric range similar to Groovy's range. Maybe there is such code already but quick google search did not give me anything useful so i hacked it together myself.

Later on I thought maybe it would be worth sharing it so here it is.

Numeric Range Basics

It is not really rocket science but it might be helpful to have numeric ranges that can expand and are aware of what is the min, max etc.

Here are some examples of Zend Simple Number Range class proposal:

        $range = new SimpleNumberRange(-70,101);
        //<-70..101>
        
        $range->reverse();
        //<101..-70>
        
        $range->isAscending();
        //false
        
        $range->contains(16);
        //true
        
        $range->getMin();
        //-70
        
        $range->getMax();
        // 101
        
        $range->count();
        // 172
        
        $range = $range->unionWith(new SimpleNumberRange(-5, 54));
        // <54..-5> as orientation is inherited from current instance (was reverse)

        $range = $range->unionWith(new SimpleNumberRange(400, -400));
        // <54..-5> as -5,54 is the union no matter how wide is the other range
        
        $range->expandLeft()->expandLeft();
        // <56..-5> expand works in ascending and descending order

        $range = new SimpleNumberRange(-2,5);
        foreach($range as $number){
            echo " ".$number;
        }
        // -2 -1 0 1 2 3 4 5

More complex number range expansion

The only tricky bit is that expand left and right methods can take a limit or array of limits. Any number passed as limit will cause expand to fail if it would cross the limit. So

        $r = new SimpleNumberRange(0, 5);
        $r->expandRight(2); //expands as expanding right does not cross 2 (we cross 5) 
        $r->expandRight(6); //does not expand as right boundary was 6 so cant cross limit
        echo $r."\n"; // 0..6

Pagination made simple with Zend Paginator and Number Range

I think that paginator should allow us to easily create pagination links based only on total items count, items per page and current page number.

I may be wrong but Zend Paginator needs an iterator or a data source to get page links generated.

With the number range you can easily create page links like this:

        $start = microtime(true);
            $range = new SimpleNumberRange(0,10000001);
            $adapter = new \Zend\Paginator\Adapter\Iterator($range);
            $pager = new \Zend\Paginator\Paginator($adapter);
            $pager->setItemCountPerPage(15);
            $pager->setCurrentPageNumber(44);
        $stop = microtime(true);
        echo "\n Time: ";
        echo $stop - $start;
        echo "\n Pages count: ";
        echo $pager->count();
        echo "\n Pagination page numbers: ";
        print_r( $pager->getPages()->pagesInRange );

As you can see Paginator will give you correct page numbers to be shown in the pagination links. If you use number range you wont be able to get the ids for the current page though. Paginator would think that ids are continuous.

In example above we create zend paginator instance and use iterator adapter. We pass number range as iterator instance so that paginator can figure out how many items are there in the collection.

Memory-wise we only have a range instance with left and right boundaries. It is not like range() function which would create a array and populate it with all the numbers (that could take a lot of memory).

Time-wise paginator does some stuff during it's initialization that takes time. I am not sure yet as did not have time to profile it but it takes 12ms to create the paginator instance. That seems a bit too long for me. Will try to get to the bottom of it later.

Get code of Simple Number Range

It is a initial code checkin with 100% code coverage but it may change if review process requires so. It is Zend Framework 2 compatible (PHP 5.3 only).

Code is available here on git repository: Simple Number Range class for Zend Framework 2.

I hope someone finds it useful.

art

Comments

Post new comment

Image CAPTCHA