From 2f18eb65b1d60fa87191f8f27efe675bdce81062 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Tue, 21 Aug 2012 14:55:09 +0000 Subject: [PATCH] Added ability to specify different obligatory arguments list for different functions to the PHP and JavaScript MibewAPI library --- src/messenger/webim/js/164/mibewapi.js | 16 +-- src/messenger/webim/js/source/mibewapi.js | 140 +++++++++++++++++----- src/messenger/webim/libs/mibew_api.php | 124 ++++++++++++++++--- 3 files changed, 222 insertions(+), 58 deletions(-) diff --git a/src/messenger/webim/js/164/mibewapi.js b/src/messenger/webim/js/164/mibewapi.js index e1bf7f7e..f1ca4f6d 100644 --- a/src/messenger/webim/js/164/mibewapi.js +++ b/src/messenger/webim/js/164/mibewapi.js @@ -6,14 +6,16 @@ License: http://mibew.org/license.php */ function MibewAPI(a){this.protocolVersion="1.0";if("object"!=typeof a||!(a instanceof MibewAPIInteraction))throw Error("Wrong interaction type");this.interaction=a} -MibewAPI.prototype.checkFunction=function(a,b){if("undefined"==typeof a["function"]||""==a["function"])throw Error("Cannot call for function with no name");if(b)for(var c=0;c + * this.obligatoryArguments = { + * '*': { + * 'return': {}, + * 'references': {} + * }, + * 'result': { + * 'errorCode': 0 + * } + * } + * + * @type Object + * @private + */ + this.obligatoryArguments = {}; + + /** + * Reserved function's names + * + * Defines reserved(system) function's names described in the Mibew API. + * @type Array */ this.reservedFunctionNames = []; - /** - * Reserved function names - */ - this.obligatoryArguments = []; - /** - * Returns default values of obligatory arguments - * - * @returns {Object} An object fields names are obligatory arguments and - * values are default values of them - */ - this.getDefaultObligatoryArguments = function(){ - return {} - } } +/** + * Returns obligatory arguments for the functionName function + * + * @param {String} functionName Function name + * @returns {Array} An array of obligatory arguments + */ +MibewAPIInteraction.prototype.getObligatoryArguments = function(functionName) { + var obligatoryArguments = []; + // Add obligatory for all functions arguments + if (typeof this.obligatoryArguments['*'] == 'object' && + this.obligatoryArguments['*'] instanceof Array) { + for (var arg in this.obligatoryArguments['*']) { + if (! this.obligatoryArguments['*'].hasOwnProperty(arg)) { + continue; + } + obligatoryArguments.push(arg); + } + } + // Add obligatory arguments for given function + if (typeof this.obligatoryArguments[functionName] == 'object' && + this.obligatoryArguments[functionName] instanceof Array) { + for (var arg in this.obligatoryArguments[functionName]) { + if (! this.obligatoryArguments[functionName].hasOwnProperty(arg)) { + continue; + } + obligatoryArguments.push(arg); + } + } + return obligatoryArguments; +} + +/** + * Returns default values of obligatory arguments for the functionName function + * + * @param {String} functionName Function name + * @returns {Object} An object fields names are obligatory arguments and + * values are default values of them + */ +MibewAPIInteraction.prototype.getDefaultObligatoryArguments = function(functionName) { + var obligatoryArguments = []; + // Add obligatory for all functions arguments + if (typeof this.obligatoryArguments['*'] == 'object' && + this.obligatoryArguments['*'] instanceof Array) { + for (var arg in this.obligatoryArguments['*']) { + if (! this.obligatoryArguments['*'].hasOwnProperty(arg)) { + continue; + } + obligatoryArguments[arg] = this.obligatoryArguments['*'][arg]; + } + } + // Add obligatory arguments for given function + if (typeof this.obligatoryArguments[functionName] == 'object' && + this.obligatoryArguments[functionName] instanceof Array) { + for (var arg in this.obligatoryArguments[functionName]) { + if (! this.obligatoryArguments[functionName].hasOwnProperty(arg)) { + continue; + } + obligatoryArguments[arg] = this.obligatoryArguments[functionName][arg]; + } + } + return obligatoryArguments; +} +/** + * End of MibewAPIInteraction class + */ + /** * Represents Window to core interaction type * @@ -307,21 +388,16 @@ function MibewAPIInteraction(){ * @todo Think about real values! */ function MibewAPIWindowToCoreInteraction() { + this.obligatoryArguments = { + '*': { + 'return': {}, + 'references': {} + } + }; + this.reservedFunctionNames = [ 'result' ]; - - this.obligatoryArguments = [ - 'return', - 'references' - ]; - - this.getDefaultObligatoryArguments = function(){ - return { - "return" : {}, - "references" : {} - } - } } MibewAPIWindowToCoreInteraction.prototype = new MibewAPIInteraction(); diff --git a/src/messenger/webim/libs/mibew_api.php b/src/messenger/webim/libs/mibew_api.php index b6acc47e..e283bd9c 100644 --- a/src/messenger/webim/libs/mibew_api.php +++ b/src/messenger/webim/libs/mibew_api.php @@ -201,7 +201,7 @@ Class MibewAPI { ); } $unset_arguments = array_diff( - $this->interaction->obligatoryArguments, + $this->interaction->getObligatoryArguments($function['function']), array_keys($function['arguments']) ); if (! empty($unset_arguments)) { @@ -273,7 +273,7 @@ Class MibewAPI { * @return array Result package */ public function buildResult($token, $result_arguments) { - $arguments = $result_arguments + $this->interaction->getDefaultObligatoryArguments(); + $arguments = $result_arguments + $this->interaction->getDefaultObligatoryArguments('result'); $package = array( 'token' => $token, 'functions' => array( @@ -438,44 +438,130 @@ Class MibewAPIExecutionContext { */ abstract class MibewAPIInteraction { /** - * Abligatory arguments in called functions - * @var array + * Defines obligatory arguments and default values for them + * + * @var array Keys of the array are function names ('*' for all functions). Values are arrays of obligatory + * arguments with key for name of an argument and value for default value. + * + * For example: + * + * protected $obligatoryArguments = array( + * '*' => array( // Obligatory arguments for all functions are + * 'return' => array(), // 'return' with array() by default and + * 'references' => array() // 'references' with array() by default + * ), + * 'result' => array( // There is an additional argument for the result function + * 'errorCode' => 0 // This is 'error_code' with 0 by default + * ) + * ); + * */ - public $obligatoryArguments; - /** - * Reserved function names - * @var array - */ - public $reservedFunctionNames; + protected $obligatoryArguments = array(); /** - * Returns default values of obligatory arguments + * Reserved function's names * + * Defines reserved(system) function's names described in the Mibew API. + * @var array + */ + public $reservedFunctionNames = array(); + + /** + * Returns obligatory arguments for the $function_name function + * + * @param string $function_name Function name + * @return array An array of obligatory arguments + */ + public function getObligatoryArguments($function_name) { + $obligatory_arguments = array(); + // Add obligatory for all functions arguments + if (! empty($this->obligatoryArguments['*'])) { + $obligatory_arguments = array_merge( + $obligatory_arguments, + array_keys($this->obligatoryArguments['*']) + ); + } + // Add obligatory arguments for given function + if (! empty($this->obligatoryArguments[$function_name])) { + $obligatory_arguments = array_merge( + $obligatory_arguments, + array_keys($this->obligatoryArguments[$function_name]) + ); + } + return array_unique($obligatory_arguments); + } + + /** + * Returns default values of obligatory arguments for the $function_name function + * + * @param string $function_name Function name * @return array Associative array with keys are obligatory arguments and values are default * values of them */ - abstract public function getDefaultObligatoryArguments(); + public function getDefaultObligatoryArguments($function_name) { + $obligatory_arguments = array(); + // Add obligatory for all functions arguments + if (! empty($this->obligatoryArguments['*'])) { + $obligatory_arguments = array_merge($obligatory_arguments, $this->obligatoryArguments['*']); + } + // Add obligatory arguments for given function + if (! empty($this->obligatoryArguments[$function_name])) { + $obligatory_arguments = array_merge($obligatory_arguments, $this->obligatoryArguments[$function_name]); + } + return $obligatory_arguments; + } } /** * Implements Base Mibew Interaction */ class MibewAPIBaseInteraction extends MibewAPIInteraction { - public $obligatoryArguments = array( - 'references', - 'return' + /** + * Defines obligatory arguments and default values for them + * @var array + * @see MibewAPIInteraction::$obligatoryArgumnents + */ + protected $obligatoryArguments = array( + '*' => array( + 'references' => array(), + 'return' => array() + ) ); + /** + * Reserved function's names + * @var array + * @see MibewAPIInteraction::$reservedFunctionNames + */ public $reservedFunctionNames = array( 'result' ); +} - public function getDefaultObligatoryArguments() { - return array( +/** + * Implements Mibew Core - Mibew Chat Window interaction + */ +class MibewAPIWindowInteraction extends MibewAPIInteraction { + /** + * Defines obligatory arguments and default values for them + * @var array + * @see MibewAPIInteraction::$obligatoryArgumnents + */ + protected $obligatoryArguments = array( + '*' => array( 'references' => array(), 'return' => array() - ); - } + ) + ); + + /** + * Reserved function's names + * @var array + * @see MibewAPIInteraction::$reservedFunctionNames + */ + public $reservedFunctionNames = array( + 'result' + ); } /**