diff --git a/src/mibew/js/compiled/chat/mibewapi_chat_interaction.js b/src/mibew/js/compiled/chat/mibewapi_chat_interaction.js index 5bd4abfe..564c731a 100644 --- a/src/mibew/js/compiled/chat/mibewapi_chat_interaction.js +++ b/src/mibew/js/compiled/chat/mibewapi_chat_interaction.js @@ -5,4 +5,4 @@ You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 */ -MibewAPIChatInteraction=function(){this.obligatoryArguments={"*":{threadId:null,token:null,"return":{},references:{}},result:{errorCode:0}};this.getReservedFunctionsNames=function(){return["result"]}};MibewAPIChatInteraction.prototype=new MibewAPIInteraction; +MibewAPIChatInteraction=function(){this.mandatoryArguments=function(){return{"*":{threadId:null,token:null,"return":{},references:{}},result:{errorCode:0}}};this.getReservedFunctionsNames=function(){return["result"]}};MibewAPIChatInteraction.prototype=new MibewAPIInteraction; diff --git a/src/mibew/js/compiled/chat_app.js b/src/mibew/js/compiled/chat_app.js index 929b8437..90f617e5 100644 --- a/src/mibew/js/compiled/chat_app.js +++ b/src/mibew/js/compiled/chat_app.js @@ -11,7 +11,7 @@ You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 */ -MibewAPIChatInteraction=function(){this.obligatoryArguments={"*":{threadId:null,token:null,"return":{},references:{}},result:{errorCode:0}};this.getReservedFunctionsNames=function(){return["result"]}};MibewAPIChatInteraction.prototype=new MibewAPIInteraction; +MibewAPIChatInteraction=function(){this.mandatoryArguments=function(){return{"*":{threadId:null,token:null,"return":{},references:{}},result:{errorCode:0}}};this.getReservedFunctionsNames=function(){return["result"]}};MibewAPIChatInteraction.prototype=new MibewAPIInteraction; /* Copyright 2005-2013 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License"). diff --git a/src/mibew/js/compiled/mibewapi.js b/src/mibew/js/compiled/mibewapi.js index 46c4c74e..45448954 100644 --- a/src/mibew/js/compiled/mibewapi.js +++ b/src/mibew/js/compiled/mibewapi.js @@ -6,15 +6,14 @@ http://www.apache.org/licenses/LICENSE-2.0 */ 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=this.interaction.getReservedFunctionsNames(),d=0;d - * this.obligatoryArguments = { - * '*': { - * 'return': {}, - * 'references': {} - * }, - * 'result': { - * 'errorCode': 0 + * this.mandatoryArguments = function() { + * return { + * '*': { + * 'return': {}, + * 'references': {} + * }, + * 'result': { + * 'errorCode': 0 + * } * } * } * - * @type Object + * @returns {Object} * @private */ - this.obligatoryArguments = {}; + this.mandatoryArguments = function() { + return {}; + } /** * Returns reserved (system) functions' names @@ -321,62 +325,64 @@ function MibewAPIInteraction(){ } /** - * Returns obligatory arguments for the functionName function + * Returns mandatory arguments for the functionName function * * @param {String} functionName Function name - * @returns {Array} An array of obligatory arguments + * @returns {Array} An array of mandatory arguments */ -MibewAPIInteraction.prototype.getObligatoryArguments = function(functionName) { - var obligatoryArguments = []; - // Add obligatory for all functions arguments - if (typeof this.obligatoryArguments['*'] == 'object') { - for (var arg in this.obligatoryArguments['*']) { - if (! this.obligatoryArguments['*'].hasOwnProperty(arg)) { +MibewAPIInteraction.prototype.getMandatoryArguments = function(functionName) { + var allMandatoryArguments = this.mandatoryArguments(); + var mandatoryArguments = []; + // Add mandatory for all functions arguments + if (typeof allMandatoryArguments['*'] == 'object') { + for (var arg in allMandatoryArguments['*']) { + if (! allMandatoryArguments['*'].hasOwnProperty(arg)) { continue; } - obligatoryArguments.push(arg); + mandatoryArguments.push(arg); } } - // Add obligatory arguments for given function - if (typeof this.obligatoryArguments[functionName] == 'object') { - for (var arg in this.obligatoryArguments[functionName]) { - if (! this.obligatoryArguments[functionName].hasOwnProperty(arg)) { + // Add mandatory arguments for given function + if (typeof allMandatoryArguments[functionName] == 'object') { + for (var arg in allMandatoryArguments[functionName]) { + if (! allMandatoryArguments[functionName].hasOwnProperty(arg)) { continue; } - obligatoryArguments.push(arg); + mandatoryArguments.push(arg); } } - return obligatoryArguments; + return mandatoryArguments; } /** - * Returns default values of obligatory arguments for the functionName function + * Returns default values of mandatory arguments for the functionName function * * @param {String} functionName Function name - * @returns {Object} An object fields names are obligatory arguments and + * @returns {Object} An object fields names are mandatory arguments and * values are default values of them */ -MibewAPIInteraction.prototype.getObligatoryArgumentsDefaults = function(functionName) { - var obligatoryArguments = {}; - // Add obligatory for all functions arguments - if (typeof this.obligatoryArguments['*'] == 'object') { - for (var arg in this.obligatoryArguments['*']) { - if (! this.obligatoryArguments['*'].hasOwnProperty(arg)) { +MibewAPIInteraction.prototype.getMandatoryArgumentsDefaults = function(functionName) { + var allMandatoryArguments = this.mandatoryArguments(); + var mandatoryArguments = {}; + // Add mandatory for all functions arguments + if (typeof allMandatoryArguments['*'] == 'object') { + for (var arg in allMandatoryArguments['*']) { + if (! allMandatoryArguments['*'].hasOwnProperty(arg)) { continue; } - obligatoryArguments[arg] = this.obligatoryArguments['*'][arg]; + mandatoryArguments[arg] = allMandatoryArguments['*'][arg]; } } - // Add obligatory arguments for given function - if (typeof this.obligatoryArguments[functionName] == 'object') { - for (var arg in this.obligatoryArguments[functionName]) { - if (! this.obligatoryArguments[functionName].hasOwnProperty(arg)) { + // Add mandatory arguments for given function + if (typeof allMandatoryArguments[functionName] == 'object') { + for (var arg in allMandatoryArguments[functionName]) { + if (! allMandatoryArguments[functionName].hasOwnProperty(arg)) { continue; } - obligatoryArguments[arg] = this.obligatoryArguments[functionName][arg]; + mandatoryArguments[arg] = allMandatoryArguments[functionName][arg]; } } - return obligatoryArguments; + return mandatoryArguments; } /** * End of MibewAPIInteraction class diff --git a/src/mibew/js/source/users/mibewapi_users_interaction.js b/src/mibew/js/source/users/mibewapi_users_interaction.js index ce15a9e2..23b4202d 100644 --- a/src/mibew/js/source/users/mibewapi_users_interaction.js +++ b/src/mibew/js/source/users/mibewapi_users_interaction.js @@ -12,16 +12,18 @@ * @constructor */ MibewAPIUsersInteraction = function() { - this.obligatoryArguments = { - '*': { - 'agentId': null, - 'return': {}, - 'references': {} - }, - 'result': { - 'errorCode': 0 - } - }; + this.mandatoryArguments = function() { + return { + '*': { + 'agentId': null, + 'return': {}, + 'references': {} + }, + 'result': { + 'errorCode': 0 + } + }; + } this.getReservedFunctionsNames = function() { return [ diff --git a/src/mibew/libs/classes/Mibew/API/API.php b/src/mibew/libs/classes/Mibew/API/API.php index c61d6c5b..6f487ee9 100644 --- a/src/mibew/libs/classes/Mibew/API/API.php +++ b/src/mibew/libs/classes/Mibew/API/API.php @@ -196,13 +196,13 @@ class API ); } $unset_arguments = array_diff( - $this->interaction->getObligatoryArguments($function['function']), + $this->interaction->getMandatoryArguments($function['function']), array_keys($function['arguments']) ); if (!empty($unset_arguments)) { throw new APIException( "Arguments '" . implode("', '", $unset_arguments) . "' must be set", - APIException::OBLIGATORY_ARGUMENTS_MISSED + APIException::MANDATORY_ARGUMENTS_MISSED ); } } @@ -264,7 +264,7 @@ class API */ public function buildResult($token, $result_arguments) { - $arguments = $result_arguments + $this->interaction->getObligatoryArgumentsDefaults('result'); + $arguments = $result_arguments + $this->interaction->getMandatoryArgumentsDefaults('result'); $package = array( 'token' => $token, 'functions' => array( diff --git a/src/mibew/libs/classes/Mibew/API/APIException.php b/src/mibew/libs/classes/Mibew/API/APIException.php index e30f408e..7b78c612 100644 --- a/src/mibew/libs/classes/Mibew/API/APIException.php +++ b/src/mibew/libs/classes/Mibew/API/APIException.php @@ -71,9 +71,9 @@ class APIException extends \Exception */ const NOT_VALID_JSON = 12; /** - * Some of the function's obligatory arguments are missed + * Some of the function's mandatory arguments are missed */ - const OBLIGATORY_ARGUMENTS_MISSED = 13; + const MANDATORY_ARGUMENTS_MISSED = 13; /** * Request contains more than one result functions */ diff --git a/src/mibew/libs/classes/Mibew/API/Interaction/AbstractInteraction.php b/src/mibew/libs/classes/Mibew/API/Interaction/AbstractInteraction.php index afc6f2a0..1b6092e9 100644 --- a/src/mibew/libs/classes/Mibew/API/Interaction/AbstractInteraction.php +++ b/src/mibew/libs/classes/Mibew/API/Interaction/AbstractInteraction.php @@ -34,83 +34,93 @@ abstract class AbstractInteraction abstract public function getReservedFunctionsNames(); /** - * Defines obligatory arguments and default values for them + * Defines mandatory 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 + * This method implements "template method" design pattern. + * + * @return array Keys of the array are function names ('*' for all functions). + * Values are arrays of Mandatory 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 - * ), + * protected function mandatoryArguments() + * { + * return array( + * // Mandatory arguments for all functions are: + * '*' => array( + * // 'return' with array() by default and + * 'return' => array(), + * // 'references' with array() by default + * 'references' => array(), + * ), * - * 'result' => array( * // There is an additional argument for the result function - * 'errorCode' => 0 // This is 'error_code' with 0 by default - * ) - * ); + * 'result' => array( + * // This is 'error_code' with 0 by default + * 'errorCode' => 0, + * ), + * ); + * } * */ - protected $obligatoryArguments = array(); + abstract protected function mandatoryArguments(); /** - * Returns obligatory arguments for the $function_name function + * Returns mandatory arguments for the $function_name function * * @param string $function_name Function name - * @return array An array of obligatory arguments + * @return array An array of mandatory arguments */ - public function getObligatoryArguments($function_name) + public function getMandatoryArguments($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['*']) + $all_mandatory_arguments = $this->mandatoryArguments(); + $mandatory_arguments = array(); + // Add mandatory for all functions arguments + if (!empty($all_mandatory_arguments['*'])) { + $mandatory_arguments = array_merge( + $mandatory_arguments, + array_keys($all_mandatory_arguments['*']) ); } - // Add obligatory arguments for given function - if (!empty($this->obligatoryArguments[$function_name])) { - $obligatory_arguments = array_merge( - $obligatory_arguments, - array_keys($this->obligatoryArguments[$function_name]) + // Add mandatory arguments for given function + if (!empty($all_mandatory_arguments[$function_name])) { + $mandatory_arguments = array_merge( + $mandatory_arguments, + array_keys($all_mandatory_arguments[$function_name]) ); } - return array_unique($obligatory_arguments); + return array_unique($mandatory_arguments); } /** - * Returns default values of obligatory arguments for the $function_name + * Returns default values of mandatory arguments for the $function_name * function * * @param string $function_name Function name - * @return array Associative array with keys are obligatory arguments and + * @return array Associative array with keys are mandatory arguments and * values are default values of them */ - public function getObligatoryArgumentsDefaults($function_name) + public function getMandatoryArgumentsDefaults($function_name) { - $obligatory_arguments = array(); - // Add obligatory for all functions arguments - if (!empty($this->obligatoryArguments['*'])) { - $obligatory_arguments = array_merge( - $obligatory_arguments, - $this->obligatoryArguments['*'] + $all_mandatory_arguments = $this->mandatoryArguments(); + $mandatory_arguments = array(); + // Add mandatory for all functions arguments + if (!empty($all_mandatory_arguments['*'])) { + $mandatory_arguments = array_merge( + $mandatory_arguments, + $all_mandatory_arguments['*'] ); } - // Add obligatory arguments for given function - if (!empty($this->obligatoryArguments[$function_name])) { - $obligatory_arguments = array_merge( - $obligatory_arguments, - $this->obligatoryArguments[$function_name] + // Add mandatory arguments for given function + if (!empty($all_mandatory_arguments[$function_name])) { + $mandatory_arguments = array_merge( + $mandatory_arguments, + $all_mandatory_arguments[$function_name] ); } - return $obligatory_arguments; + return $mandatory_arguments; } } diff --git a/src/mibew/libs/classes/Mibew/API/Interaction/ChatInteraction.php b/src/mibew/libs/classes/Mibew/API/Interaction/ChatInteraction.php index 73b86391..af783a30 100644 --- a/src/mibew/libs/classes/Mibew/API/Interaction/ChatInteraction.php +++ b/src/mibew/libs/classes/Mibew/API/Interaction/ChatInteraction.php @@ -36,19 +36,23 @@ class ChatInteraction extends AbstractInteraction } /** - * Defines obligatory arguments and default values for them - * @var array - * @see \Mibew\API\Interaction\AbstractInteraction::$obligatoryArgumnents + * Defines mandatory arguments and default values for them. + * + * @return array + * @see \Mibew\API\Interaction\AbstractInteraction::mandatoryArguments */ - protected $obligatoryArguments = array( - '*' => array( - 'threadId' => null, - 'token' => null, - 'references' => array(), - 'return' => array(), - ), - 'result' => array( - 'errorCode' => 0, - ), - ); + protected function mandatoryArguments() + { + return array( + '*' => array( + 'threadId' => null, + 'token' => null, + 'references' => array(), + 'return' => array(), + ), + 'result' => array( + 'errorCode' => 0, + ), + ); + } } diff --git a/src/mibew/libs/classes/Mibew/API/Interaction/InviteInteraction.php b/src/mibew/libs/classes/Mibew/API/Interaction/InviteInteraction.php index dd40a915..edb8585d 100644 --- a/src/mibew/libs/classes/Mibew/API/Interaction/InviteInteraction.php +++ b/src/mibew/libs/classes/Mibew/API/Interaction/InviteInteraction.php @@ -36,18 +36,22 @@ class InviteInteraction extends AbstractInteraction } /** - * Defines obligatory arguments and default values for them - * @var array - * @see \Mibew\API\Interaction\AbstractInteraction::$obligatoryArgumnents + * Defines mandatory arguments and default values for them. + * + * @return array + * @see \Mibew\API\Interaction\AbstractInteraction::mandatoryArguments */ - protected $obligatoryArguments = array( - '*' => array( - 'references' => array(), - 'return' => array(), - 'visitorId' => null, - ), - 'result' => array( - 'errorCode' => 0, - ), - ); + protected function mandatoryArguments() + { + return array( + '*' => array( + 'references' => array(), + 'return' => array(), + 'visitorId' => null, + ), + 'result' => array( + 'errorCode' => 0, + ), + ); + } } diff --git a/src/mibew/libs/classes/Mibew/API/Interaction/UsersInteraction.php b/src/mibew/libs/classes/Mibew/API/Interaction/UsersInteraction.php index 97dd3583..a0464a7f 100644 --- a/src/mibew/libs/classes/Mibew/API/Interaction/UsersInteraction.php +++ b/src/mibew/libs/classes/Mibew/API/Interaction/UsersInteraction.php @@ -36,21 +36,25 @@ class UsersInteraction extends AbstractInteraction } /** - * Defines obligatory arguments and default values for them - * @var array - * @see \Mibew\API\Interaction\AbstractInteraction::$obligatoryArgumnents + * Defines mandatory arguments and default values for them. + * + * @return array + * @see \Mibew\API\Interaction\AbstractInteraction::mandatoryArguments */ - protected $obligatoryArguments = array( - '*' => array( - 'agentId' => null, - 'references' => array(), - 'return' => array(), - ), - 'updateThreads' => array( - 'revision' => 0, - ), - 'result' => array( - 'errorCode' => 0, - ), - ); + protected function mandatoryArguments() + { + return array( + '*' => array( + 'agentId' => null, + 'references' => array(), + 'return' => array(), + ), + 'updateThreads' => array( + 'revision' => 0, + ), + 'result' => array( + 'errorCode' => 0, + ), + ); + } }