Files
fdn2/private/lib/backup/cbsClient/rapi/messages/RAPI_MessageDetails.php
T

135 lines
3.7 KiB
PHP

<?php
/*
Author: Riccardo Di Dato
Creation Date: 01/lug/2015
*/
abstract class RAPI_MessageDetails {
/**
* @var stdClass
*/
private $data;
/**
* @var stdClass
*/
private $attachments = array();
/**
* @return stdClass
*/
public function getData(){
return $this->data;
}
/**
* @return stdClass
*/
public function getAttachmentData($name){
if (!array_key_exists($name, $this->attachments)){
throw new RAPI_Exception("Invalid attachment name ($name) passed to '".get_called_class()."::getAttachmentData()'");
}
return $this->attachments[$name];
}
/**
* Imposta il file di appoggio LOCALE per un attachment
* @param string $name nome dell'attachment
* @param string $path il path del file
* @throws RAPI_Exception
*/
public function setAttachmentLocalFileReference($name, $path){
if (!array_key_exists($name, $this->attachments)){
throw new RAPI_Exception("Invalid attachment name ($name) passed to '".get_called_class()."::setAttachmentLocalFileReference()'");
}
$this->attachments[$name]->localReference = $path;
}
public function getAttachmentLocalFileReference($name){
if (!array_key_exists($name, $this->attachments)){
throw new RAPI_Exception("Invalid attachment name ($name) passed to '".get_called_class()."::getAttachmentLocalFileReference()'");
}
return $this->attachments[$name]->localReference;
}
/**
* Il parametro $data deve avere come attributi almeno quelli restituiti da getArgumentList()
* @param stdClass $data
* @throws RAPI_Exception
*/
public function __construct(stdClass $data, array $attachments = array()){
foreach (static::getArgumentList() as $arg){
if (!property_exists($data, $arg)){
throw new RAPI_Exception("Invalid data object passed to '".get_called_class()."'. Missing property '$arg'");
}
}
$requiredAttachments = static::getAttachmentList();
$requiredFields = array("md5","size","name");
if (sizeof($attachments)>0){
foreach ($attachments as $attachment){
foreach ($requiredFields as $field){
if (!property_exists($attachment, $field)){
throw new RAPI_Exception("Invalid attachment data object passed to '".get_called_class()."'. Missing property '$field' for attachment '$attachName'");
}
}
$this->attachments[$attachment->name] = $attachment;
if ( ($key = array_search($attachment->name, $requiredAttachments)) !== false ){
unset($requiredAttachments[$key]);
}
}
}
if (sizeof($requiredAttachments)>0){
throw new RAPI_Exception("Invalid attachment array passed to '".get_called_class()."'. Missing attachments '".implode(", ", $requiredAttachments)."'");
}
$this->data = $data;
}
/**
* Restituisce la codifica JSON del messaggio
* @return string
*/
public function __toString(){
return json_encode($this->generateEncryptionObject());
}
/**
* Restituisce una stdClass utilizzabile per rigenerare l'oggetto
* @return stdClass
*/
public function generateEncryptionObject(){
$tmp = new stdClass();
$tmp->data = new stdClass();
foreach (static::getArgumentList() as $argName){
$tmp->data->$argName = $this->data->$argName;
}
$tmp->attachments = array();
foreach (static::getAttachmentList() as $attachName){
$ele = new stdClass();
$ele->name = $attachName;
$ele->md5 = $this->attachments[$attachName]->md5;
$ele->size = $this->attachments[$attachName]->size;
$tmp->attachments[] = $ele;
}
return $tmp;
}
/*
* Abstract Methods
*/
/**
* Returns the list of arguments
* @return string[]
*/
public static abstract function getArgumentList();
/**
* Returns the list of attachments
* @var stdClass[]
*/
public static abstract function getAttachmentList();
}
?>