whiteoctober / Pagerfanta

Pagination for PHP.
Other
1.59k stars 2 forks source link

Any easy way to output the paginator as json? #262

Closed zspine closed 5 years ago

zspine commented 5 years ago

Hi

Is there any helper method to output the paginator properties in array or json format? similar to Zend Paginator toJson method with JsonSerializable support? my current workaround:

    public function toArray(Pagerfanta $pagerfanta)
    {
        $items = (array) $pagerfanta->getCurrentPageResults();

        return [
            'items'             => $items,
            'count_results'     => $pagerfanta->getNbResults(),
            'current_page'      => $pagerfanta->getCurrentPage(),
            'number_of_pages'   => $pagerfanta->getNbPages(),
            'next'              => ($pagerfanta->hasNextPage()) ? $pagerfanta->getNextPage() : null,
            'prev'              => ($pagerfanta->hasPreviousPage()) ? $pagerfanta->getPreviousPage() : null,
            'paginate'          => $pagerfanta->haveToPaginate(),
        ];
    }
sampart commented 5 years ago

Hi @zspine, thanks for getting in touch. There's not a method which does what you describe. Could you tell us more about your use-case for this so that we can see whether it's something to consider adding into the project itself? Your workaround looks pretty good to me.

(You can also use iterator_to_array($pagerfanta->getCurrentPageResults()) if you'd rather do that than casting.)

zspine commented 5 years ago

Hi @sampart, Thanks a lot for the quick response :). I am using the WhiteOctoberPagerfantaBundle in a symfony project and front-end uses angularjs (SPA). So all the request and response handled through json.

Possible use cases

  1. API (XML/JSON)
  2. SPA (Single Page Application)

Thanks again for pointing out the iterator_to_array, much better than casting.

Cheers!

sampart commented 5 years ago

Hi @zspine, thanks for explaining your use-case. You're right - a method like this would help people using javascript frameworks. How would you feel about making a PR to incorporate the method you've created (and updating the README and tests to cover it)?

stof commented 5 years ago

anyone building an API probably relies on some serializer (because items also needs to be turned into array/json), and they could configure the handling of a Pagerfanta object there. I'm not sure the helper method used here would actually be useful for that case.

zspine commented 5 years ago

@sampart Hi, PR am more than happy to do it... as @stof suggested it may not be useful in API scenarios, so now I am not sure whether adding this to the main project is a good idea or not, maybe the actual use cases are very rare. Another option is we can just update the document with an example and avoid incorporating in to project. Any suggestions?

sampart commented 5 years ago

@zspine. I think @stof makes a good point; I agree a PR for a helper method isn't necessary. I'm not sure that we need a documentation update either - the details of the method people need will vary, and shouldn't be that complicated in any case.

Did you hit any particular difficulties or points of confusion when making your method? If so, we could think about some specific documentation that would have helped.

zspine commented 5 years ago

@sampart Sorry for the delayed reply... I agree with your decision.

Well I had few difficulties in the beginning then I got familiar with the library and had no issues. Again it's difficult to tell :) even if something is difficult or not user friendly it becomes normal over time until someone raises the issue again. I raised this issue because I am familiar with Zend and was expecting a similar functionality. So better to watch for demand before making any changes.

P.S. We started to use Pagefanta as the main library for pagination and we really enjoy it, keep up the good work!!!!!

Cheers

sampart commented 5 years ago

Thanks @zspine, glad you like it! Let's leave this one for now and, as you say, see whether related issues come up from elsewhere. Thanks again for your time here.