-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from ipalaus/feature/cursors
[Feature] Use cursors to navigate collections.
- Loading branch information
Showing
7 changed files
with
275 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the League\Fractal package. | ||
* | ||
* (c) Phil Sturgeon <email@philsturgeon.co.uk> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace League\Fractal\Cursor; | ||
|
||
/** | ||
* A generic cursor adapter. | ||
* | ||
* @author Isern Palaus <ipalaus@ipalaus.com> | ||
*/ | ||
class Cursor implements CursorInterface | ||
{ | ||
/** | ||
* Current cursor value. | ||
* | ||
* @var mixed | ||
*/ | ||
protected $current; | ||
|
||
/** | ||
* Next cursor value. | ||
* | ||
* @var mixed | ||
*/ | ||
protected $next; | ||
|
||
/** | ||
* Items being holded for the current cursor position. | ||
* | ||
* @var integer | ||
*/ | ||
protected $count; | ||
|
||
/** | ||
* Create a new Cursor instance. | ||
* | ||
* @param mixed $current | ||
* @param mixed $next | ||
* @param integer $count | ||
*/ | ||
public function __construct($current = null, $next = null, $count = null) | ||
{ | ||
$this->current = $current; | ||
$this->next = $next; | ||
$this->count = $count; | ||
} | ||
|
||
/** | ||
* Get the current cursor value. | ||
* | ||
* @return mixed | ||
*/ | ||
public function getCurrent() | ||
{ | ||
return $this->current; | ||
} | ||
|
||
/** | ||
* Set the current cursor value. | ||
* | ||
* @param mixed $current | ||
* @return League\Fractal\Cursor\Cursor | ||
*/ | ||
public function setCurrent($current) | ||
{ | ||
$this->current = $current; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Get the next cursor value. | ||
* | ||
* @return mixed | ||
*/ | ||
public function getNext() | ||
{ | ||
return $this->next; | ||
} | ||
|
||
/** | ||
* Set the next cursor value. | ||
* | ||
* @param mixed $next | ||
* @return League\Fractal\Cursor\Cursor | ||
*/ | ||
public function setNext($next) | ||
{ | ||
$this->next = $next; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Returns the total items in the current cursor. | ||
* | ||
* @return integer | ||
*/ | ||
public function getCount() | ||
{ | ||
return $this->count; | ||
} | ||
|
||
/** | ||
* Set the total items in the current cursor. | ||
* | ||
* @param integer $count | ||
* @return League\Fractal\Cursor\Cursor | ||
*/ | ||
public function setCount($count) | ||
{ | ||
$this->count = $count; | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the League\Fractal package. | ||
* | ||
* (c) Phil Sturgeon <email@philsturgeon.co.uk> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace League\Fractal\Cursor; | ||
|
||
/** | ||
* A common interface for cursors to use. | ||
* | ||
* @author Isern Palaus <ipalaus@ipalaus.com> | ||
*/ | ||
interface CursorInterface | ||
{ | ||
public function getCurrent(); | ||
public function getNext(); | ||
public function getCount(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace League\Fractal\Test\Cursor; | ||
|
||
use League\Fractal\Cursor\Cursor; | ||
use Mockery; | ||
|
||
class CursorAdapterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
public function testCursorAdapter() | ||
{ | ||
$cursor = new Cursor(100, 110, 10); | ||
|
||
$this->assertEquals($cursor->getCurrent(), 100); | ||
$this->assertEquals($cursor->getNext(), 110); | ||
$this->assertEquals($cursor->getCount(), 10); | ||
|
||
$cursor->setCurrent(110); | ||
$cursor->setNext(114); | ||
$cursor->setCount(4); | ||
|
||
$this->assertEquals($cursor->getCurrent(), 110); | ||
$this->assertEquals($cursor->getNext(), 114); | ||
$this->assertEquals($cursor->getCount(), 4); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.