From 225db902e5476b79ce5a2d714488bde207736c4d Mon Sep 17 00:00:00 2001 From: Lee Davis Date: Tue, 16 Jul 2013 13:34:14 +0100 Subject: [PATCH] update --- .../Error/Response/ApiProblemJson.php | 159 ------------------ src/DrestCommon/ResultSet.php | 83 ++++++--- tests/DrestCommonTests/ResultSetTest.php | 57 +++++++ 3 files changed, 115 insertions(+), 184 deletions(-) delete mode 100644 src/DrestCommon/Error/Response/ApiProblemJson.php create mode 100644 tests/DrestCommonTests/ResultSetTest.php diff --git a/src/DrestCommon/Error/Response/ApiProblemJson.php b/src/DrestCommon/Error/Response/ApiProblemJson.php deleted file mode 100644 index 5472964..0000000 --- a/src/DrestCommon/Error/Response/ApiProblemJson.php +++ /dev/null @@ -1,159 +0,0 @@ -title = $message; - } - - /** - * @return string $describedBy - */ - public function getDescribedBy() - { - return $this->describedBy; - } - - /** - * @return string $title - */ - public function getTitle() - { - return $this->title; - } - - /** - * @return integer $httpStatus - */ - public function getHttpStatus() - { - return $this->httpStatus; - } - - /** - * @return string $detail - */ - public function getDetail() - { - return $this->detail; - } - - /** - * @return integer $supportId - */ - public function getSupportId() - { - return $this->supportId; - } - - /** - * @return array $more - */ - public function getMore() - { - return $this->more; - } - - /** - * @param string $describedBy - */ - public function setDescribedBy($describedBy) - { - $this->describedBy = $describedBy; - } - - /** - * @param string $title - */ - public function setTitle($title) - { - $this->title = $title; - } - - /** - * @param integer $httpStatus - */ - public function setHttpStatus($httpStatus) - { - $this->httpStatus = $httpStatus; - } - - /** - * @param string $detail - */ - public function setDetail($detail) - { - $this->detail = $detail; - } - - /** - * @param integer $supportId - */ - public function setSupportId($supportId) - { - $this->supportId = $supportId; - } - - /** - * Set more information - * @param array $more - */ - public function setMore(array $more) - { - $this->more = (array)$more; - } - - /** - * @see \DrestCommon\Error\Response.ResponseInterface::getContentType() - */ - public static function getContentType() - { - return 'application/api-problem+json'; - } - - /** - * Every error document you should be able to recreate from the generated string - * @param string $string - * @return Json $errorResponse - */ - public static function createFromString($string) - { - //$result = json_decode($string, true); - //$instance = new self(); - - // @todo: need to recurse $result to populate the instance (or at least the "more" variable) - - //return $instance; - } - - /** - * Get the response document representation of this object - * @return string $response - */ - public function render() - { - return json_encode( - array_filter(get_object_vars($this), function ($item) { - return !empty($item); - }) - ); - } -} \ No newline at end of file diff --git a/src/DrestCommon/ResultSet.php b/src/DrestCommon/ResultSet.php index 4e2608e..0003111 100644 --- a/src/DrestCommon/ResultSet.php +++ b/src/DrestCommon/ResultSet.php @@ -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 @@ -39,8 +33,6 @@ private function __construct(array $data, $keyName) } $this->data = $data; $this->keyName = $keyName; - - $this->position = 0; } /** @@ -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; } } \ No newline at end of file diff --git a/tests/DrestCommonTests/ResultSetTest.php b/tests/DrestCommonTests/ResultSetTest.php new file mode 100644 index 0000000..521f86a --- /dev/null +++ b/tests/DrestCommonTests/ResultSetTest.php @@ -0,0 +1,57 @@ +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)); + } + + +} \ No newline at end of file