<?php
namespace SabaiApps\Framework\User;

/**
 * IdentityPaginator class for paginating user identities.
 * Fetches and counts user identities using an identity fetcher.
 */
class IdentityPaginator extends \SabaiApps\Framework\Paginator\AbstractPaginator {

	protected $_identityFetcher, $_sort, $_order;

	/**
	 * Constructor.
	 *
	 * @param AbstractIdentityFetcher $identityFetcher Identity fetcher instance.
	 * @param int                     $perpage Number of items per page.
	 * @param string                  $sort Sorting field.
	 * @param string                  $order Sorting order (ASC/DESC).
	 * @param int                     $limit Maximum number of items to paginate (optional).
	 */
	public function __construct( AbstractIdentityFetcher $identityFetcher, $perpage, $sort, $order, $limit = 0 ) {
		parent::__construct( $perpage, $limit );
		$this->_identityFetcher = $identityFetcher;
		$this->_sort            = $sort;
		$this->_order           = $order;
	}

	/**
	 * Gets the total count of user identities.
	 *
	 * @return int
	 */
	protected function _getElementCount() {
		return $this->_identityFetcher->count();
	}

	/**
	 * Fetches user identities for the current page.
	 *
	 * @param int $limit Number of items to fetch.
	 * @param int $offset Offset for fetching items.
	 * @return \ArrayObject
	 */
	protected function _getElements( $limit, $offset ) {
		return $this->_identityFetcher->fetch( $limit, $offset, $this->_sort, $this->_order );
	}
}
