Skip to content

Commit

Permalink
Collection Updates
Browse files Browse the repository at this point in the history
Added some functionality for messing with Collections. Corrected
building of Model when requested.
  • Loading branch information
MichaelJ2324 committed Jun 10, 2017
1 parent ddce22c commit 4c84a91
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 16 deletions.
64 changes: 53 additions & 11 deletions src/Endpoint/Abstracts/AbstractCollectionEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class AbstractCollectionEndpoint extends AbstractSmartEndpoint implemen
protected $collection = array();

/**
* The Class Name
* The Class Name of the ModelEndpoint
* @var string
*/
protected $model;
Expand Down Expand Up @@ -133,18 +133,48 @@ public function get($id) {
$data = NULL;
if ($this->offsetExists($id)){
$data = $this->collection[$id];
$Model = $this->buildModel();
if ($Model !== NULL && is_array($data)){
$Model = $this->buildModel();
foreach($data as $key => $value){
$Model->set($key,$value);
}
$Model = $this->buildModel($data);
if ($Model !== NULL){
$data = $Model;
}
}
return $data;
}

/**
* Get a model based on numerical index
* @param int $index
* @return array|ModelInterface
*/
public function at($index){
$return = NULL;
$index = intval($index);
$data = $this->asArray();
reset($data);
if ($index < 0){
$index += $this->length();
}
$c = 1;
while ($c <= $index){
next($data);
$c++;
}
$return = current($data);
$Model = $this->buildModel($return);
if ($Model !== NULL){
$return = $Model;
}
return $return;
}

/**
* Return the current collection count
* @return int
*/
public function length(){
return count($this->collection);
}

/**
* @inheritdoc
* @throws UnknownEndpoint
Expand Down Expand Up @@ -215,12 +245,24 @@ protected function updateCollection(){
}

/**
* @return ModelInterface
* Build the ModelEndpoint
* @param array $data
* @return AbstractModelEndpoint
*/
protected function buildModel(){
protected function buildModel(array $data = array()){
$Model = NULL;
if (isset($this->model)){
return new $this->model();
$Model = new $this->model();
$Model->setBaseUrl($this->getBaseUrl());
if ($this->getAuth() !== NULL) {
$Model->setAuth($this->getAuth());
}
if (!empty($data)){
foreach($data as $key => $value){
$Model->set($key,$value);
}
}
}
return NULL;
return $Model;
}
}
6 changes: 1 addition & 5 deletions src/Endpoint/Abstracts/AbstractModelEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
namespace MRussell\REST\Endpoint\Abstracts;

use MRussell\Http\Request\Curl;
use MRussell\Http\Request\RequestInterface;
use MRussell\Http\Response\ResponseInterface;
use MRussell\REST\Endpoint\Data\AbstractEndpointData;
use MRussell\REST\Endpoint\Data\DataInterface;
use MRussell\REST\Endpoint\Interfaces\ModelInterface;
use MRussell\REST\Endpoint\JSON\ModelEndpoint;
use MRussell\REST\Exception\Endpoint\EndpointException;
use MRussell\REST\Exception\Endpoint\MissingModelId;
use MRussell\REST\Exception\Endpoint\UnknownModelAction;

Expand Down Expand Up @@ -86,8 +83,7 @@ public function __construct(array $options = array(), array $properties = array(

public function __call($name, $arguments) {
if (array_key_exists($name,$this->actions)){
$this->action = $name;
$this->setCurrentAction($this->action);
$this->setCurrentAction($name);
return $this->execute($arguments);
}
throw new UnknownModelAction(array(get_class($this),$name));
Expand Down
22 changes: 22 additions & 0 deletions tests/Endpoint/AbstractCollectionEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MRussell\REST\Tests\Endpoint;

use MRussell\REST\Endpoint\JSON\ModelEndpoint;
use MRussell\REST\Tests\Stubs\Auth\AuthController;
use MRussell\REST\Tests\Stubs\Endpoint\CollectionEndpointWithModel;
use MRussell\REST\Tests\Stubs\Endpoint\CollectionEndpoint;

Expand Down Expand Up @@ -79,6 +80,8 @@ public function testConstructor(){
* @covers ::buildModel
* @covers ::clear
* @covers ::reset
* @covers ::at
* @covers ::length
*/
public function testDataAccess(){
$Collection = new CollectionEndpointWithModel();
Expand All @@ -98,6 +101,7 @@ public function testDataAccess(){
unset($Collection[0]);
$this->assertEquals(FALSE,isset($Collection[0]));
$this->assertEquals(array(),$Collection->asArray());
$this->assertEquals(0,$Collection->length());
$this->assertEquals($Collection,$Collection->update($this->collection));
$this->assertEquals($this->collection,$Collection->asArray());
$this->assertEquals(array(
Expand All @@ -118,6 +122,24 @@ public function testDataAccess(){
$Model = $Collection->get('abc123');
$this->assertEquals(TRUE,is_object($Model));
$this->assertEquals('bar',$Model->get('foo'));
$Auth = new AuthController();
$Collection->setAuth($Auth);
$Model = $Collection->get('abc123');
$this->assertEquals(TRUE,is_object($Model));
$this->assertEquals($Auth,$Model->getAuth());
$Model = $Collection->at(1);
$this->assertEquals(array(
'id' => 'efg234',
'name' => 'test',
'foo' => ''
),$Model->asArray());
$Model = $Collection->at(-1);
$this->assertEquals(array(
'id' => 'k2r2d2',
'name' => 'Rogue One',
'foo' => 'bar'
),$Model->asArray());
$this->assertEquals(3,$Collection->length());
$this->assertEquals($Collection,$Collection->reset());
$this->assertEquals(array(),$Collection->asArray());
$this->assertEquals($Collection,$Collection->update($this->collection));
Expand Down

0 comments on commit 4c84a91

Please sign in to comment.