Skip to content

Commit

Permalink
Add suport to read the count node from the response
Browse files Browse the repository at this point in the history
  • Loading branch information
markmercedes committed Jun 14, 2013
1 parent 656cd5b commit a40f4da
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
31 changes: 24 additions & 7 deletions lib/Config/TRestConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ class TRestConfig {

public function __construct($parameters) {
$this->apiUrl = $parameters['apiUrl'];
if(array_key_exists('singleItemNode', $parameters))
if (array_key_exists('singleItemNode', $parameters))
$this->singleItemNode = $parameters['singleItemNode'];
if(array_key_exists('listItemNode', $parameters))
if (array_key_exists('listItemNode', $parameters))
$this->listItemNode = $parameters['listItemNode'];
if(array_key_exists('cacheAdapter', $parameters))
if (array_key_exists('cacheAdapter', $parameters))
$this->cacheAdapter = $parameters['cacheAdapter'];
if (array_key_exists('listCountNode', $parameters))
$this->listCountNode = $parameters['listCountNode'];
}

/**
Expand All @@ -38,21 +40,36 @@ public function __construct($parameters) {
* of a property of the json result
*/
private $listItemNode;

/**
*
* @var CacheAdapter, has a cache adapter instance that should be used to cache data in the models
*
* @var CacheAdapter, has a cache adapter instance that should be used to
* cache data in the models
*/
private $cacheAdapter;

/**
*
* @var string, the name of the node that has the item count for a list result
*/
private $listCountNode;

/**
* @return the $listCountNode
*/
public function getListCountNode() {
return $this->listCountNode;
}

/**
*
* @return the $cacheAdapter
*/
public function getCacheAdapter() {
return $this->cacheAdapter;
}

/**
/**
*
* @return the $api_url
*/
Expand Down
27 changes: 20 additions & 7 deletions lib/Models/TRestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ abstract class TRestModel {
protected static $listItemNode;

protected static $configName = 'default';

protected static $listCountNode;

protected static function getConfig() {
return TRestConfigFactory::get(static::$configName);
Expand All @@ -38,15 +40,18 @@ public static function find($id, $params = array()) {
}

public static function findAll($limit = 0, $page = 0, $params = array()) {
$request = (new TRestRequest())->setUrl(self::getConfig()->getApiUrl())->setResource(static::$resource);
$request = (new TRestRequest())->setUrl(self::getConfig()->getApiUrl())->setResource(static::$resource)->setParameters($params);
if ($limit)
$request->setParameter('limit', $limit);
if ($page)
$request->setParameter('page', $page);
$result = array();
$items = self::getListItemNode(self::getRequestClient()->get($request));
foreach ($items as $item) {
$result[] = self::mapToObject($item, get_called_class());
$result = new \stdClass();
$result->items = array();
$response = self::getRequestClient()->get($request);
$responseItems = self::getListItemNode($response);
$result->count = self::getListCountNode($response);
foreach ($responseItems as $item) {
$result->items[] = self::mapToObject($item, get_called_class());
}
return $result;
}
Expand Down Expand Up @@ -77,7 +82,6 @@ protected function assignPropertyValues($values, $fields) {
$this->assignEmptyFieldValue($key, $fields[$key]['type']);
}
}
return $obj;
}

protected function assignRelations($values, $relations) {
Expand Down Expand Up @@ -125,7 +129,7 @@ public function getSingleItemNode($response) {
return is_array($result) ? $result[0] : $result;
}

public function getListItemNode($response) {
public static function getListItemNode($response) {
$result = null;
if (static::$listItemNode)
$result = $response->{static::$listItemNode};
Expand All @@ -135,6 +139,15 @@ public function getListItemNode($response) {
$result = $response;
return $result;
}

public function getListCountNode($response) {
$result = 0;
if (static::$listCountNode)
$result = $response->{static::$listCountNode};
else if (self::getConfig()->getListCountNode())
$result = $response->{self::getConfig()->getListCountNode()};
return $result;
}

public function __construct($values = null) {
$fields = $this->fields();
Expand Down

0 comments on commit a40f4da

Please sign in to comment.