<?php


namespace Opentalent\OtCore\Domain\Repository;

/**
 * Represents an unserialized api response of 'PagedCollection' type
 *
 * @package Opentalent\OtCore\Domain\Repository
 */
class ApiPagedCollection
{
    private $totalItems;
    private $itemsPerPage;
    private $currentPage;
    private $members;

    /**
     * ApiResponse constructor.
     * @param int $totalItems
     * @param int $itemsPerPage
     * @param int $currentPage
     * @param array $members
     */
    public function __construct(
        int $totalItems,
        int $itemsPerPage,
        int $currentPage,
        array $members
    ) {
        $this->totalItems = $totalItems;
        $this->itemsPerPage = $itemsPerPage;
        $this->currentPage = $currentPage;
        $this->members = $members;
    }

    /**
     * @return int
     */
    public function getTotalItems() {
        return $this->totalItems;
    }

    /**
     * @return int
     */
    public function getItemsPerPage() {
        return $this->itemsPerPage;
    }

    /**
     * @return array
     */
    public function getMembers() {
        return $this->members;
    }

    /**
     * @return int
     */
    public function getCurrentPage() {
        return $this->currentPage;
    }

    /**
     * @return int
     */
    public function getLastPage() {
        if (!$this->getItemsPerPage() > 0) {
            return 1;
        }
        $nb = intdiv($this->getTotalItems(), $this->getItemsPerPage());
        if ($this->getTotalItems() % $this->getItemsPerPage() != 0) {
            $nb += 1;
        }
        return $nb;
    }

    /**
     * @return array
     */
    public function getPages() {
        return range(1, $this->getLastPage());
    }
}
