<?php

namespace App\Traits;

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection; 

trait CustomPaginationTrait 
{
    /**
     * Custom Paginate Items 
     * 
     * @param array | object $items 
     * @param int            $currentPage 
     * @param int            $sizePerPage 
     * 
     * @return object 
     */
    public function customPaginate(
        array | object $items, 
        int $currentPage=1, 
        int $sizePerPage=20
    ) : object
    {
        $items      = $items instanceof Collection ? $items : Collection::make($items);
        $totalItems = $items->count(); 

        $items = $items->forPage($currentPage, $sizePerPage);
        $items = $items->diff([]);
        
        return new LengthAwarePaginator(
            items      : $items,
            total      : $totalItems,
            perPage    : $sizePerPage,
            currentPage: $currentPage
        );
    }
}