<?php
namespace SabaiApps\Framework\Model\Paginator;

use SabaiApps\Framework\Model\AbstractEntityRepository;
use SabaiApps\Framework\Criteria\AbstractCriteria;

/**
 * CriteriaPaginator class for paginating entities based on criteria.
 * Fetches and counts entities matching the given criteria.
 */
class CriteriaPaginator extends AbstractPaginator {

	protected $_criteria;

	/**
	 * Constructor.
	 *
	 * @param AbstractEntityRepository $repository Entity repository.
	 * @param AbstractCriteria         $criteria Criteria for filtering entities.
	 * @param int                      $perpage Number of entities per page.
	 * @param string                   $sort Sorting field.
	 * @param string                   $order Sorting order (ASC/DESC).
	 * @param int                      $limit Maximum number of entities to fetch.
	 */
	public function __construct( AbstractEntityRepository $repository, AbstractCriteria $criteria, $perpage, $sort, $order, $limit = 0 ) {
		parent::__construct( $repository, $perpage, $sort, $order, $limit );
		$this->_criteria = $criteria;
	}

	/**
	 * Gets the total count of elements for pagination.
	 *
	 * @return int Total count of elements.
	 */
	protected function _getElementCount() {
		return $this->_repository->countByCriteria( $this->_criteria );
	}

	/**
	 * Fetches elements for the current page.
	 *
	 * @param int $limit Number of elements to fetch.
	 * @param int $offset Offset for fetching elements.
	 * @return mixed Fetched elements.
	 */
	protected function _getElements( $limit, $offset ) {
		return $this->_repository->fetchByCriteria( $this->_criteria, $limit, $offset, $this->_sort, $this->_order );
	}
}
