Skip to content

Commit

Permalink
Documentation for the TRestModel
Browse files Browse the repository at this point in the history
  • Loading branch information
markmercedes committed Jun 22, 2013
1 parent 6d5ef3a commit 5f51f50
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions lib/Models/TRestModel.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
<?php

/**
*
* Model that should be use used to inherit the functionallity of this library
*
* @author Marcos Mercedes <marcos.mercedesn@gmail.com>
* @package TRest\Models
*/
namespace TRest\Models;

use TRest\Http\TRestRequest;

abstract class TRestModel extends TRestModelBase {

/**
* Creates (POST) or updates(PUT) an entity
*
* @return boolean
*/
public function save() {
$request = (new TRestRequest())->setUrl(self::getConfig()->getApiUrl())->setResource(static::$resource)->setPath($this->{static::$recordId})->setEntity(json_encode(array(
static::$singleItemNode => $this->mapToJSON()
Expand All @@ -15,14 +27,19 @@ public function save() {
$this->constructObject(self::getSingleItemNode($result));
return true;
}

public function delete(){

/**
* Deletes an entity
*/
public function delete() {
$request = (new TRestRequest())->setUrl(self::getConfig()->getApiUrl())->setResource(static::$resource)->setPath($this->{static::$recordId});
self::getRequestClient()->delete($request);
}

/**
*
*
* Finds a entity by it's specified identifer
*
* @return TRestModel
*/
public static function find($id, $params = array(), $path = null, $cacheTtl = TREST_DEFAULT_CACHE_TTL) {
Expand All @@ -41,6 +58,19 @@ public static function find($id, $params = array(), $path = null, $cacheTtl = TR
}
}

/**
*
* Returns an object with a list of objects in a property named items and in
* case that pagination is used, in another property name count returns the
* total items that are available for this response
*
* @param number $limit
* @param number $page
* @param array $params
* @param string $path
* @param number $cacheTtl
* @return array of TRestModels
*/
public static function findAll($limit = 0, $page = 0, $params = array(), $path = null, $cacheTtl = TREST_DEFAULT_CACHE_TTL) {
$request = (new TRestRequest())->setUrl(self::getConfig()->getApiUrl())->setPath($path)->setResource(static::$resource)->setParameters($params)->setParameter('limit', $limit)->setParameter('page', $page);
$cacheKey = $request->getUrlHash();
Expand All @@ -63,6 +93,12 @@ public static function findAll($limit = 0, $page = 0, $params = array(), $path =
return $result;
}

/**
*
* Assigns the properties values to this class
*
* @param object $values
*/
protected function constructObject($values = null) {
$fields = $this->fields();
if ($values) {
Expand All @@ -75,6 +111,13 @@ protected function constructObject($values = null) {
}
}

/**
*
* Constructs an object and assign the parameters specified by the $values
* argument passed to the constructor
*
* @param object $values
*/
public function __construct($values = null) {
$this->constructObject($values);
$this->build();
Expand Down

0 comments on commit 5f51f50

Please sign in to comment.