Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
leedavis81 committed Jul 16, 2013
1 parent 864915c commit 225db90
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 184 deletions.
159 changes: 0 additions & 159 deletions src/DrestCommon/Error/Response/ApiProblemJson.php

This file was deleted.

83 changes: 58 additions & 25 deletions src/DrestCommon/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@
* Drest result set
* @author Lee
*/
class ResultSet implements \Iterator
class ResultSet implements \Countable, \IteratorAggregate, \ArrayAccess
{
/**
* Current iteration position
* @var integer $position
*/
private $position = 0;

/**
* Data - immutable and injected on construction
* @var array $data
Expand All @@ -39,8 +33,6 @@ private function __construct(array $data, $keyName)
}
$this->data = $data;
$this->keyName = $keyName;

$this->position = 0;
}

/**
Expand All @@ -52,39 +44,80 @@ public function toArray()
return array($this->keyName => $this->data);
}

public function current()
/**
* Create an instance of a results set object
* @param array $data
* @param string $keyName
* @return ResultSet
*/
public static function create($data, $keyName)
{
return $this->data[$this->position];
return new self($data, $keyName);
}

public function key()

/**
* @return int
*/
public function count()
{
return $this->position;
return count($this->data);
}

public function next()
/**
* @return \ArrayIterator
*/
public function getIterator()
{
++$this->position;
return new \ArrayIterator($this->data);
}

public function rewind()
/**
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
{
$this->position = 0;
return isset($this->data[$offset]);
}

public function valid()
/**
* @param mixed $offset
* @return mixed
*/
public function offsetGet($offset)
{
return isset($this->data[$this->position]);
if (isset($this->data[$offset])) {
return $this->data[$offset];
}
return null;
}

/**
* Create an instance of a results set object
* @param array $data
* @param string $keyName
* @return ResultSet
* @param mixed $offset
* @param mixed $value
* @return bool
*/
public static function create($data, $keyName)
public function offsetSet($offset, $value)
{
return new self($data, $keyName);
if ( !isset($offset)) {
$this->data[] = $value;
return true;
}
return $this->data[$offset] = $value;
}

/**
* @param mixed $offset
* @return mixed
*/
public function offsetUnset($offset)
{
if (isset($this->data[$offset])) {
$removed = $this->data[$offset];
unset($this->data[$offset]);
return $removed;
}
return null;
}
}
57 changes: 57 additions & 0 deletions tests/DrestCommonTests/ResultSetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
namespace DrestTests;


use DrestCommon\ResultSet;
use DrestCommonTests\DrestCommonTestCase;


class ResultSetTest extends DrestCommonTestCase
{

public function testResultSetConstruction()
{
$resultSet = ResultSet::create(array('part1', 'part2', 'part3'), 'parts');
$this->assertInstanceOf('DrestCommon\ResultSet', $resultSet);

$refl = new \ReflectionClass('DrestCommon\ResultSet');
$this->assertTrue($refl->getConstructor()->isPrivate());
}

/**
* @expectedException Exception
*/
public function testResultSetThrowExceptionWithObjectKeyname()
{
$resultSet = ResultSet::create(array('part1', 'part2', 'part3'), new \StdClass);
}

/**
* @expectedException Exception
*/
public function testResultSetThrowExceptionWithArrayKeyname()
{
$resultSet = ResultSet::create(array('part1', 'part2', 'part3'), array(1, 2));
}

public function testResultSetIteration()
{
$partsArray = array('part1', 'part2', 'part3');
$resultSet = ResultSet::create($partsArray, 'parts');

$x = 0;
foreach ($resultSet as $part)
{
$this->assertEquals($partsArray[$x], $part);
$x++;
}

$rsIterator = $resultSet->getIterator();
$this->assertInstanceOf('ArrayIterator', $rsIterator);
reset($rsIterator);
$this->assertEquals($partsArray[0], current($rsIterator));
$this->assertEquals($partsArray[1], next($rsIterator));
}


}

0 comments on commit 225db90

Please sign in to comment.