Initial Commit
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
abstract class App_Model_AbstractRest
|
||||
{
|
||||
protected $_resources = array();
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return Application_Model_RestResources_Generic
|
||||
*/
|
||||
public function getResource($name)
|
||||
{
|
||||
|
||||
if (!isset($this->_resources[$name])) {
|
||||
$class = implode('_', array(
|
||||
$this->_getNamespace(),
|
||||
'Model',
|
||||
'RestResources',
|
||||
ucfirst($name)));
|
||||
|
||||
//Controllo l'esistenza della classe [commentato perchè genera errore su alcune risorse]
|
||||
if (!class_exists($class)) {
|
||||
throw new App_Controller_Errors_WebServiceError("Errore di generazione per la classe risorsa rest '$class'. Potrebbe non esistere!");
|
||||
}
|
||||
|
||||
$this->_resources[$name] = new $class();
|
||||
}
|
||||
|
||||
return $this->_resources[$name];
|
||||
}
|
||||
|
||||
private function _getNamespace()
|
||||
{
|
||||
$ns = explode('_', get_class($this));
|
||||
return $ns[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $results
|
||||
* @param $page
|
||||
* @param $count
|
||||
* @return Zend_Paginator
|
||||
*
|
||||
* Questa funzione pasato un array lo converte in un oggetto Zend Paginator
|
||||
*/
|
||||
public function converResultsToZendPaginator($results,$page,$count){
|
||||
$dati = $this->getOffsetPaginatedArray($results['data'],$results['totalItem'],$page,$count);
|
||||
$results = Zend_Paginator::factory($dati);
|
||||
$results->setItemCountPerPage($count);
|
||||
$results->setCurrentPageNumber($page);
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $results
|
||||
* @param $totalItems
|
||||
* @param $page
|
||||
* @param $count
|
||||
* @return array
|
||||
* Funzione che crea un offset di oggetti per far si che il paginator conosca il numero totale di elemnti presenti
|
||||
*/
|
||||
public function getOffsetPaginatedArray($results,$totalItems,$page,$count){
|
||||
$dati = array();
|
||||
$rangeStart = (($page * $count) - $count) + 1;
|
||||
$rangeEnd = $page * $count;
|
||||
$rangeX = 0;
|
||||
|
||||
for($i = 0; $i < $totalItems ;$i++){
|
||||
if($i >= $rangeStart -1 && $i <= $rangeEnd -1){
|
||||
$dati[$i] = $results[$rangeX];
|
||||
$rangeX++;
|
||||
}else{
|
||||
$dati[$i] = array();
|
||||
}
|
||||
}
|
||||
|
||||
return $dati;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user