PaginatedCollection.php 2.76 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
<?php
namespace Braintree;

use Iterator;

/**
 * Braintree PaginatedCollection
 * PaginatedCollection is a container object for paginated data
 *
 * retrieves and pages through large collections of results
 *
 * example:
 * <code>
 * $result = MerchantAccount::all();
 *
 * foreach($result as $merchantAccount) {
 *   print_r($merchantAccount->status);
 * }
 * </code>
 *
 * @package    Braintree
 * @subpackage Utility
 */
class PaginatedCollection implements Iterator
{
    private $_pager;
    private $_pageSize;
    private $_currentPage;
    private $_index;
    private $_totalItems;
    private $_items;

    /**
     * set up the paginated collection
     *
     * expects an array of an object and method to call on it
     *
     * @param array $pager
     */
    public function  __construct($pager)
    {
        $this->_pager = $pager;
        $this->_pageSize = 0;
        $this->_currentPage = 0;
        $this->_totalItems = 0;
        $this->_index = 0;
    }

    /**
     * returns the current item when iterating with foreach
     */
    public function current()
    {
        return $this->_items[($this->_index % $this->_pageSize)];
    }

    public function key()
    {
        return null;
    }

    /**
     * advances to the next item in the collection when iterating with foreach
     */
    public function next()
    {
        ++$this->_index;
    }

    /**
     * rewinds the collection to the first item when iterating with foreach
     */
    public function rewind()
    {
        $this->_index = 0;
        $this->_currentPage = 0;
        $this->_pageSize = 0;
        $this->_totalItems = 0;
        $this->_items = [];
    }

    /**
     * returns whether the current item is valid when iterating with foreach
     */
    public function valid()
    {
        if ($this->_currentPage == 0 || $this->_index % $this->_pageSize == 0 && $this->_index < $this->_totalItems)
        {
            $this->_getNextPage();
        }

        return $this->_index < $this->_totalItems;
    }

    private function _getNextPage()
    {
        $this->_currentPage++;
        $object = $this->_pager['object'];
        $method = $this->_pager['method'];

        if (isset($this->_pager['query'])) {
            $query = $this->_pager['query'];
            $result = call_user_func(
                [$object, $method],
                $query,
                $this->_currentPage
            );
        } else {
            $result = call_user_func(
                [$object, $method],
                $this->_currentPage
            );
        }

        $this->_totalItems= $result->getTotalItems();
        $this->_pageSize = $result->getPageSize();
        $this->_items = $result->getCurrentPage();
    }
}
class_alias('Braintree\PaginatedCollection', 'Braintree_PaginatedCollection');