Giter Site home page Giter Site logo

Comments (1)

red-crown avatar red-crown commented on April 26, 2024

For what it's worth, I've been using something like this as a wrapper.

<?php namespace PayPerClick\Market\Facebook\Data;

use FacebookAds\Cursor;

/**
 * Class ReportCursor
 *
 * @package PayPerClick\Market\Facebook\Data
 */
class ReportCursor implements \Iterator, \Countable, \ArrayAccess {

    /**
     * @type int
     */
    protected $position = 0;

    /**
     * @type Cursor[]
     */
    protected $cursors = [];

    /**
     * @param Cursor $cursor
     */
    public function __construct(Cursor $cursor) {
        $cursor->setUseImplicitFetch(false);
        $this->cursors[] = $cursor;
    }

    /**
     * @return Cursor
     */
    public function getCursor() {
        return $this->cursors[ $this->position ];
    }

    public function current() {
        return $this->getCursor()->current()->getData();
    }

    public function next() {
        $this->getCursor()->next();
        if ($this->getCursor()->key() === null) {
            $this->advanceCursors();
        }
    }

    protected function advanceCursors() {
        if ($this->hasCursor($this->position+1)) {
            $this->getCursor()->rewind();
            $this->position++;
        } else if ($this->hasNextPage()) {
            $this->fetchNext();
        }
    }

    /**
     * @return bool
     */
    protected function hasNextPage() {
        return $this->getNextPage() !== null;
    }

    /**
     * @return string|null
     */
    protected function getNextPage() {
        $content = $this->getCursor()->getLastResponse()->getContent();

        return isset($content['paging']['next']) ? $content['paging']['next'] : null;
    }

    /**
     * @param int $offset
     * @return bool
     */
    protected function hasCursor($offset) {
        return isset($this->cursors[ $offset ]);
    }

    protected function fetchNext() {
        parse_str(parse_url($this->getNextPage(), PHP_URL_QUERY), $previousParams);

        $objectPrototype = clone $this->getCursor()->offsetGet($this->getCursor()->getIndexRight());
        $request         = $this->getCursor()->getLastResponse()->getRequest()->createClone();

        $request->getQueryParams()->offsetSet('offset', $previousParams['offset']);

        $this->getCursor()->rewind();

        $this->position++;

        $this->cursors[ $this->position ] = new Cursor($request->execute(), $objectPrototype);
    }

    public function key() {
        return $this->getCursor()->key();
    }

    public function valid() {
        return $this->getCursor()->valid();
    }

    public function rewind() {
        $this->position = 0;
    }

    public function offsetExists($offset) {
        return $this->getCursor()->offsetExists($offset);
    }

    public function offsetGet($offset) {
        return $this->getCursor()->offsetGet($offset);
    }

    public function offsetSet($offset, $value) {
        $this->getCursor()->offsetSet($offset, $value);
    }

    public function offsetUnset($offset) {
        $this->getCursor()->offsetUnset($offset);
    }

    public function count() {
        return array_reduce($this->cursors, function ($a, $cursor) {
            return $a + $cursor->count();
        }, 0);
    }
}

from facebook-php-business-sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.