Skip to content

Instantly share code, notes, and snippets.

@Grinderofl
Created October 27, 2011 09:08
Show Gist options
  • Save Grinderofl/1319114 to your computer and use it in GitHub Desktop.
Save Grinderofl/1319114 to your computer and use it in GitHub Desktop.
PHP CakePHP: JSON DataSource
<?php
// CORE/app/models/datasources/json_source.php
/**
* JSON Source module for CakePHP.
*
* @package cake
*/
App::import('Core', 'HttpSocket');
class JsonSource extends DataSource {
/**
* General schema for datasource to recognise
*
* @var array
* @access protected
*/
protected $_schema = array(
'users' => array(
'id' => array(
'type' => 'integer',
'null' => true,
'key' => 'primary',
'length' => 11
),
'email' => array(
'type' => 'string',
'length' => 64
),
'password' => array(
'type' => 'string',
),
'firstname' => array(
'type' => 'string'
)
)
);
/**
* Connection
*
* @var object
* @access protected
*/
protected $connection = null;
/**
* Constructor
*
* @param array $config The configuration array passed from the creating function
* @access public
*/
public function __construct($config) {
$host = $config['host'];
$this->connection = new HttpSocket("{$host}");
parent::__construct($config);
}
/**
* Check whether we have an established connection to the server
*
* @access public
* @return boolean True if connection exists
*/
public function isConnected() {
return $this->connection !== null;
}
/**
* List the possible 'database' names we can use
* @access public
* @return array Source list
*/
public function listSources() {
return array('users');
}
/**
* Read function used by find queries
*
* @param $model string Model to search from
* @param $queryData array Conditions and other items
* @access public
* @return array Found results
*/
public function read($model, $queryData = array()) {
$url = "/readers/login/";
$queryData = $this->_userToReader($this->_conditionsToArray($queryData));
$result = $this->connection->post(
$url,
json_encode($queryData),
array(
'header' => array(
'Content-Type' => 'application/json',
'User-Agent' => 'Ekspress JSON DataSource'
)
)
);
if(empty($result) || $result == 'false') {
return false;
}
$response = $this->_readerToUser(json_decode($result, true));
return array('0' =>$response);
}
/**
* Internal function to convert User array (our model uses User for authentication)
* to Reader array (what the model in the webservice uses)
*
* @param $queryData array Passed query information
* @access private
* @return array Parsed query data
*/
function _userToReader($queryData) {
foreach($queryData as $key => $value) {
if($key == "User") {
$newkey = "Reader";
$queryData[$newkey] = $value;
unset($queryData[$key]);
}
}
return $queryData;
}
/**
* Internal function to convert Reader array (the one webservice uses) into
* User array (the one we use for authentication)
*
* @param $response array Passed response data
* @access private
* @return array Parsed response data
*/
function _readerToUser($response) {
foreach($response as $key => $value) {
if($key == "Reader") {
$newkey = "User";
$response[$newkey] = $value;
unset($response[$key]);
}
}
return $response;
}
/**
* Parses conditions into actual usable array
*
* @param $data array Passed conditions
* @access private
* @return array Parsed condition data
*/
function _conditionsToArray($data) {
$result = array();
foreach($data['conditions'] as $key => $value) {
$keys = explode('.', $key);
$result[$keys[0]][$keys[1]] = $value;
}
return $result;
}
public function describe($model) {
return $this->_schema['users'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment