Add Interaction::mandatoryArguments method

Rename "obligatory" arguments to "mandatory" arguments. Use
Mibew API Interaction "mandatoryArguments" method instead of
"obligatoryArguments" property to increase code reliability.
This commit is contained in:
Dmitriy Simushev 2014-01-29 12:23:17 +00:00
parent 26b2d74cc4
commit b5778a8be2
14 changed files with 207 additions and 176 deletions

View File

@ -5,4 +5,4 @@
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 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;

View File

@ -11,7 +11,7 @@
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 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. Copyright 2005-2013 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License"). Licensed under the Apache License, Version 2.0 (the "License").

View File

@ -6,15 +6,14 @@
http://www.apache.org/licenses/LICENSE-2.0 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} 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<c.length;d++)if(a["function"]==c[d])throw Error("'"+a["function"]+"' is reserved function name");if("object"!=typeof a.arguments)throw Error("There are no arguments in '"+a["function"]+"' function");var c=0,f=this.interaction.getObligatoryArguments(a["function"]),e;a:for(e in a.arguments)for(d= 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 d=this.interaction.getReservedFunctionsNames(),c=0;c<d.length;c++)if(a["function"]==d[c])throw Error("'"+a["function"]+"' is reserved function name");if("object"!=typeof a.arguments)throw Error("There are no arguments in '"+a["function"]+"' function");var d=0,f=this.interaction.getMandatoryArguments(a["function"]),e;a:for(e in a.arguments)for(c=
0;d<f.length;d++)if(e==f[d]){c++;continue a}if(c!=f.length)throw Error("Not all obligatory arguments are set in '"+a["function"]+"' function");};MibewAPI.prototype.checkRequest=function(a){if("string"!=typeof a.token){if("undefined"==typeof a.token)throw Error("Empty token");throw Error("Wrong token type");}if(""==a.token)throw Error("Empty token");if("object"!=typeof a.functions||!(a.functions instanceof Array)||0==a.functions.length)throw Error("Empty functions set");for(var b=0;b<a.functions.length;b++)this.checkFunction(a.functions[b])}; 0;c<f.length;c++)if(e==f[c]){d++;continue a}if(d!=f.length)throw Error("Not all mandatory arguments are set in '"+a["function"]+"' function");};MibewAPI.prototype.checkRequest=function(a){if("string"!=typeof a.token){if("undefined"==typeof a.token)throw Error("Empty token");throw Error("Wrong token type");}if(""==a.token)throw Error("Empty token");if("object"!=typeof a.functions||!(a.functions instanceof Array)||0==a.functions.length)throw Error("Empty functions set");for(var b=0;b<a.functions.length;b++)this.checkFunction(a.functions[b])};
MibewAPI.prototype.checkPackage=function(a){if("undefined"==typeof a.signature)throw Error("Missed package signature");if("undefined"==typeof a.proto)throw Error("Missed protocol version");if(a.proto!=this.protocolVersion)throw Error("Wrong protocol version");if("undefined"==typeof a.async)throw Error("'async' flag is missed");if("boolean"!=typeof a.async)throw Error("Wrong 'async' flag value");if("object"!=typeof a.requests||!(a.requests instanceof Array)||0==a.requests.length)throw Error("Empty requests set"); MibewAPI.prototype.checkPackage=function(a){if("undefined"==typeof a.signature)throw Error("Missed package signature");if("undefined"==typeof a.proto)throw Error("Missed protocol version");if(a.proto!=this.protocolVersion)throw Error("Wrong protocol version");if("undefined"==typeof a.async)throw Error("'async' flag is missed");if("boolean"!=typeof a.async)throw Error("Wrong 'async' flag value");if("object"!=typeof a.requests||!(a.requests instanceof Array)||0==a.requests.length)throw Error("Empty requests set");
for(var b=0;b<a.requests.length;b++)this.checkRequest(a.requests[b])};MibewAPI.prototype.getResultFunction=function(a,b){"undefined"==typeof b&&(b=null);var c=null,d;for(d in a)if(a.hasOwnProperty(d)&&"result"==a[d]["function"]){if(null!==c)throw Error("Function 'result' already exists in functions list");c=a[d]}if(!0===b&&null===c)throw Error("There is no 'result' function in functions list");if(!1===b&&null!==c)throw Error("There is 'result' function in functions list");return c}; for(var b=0;b<a.requests.length;b++)this.checkRequest(a.requests[b])};MibewAPI.prototype.getResultFunction=function(a,b){"undefined"==typeof b&&(b=null);var d=null,c;for(c in a)if(a.hasOwnProperty(c)&&"result"==a[c]["function"]){if(null!==d)throw Error("Function 'result' already exists in functions list");d=a[c]}if(!0===b&&null===d)throw Error("There is no 'result' function in functions list");if(!1===b&&null!==d)throw Error("There is 'result' function in functions list");return d};
MibewAPI.prototype.buildResult=function(a,b){var c=this.interaction.getObligatoryArgumentsDefaults("result"),d;for(d in c)c.hasOwnProperty(d)&&(a[d]=c[d]);return{token:b,functions:[{"function":"result",arguments:a}]}};MibewAPI.prototype.encodePackage=function(a){var b={signature:""};b.proto=this.protocolVersion;b.async=!0;b.requests=a;return encodeURIComponent(JSON.stringify(b)).replace(/\%20/gi,"+")}; MibewAPI.prototype.buildResult=function(a,b){var d=this.interaction.getMandatoryArgumentsDefaults("result"),c;for(c in d)d.hasOwnProperty(c)&&(a[c]=d[c]);return{token:b,functions:[{"function":"result",arguments:a}]}};MibewAPI.prototype.encodePackage=function(a){var b={signature:""};b.proto=this.protocolVersion;b.async=!0;b.requests=a;return encodeURIComponent(JSON.stringify(b)).replace(/\%20/gi,"+")};
MibewAPI.prototype.decodePackage=function(a){a=JSON.parse(decodeURIComponent(a.replace(/\+/gi," ")));this.checkPackage(a);return a};function MibewAPIInteraction(){this.obligatoryArguments={};this.getReservedFunctionsNames=function(){return[]}} MibewAPI.prototype.decodePackage=function(a){a=JSON.parse(decodeURIComponent(a.replace(/\+/gi," ")));this.checkPackage(a);return a};function MibewAPIInteraction(){this.mandatoryArguments=function(){return{}};this.getReservedFunctionsNames=function(){return[]}}
MibewAPIInteraction.prototype.getObligatoryArguments=function(a){var b=[];if("object"==typeof this.obligatoryArguments["*"])for(var c in this.obligatoryArguments["*"])this.obligatoryArguments["*"].hasOwnProperty(c)&&b.push(c);if("object"==typeof this.obligatoryArguments[a])for(c in this.obligatoryArguments[a])this.obligatoryArguments[a].hasOwnProperty(c)&&b.push(c);return b}; MibewAPIInteraction.prototype.getMandatoryArguments=function(a){var b=this.mandatoryArguments(),d=[];if("object"==typeof b["*"])for(var c in b["*"])b["*"].hasOwnProperty(c)&&d.push(c);if("object"==typeof b[a])for(c in b[a])b[a].hasOwnProperty(c)&&d.push(c);return d};
MibewAPIInteraction.prototype.getObligatoryArgumentsDefaults=function(a){var b={};if("object"==typeof this.obligatoryArguments["*"])for(var c in this.obligatoryArguments["*"])this.obligatoryArguments["*"].hasOwnProperty(c)&&(b[c]=this.obligatoryArguments["*"][c]);if("object"==typeof this.obligatoryArguments[a])for(c in this.obligatoryArguments[a])this.obligatoryArguments[a].hasOwnProperty(c)&&(b[c]=this.obligatoryArguments[a][c]);return b}; MibewAPIInteraction.prototype.getMandatoryArgumentsDefaults=function(a){var b=this.mandatoryArguments(),d={};if("object"==typeof b["*"])for(var c in b["*"])b["*"].hasOwnProperty(c)&&(d[c]=b["*"][c]);if("object"==typeof b[a])for(c in b[a])b[a].hasOwnProperty(c)&&(d[c]=b[a][c]);return d};function MibewAPIExecutionContext(){this.returnValues={};this.functionsResults=[]}
function MibewAPIExecutionContext(){this.returnValues={};this.functionsResults=[]} MibewAPIExecutionContext.prototype.getArgumentsList=function(a){var b=a.arguments,d=a.arguments.references,c,f,e;for(e in d)if(d.hasOwnProperty(e)){f=d[e];if("undefined"==typeof this.functionsResults[f-1])throw Error("Wrong reference in '"+a["function"]+"' function. Function #"+f+" does not call yet.");if("undefined"==typeof b[e]||""==b[e])throw Error("Wrong reference in '"+a["function"]+"' function. Empty '"+e+"' argument.");c=b[e];if("undefined"==typeof this.functionsResults[f-1][c])throw Error("Wrong reference in '"+
MibewAPIExecutionContext.prototype.getArgumentsList=function(a){var b=a.arguments,c=a.arguments.references,d,f,e;for(e in c)if(c.hasOwnProperty(e)){f=c[e];if("undefined"==typeof this.functionsResults[f-1])throw Error("Wrong reference in '"+a["function"]+"' function. Function #"+f+" does not call yet.");if("undefined"==typeof b[e]||""==b[e])throw Error("Wrong reference in '"+a["function"]+"' function. Empty '"+e+"' argument.");d=b[e];if("undefined"==typeof this.functionsResults[f-1][d])throw Error("Wrong reference in '"+ a["function"]+"' function. There is no '"+c+"' argument in #"+f+" function results");b[e]=this.functionsResults[f-1][c]}return b};MibewAPIExecutionContext.prototype.getResults=function(){return this.returnValues};
a["function"]+"' function. There is no '"+d+"' argument in #"+f+" function results");b[e]=this.functionsResults[f-1][d]}return b};MibewAPIExecutionContext.prototype.getResults=function(){return this.returnValues}; MibewAPIExecutionContext.prototype.storeFunctionResults=function(a,b){var d;if(b.errorCode)this.returnValues.errorCode=b.errorCode,this.returnValues.errorMessage=b.errorMessage||"";else for(var c in a.arguments["return"])if(a.arguments["return"].hasOwnProperty(c)){d=a.arguments["return"][c];if("undefined"==typeof b[c])throw Error("Variable with name '"+c+"' is undefined in the results of the '"+a["function"]+"' function");this.returnValues[d]=b[c]}this.functionsResults.push(b)};
MibewAPIExecutionContext.prototype.storeFunctionResults=function(a,b){var c;if(b.errorCode)this.returnValues.errorCode=b.errorCode,this.returnValues.errorMessage=b.errorMessage||"";else for(var d in a.arguments["return"])if(a.arguments["return"].hasOwnProperty(d)){c=a.arguments["return"][d];if("undefined"==typeof b[d])throw Error("Variable with name '"+d+"' is undefined in the results of the '"+a["function"]+"' function");this.returnValues[c]=b[d]}this.functionsResults.push(b)};

View File

@ -5,4 +5,4 @@
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
*/ */
MibewAPIUsersInteraction=function(){this.obligatoryArguments={"*":{agentId:null,"return":{},references:{}},result:{errorCode:0}};this.getReservedFunctionsNames=function(){return["result"]}};MibewAPIUsersInteraction.prototype=new MibewAPIInteraction; MibewAPIUsersInteraction=function(){this.mandatoryArguments=function(){return{"*":{agentId:null,"return":{},references:{}},result:{errorCode:0}}};this.getReservedFunctionsNames=function(){return["result"]}};MibewAPIUsersInteraction.prototype=new MibewAPIInteraction;

View File

@ -45,7 +45,7 @@ a={hash:{},data:d};return c+=j((f=e.L10n||b.L10n,f?f.call(b,"visitors.table.head
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
*/ */
MibewAPIUsersInteraction=function(){this.obligatoryArguments={"*":{agentId:null,"return":{},references:{}},result:{errorCode:0}};this.getReservedFunctionsNames=function(){return["result"]}};MibewAPIUsersInteraction.prototype=new MibewAPIInteraction; MibewAPIUsersInteraction=function(){this.mandatoryArguments=function(){return{"*":{agentId:null,"return":{},references:{}},result:{errorCode:0}}};this.getReservedFunctionsNames=function(){return["result"]}};MibewAPIUsersInteraction.prototype=new MibewAPIInteraction;
/* /*
Copyright 2005-2013 the original author or authors. Copyright 2005-2013 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License"). Licensed under the Apache License, Version 2.0 (the "License").

View File

@ -12,7 +12,8 @@
* @constructor * @constructor
*/ */
MibewAPIChatInteraction = function() { MibewAPIChatInteraction = function() {
this.obligatoryArguments = { this.mandatoryArguments = function() {
return {
'*': { '*': {
'threadId': null, 'threadId': null,
'token': null, 'token': null,
@ -23,6 +24,7 @@ MibewAPIChatInteraction = function() {
'errorCode': 0 'errorCode': 0
} }
}; };
};
this.getReservedFunctionsNames = function() { this.getReservedFunctionsNames = function() {
return [ return [

View File

@ -71,22 +71,22 @@ MibewAPI.prototype.checkFunction = function(functionObject, filterReservedFuncti
"' function" "' function"
); );
} }
var obligatoryArgumentsCount = 0; var mandatoryArgumentsCount = 0;
var obligatoryArgumentsList = this.interaction.getObligatoryArguments( var mandatoryArgumentsList = this.interaction.getMandatoryArguments(
functionObject['function'] functionObject['function']
); );
argumentsLoop: argumentsLoop:
for (var argName in functionObject.arguments){ for (var argName in functionObject.arguments){
for (var i = 0; i < obligatoryArgumentsList.length; i++) { for (var i = 0; i < mandatoryArgumentsList.length; i++) {
if (argName == obligatoryArgumentsList[i]) { if (argName == mandatoryArgumentsList[i]) {
obligatoryArgumentsCount++; mandatoryArgumentsCount++;
continue argumentsLoop; continue argumentsLoop;
} }
} }
} }
if (obligatoryArgumentsCount != obligatoryArgumentsList.length) { if (mandatoryArgumentsCount != mandatoryArgumentsList.length) {
throw new Error( throw new Error(
"Not all obligatory arguments are set in '" + "Not all mandatory arguments are set in '" +
functionObject["function"] + "' function" functionObject["function"] + "' function"
); );
} }
@ -223,7 +223,7 @@ MibewAPI.prototype.getResultFunction = function(functionsList, existance){
*/ */
MibewAPI.prototype.buildResult = function(resultArguments, token) { MibewAPI.prototype.buildResult = function(resultArguments, token) {
var mergedArguments = resultArguments; var mergedArguments = resultArguments;
var defaultArguments = this.interaction.getObligatoryArgumentsDefaults('result'); var defaultArguments = this.interaction.getMandatoryArgumentsDefaults('result');
for (var argName in defaultArguments) { for (var argName in defaultArguments) {
if (! defaultArguments.hasOwnProperty(argName)) { if (! defaultArguments.hasOwnProperty(argName)) {
continue; continue;
@ -283,15 +283,16 @@ MibewAPI.prototype.decodePackage = function(encodedPackage){
*/ */
function MibewAPIInteraction(){ function MibewAPIInteraction(){
/** /**
* Defines obligatory arguments and default values for them * Defines mandatory arguments and default values for them
* *
* Keys of the array are function names ('*' for all functions). Values are * 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 * arrays of mandatory arguments with key for name of an argument and value
* for default value. * for default value.
* *
* For example: * For example:
* <code> * <code>
* this.obligatoryArguments = { * this.mandatoryArguments = function() {
* return {
* '*': { * '*': {
* 'return': {}, * 'return': {},
* 'references': {} * 'references': {}
@ -300,11 +301,14 @@ function MibewAPIInteraction(){
* 'errorCode': 0 * 'errorCode': 0
* } * }
* } * }
* }
* </code> * </code>
* @type Object * @returns {Object}
* @private * @private
*/ */
this.obligatoryArguments = {}; this.mandatoryArguments = function() {
return {};
}
/** /**
* Returns reserved (system) functions' names * 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 * @param {String} functionName Function name
* @returns {Array} An array of obligatory arguments * @returns {Array} An array of mandatory arguments
*/ */
MibewAPIInteraction.prototype.getObligatoryArguments = function(functionName) { MibewAPIInteraction.prototype.getMandatoryArguments = function(functionName) {
var obligatoryArguments = []; var allMandatoryArguments = this.mandatoryArguments();
// Add obligatory for all functions arguments var mandatoryArguments = [];
if (typeof this.obligatoryArguments['*'] == 'object') { // Add mandatory for all functions arguments
for (var arg in this.obligatoryArguments['*']) { if (typeof allMandatoryArguments['*'] == 'object') {
if (! this.obligatoryArguments['*'].hasOwnProperty(arg)) { for (var arg in allMandatoryArguments['*']) {
if (! allMandatoryArguments['*'].hasOwnProperty(arg)) {
continue; continue;
} }
obligatoryArguments.push(arg); mandatoryArguments.push(arg);
} }
} }
// Add obligatory arguments for given function // Add mandatory arguments for given function
if (typeof this.obligatoryArguments[functionName] == 'object') { if (typeof allMandatoryArguments[functionName] == 'object') {
for (var arg in this.obligatoryArguments[functionName]) { for (var arg in allMandatoryArguments[functionName]) {
if (! this.obligatoryArguments[functionName].hasOwnProperty(arg)) { if (! allMandatoryArguments[functionName].hasOwnProperty(arg)) {
continue; 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 * @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 * values are default values of them
*/ */
MibewAPIInteraction.prototype.getObligatoryArgumentsDefaults = function(functionName) { MibewAPIInteraction.prototype.getMandatoryArgumentsDefaults = function(functionName) {
var obligatoryArguments = {}; var allMandatoryArguments = this.mandatoryArguments();
// Add obligatory for all functions arguments var mandatoryArguments = {};
if (typeof this.obligatoryArguments['*'] == 'object') { // Add mandatory for all functions arguments
for (var arg in this.obligatoryArguments['*']) { if (typeof allMandatoryArguments['*'] == 'object') {
if (! this.obligatoryArguments['*'].hasOwnProperty(arg)) { for (var arg in allMandatoryArguments['*']) {
if (! allMandatoryArguments['*'].hasOwnProperty(arg)) {
continue; continue;
} }
obligatoryArguments[arg] = this.obligatoryArguments['*'][arg]; mandatoryArguments[arg] = allMandatoryArguments['*'][arg];
} }
} }
// Add obligatory arguments for given function // Add mandatory arguments for given function
if (typeof this.obligatoryArguments[functionName] == 'object') { if (typeof allMandatoryArguments[functionName] == 'object') {
for (var arg in this.obligatoryArguments[functionName]) { for (var arg in allMandatoryArguments[functionName]) {
if (! this.obligatoryArguments[functionName].hasOwnProperty(arg)) { if (! allMandatoryArguments[functionName].hasOwnProperty(arg)) {
continue; continue;
} }
obligatoryArguments[arg] = this.obligatoryArguments[functionName][arg]; mandatoryArguments[arg] = allMandatoryArguments[functionName][arg];
} }
} }
return obligatoryArguments; return mandatoryArguments;
} }
/** /**
* End of MibewAPIInteraction class * End of MibewAPIInteraction class

View File

@ -12,7 +12,8 @@
* @constructor * @constructor
*/ */
MibewAPIUsersInteraction = function() { MibewAPIUsersInteraction = function() {
this.obligatoryArguments = { this.mandatoryArguments = function() {
return {
'*': { '*': {
'agentId': null, 'agentId': null,
'return': {}, 'return': {},
@ -22,6 +23,7 @@ MibewAPIUsersInteraction = function() {
'errorCode': 0 'errorCode': 0
} }
}; };
}
this.getReservedFunctionsNames = function() { this.getReservedFunctionsNames = function() {
return [ return [

View File

@ -196,13 +196,13 @@ class API
); );
} }
$unset_arguments = array_diff( $unset_arguments = array_diff(
$this->interaction->getObligatoryArguments($function['function']), $this->interaction->getMandatoryArguments($function['function']),
array_keys($function['arguments']) array_keys($function['arguments'])
); );
if (!empty($unset_arguments)) { if (!empty($unset_arguments)) {
throw new APIException( throw new APIException(
"Arguments '" . implode("', '", $unset_arguments) . "' must be set", "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) public function buildResult($token, $result_arguments)
{ {
$arguments = $result_arguments + $this->interaction->getObligatoryArgumentsDefaults('result'); $arguments = $result_arguments + $this->interaction->getMandatoryArgumentsDefaults('result');
$package = array( $package = array(
'token' => $token, 'token' => $token,
'functions' => array( 'functions' => array(

View File

@ -71,9 +71,9 @@ class APIException extends \Exception
*/ */
const NOT_VALID_JSON = 12; 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 * Request contains more than one result functions
*/ */

View File

@ -34,83 +34,93 @@ abstract class AbstractInteraction
abstract public function getReservedFunctionsNames(); 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). * This method implements "template method" design pattern.
* Values are arrays of obligatory arguments with key for name of an *
* @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. * argument and value for default value.
* *
* For example: * For example:
* <code> * <code>
* protected $obligatoryArguments = array( * protected function mandatoryArguments()
* {
* return array(
* // Mandatory arguments for all functions are:
* '*' => array( * '*' => array(
* // Obligatory arguments for all functions are: * // 'return' with array() by default and
* 'return' => array(), // 'return' with array() by default and * 'return' => array(),
* 'references' => array() // 'references' with array() by default * // 'references' with array() by default
* 'references' => array(),
* ), * ),
* *
* 'result' => array(
* // There is an additional argument for the result function * // 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,
* ),
* ); * );
* }
* </code> * </code>
*/ */
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 * @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(); $all_mandatory_arguments = $this->mandatoryArguments();
// Add obligatory for all functions arguments $mandatory_arguments = array();
if (!empty($this->obligatoryArguments['*'])) { // Add mandatory for all functions arguments
$obligatory_arguments = array_merge( if (!empty($all_mandatory_arguments['*'])) {
$obligatory_arguments, $mandatory_arguments = array_merge(
array_keys($this->obligatoryArguments['*']) $mandatory_arguments,
array_keys($all_mandatory_arguments['*'])
); );
} }
// Add obligatory arguments for given function // Add mandatory arguments for given function
if (!empty($this->obligatoryArguments[$function_name])) { if (!empty($all_mandatory_arguments[$function_name])) {
$obligatory_arguments = array_merge( $mandatory_arguments = array_merge(
$obligatory_arguments, $mandatory_arguments,
array_keys($this->obligatoryArguments[$function_name]) 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 * function
* *
* @param string $function_name Function name * @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 * values are default values of them
*/ */
public function getObligatoryArgumentsDefaults($function_name) public function getMandatoryArgumentsDefaults($function_name)
{ {
$obligatory_arguments = array(); $all_mandatory_arguments = $this->mandatoryArguments();
// Add obligatory for all functions arguments $mandatory_arguments = array();
if (!empty($this->obligatoryArguments['*'])) { // Add mandatory for all functions arguments
$obligatory_arguments = array_merge( if (!empty($all_mandatory_arguments['*'])) {
$obligatory_arguments, $mandatory_arguments = array_merge(
$this->obligatoryArguments['*'] $mandatory_arguments,
$all_mandatory_arguments['*']
); );
} }
// Add obligatory arguments for given function // Add mandatory arguments for given function
if (!empty($this->obligatoryArguments[$function_name])) { if (!empty($all_mandatory_arguments[$function_name])) {
$obligatory_arguments = array_merge( $mandatory_arguments = array_merge(
$obligatory_arguments, $mandatory_arguments,
$this->obligatoryArguments[$function_name] $all_mandatory_arguments[$function_name]
); );
} }
return $obligatory_arguments; return $mandatory_arguments;
} }
} }

View File

@ -36,11 +36,14 @@ class ChatInteraction extends AbstractInteraction
} }
/** /**
* Defines obligatory arguments and default values for them * Defines mandatory arguments and default values for them.
* @var array *
* @see \Mibew\API\Interaction\AbstractInteraction::$obligatoryArgumnents * @return array
* @see \Mibew\API\Interaction\AbstractInteraction::mandatoryArguments
*/ */
protected $obligatoryArguments = array( protected function mandatoryArguments()
{
return array(
'*' => array( '*' => array(
'threadId' => null, 'threadId' => null,
'token' => null, 'token' => null,
@ -51,4 +54,5 @@ class ChatInteraction extends AbstractInteraction
'errorCode' => 0, 'errorCode' => 0,
), ),
); );
}
} }

View File

@ -36,11 +36,14 @@ class InviteInteraction extends AbstractInteraction
} }
/** /**
* Defines obligatory arguments and default values for them * Defines mandatory arguments and default values for them.
* @var array *
* @see \Mibew\API\Interaction\AbstractInteraction::$obligatoryArgumnents * @return array
* @see \Mibew\API\Interaction\AbstractInteraction::mandatoryArguments
*/ */
protected $obligatoryArguments = array( protected function mandatoryArguments()
{
return array(
'*' => array( '*' => array(
'references' => array(), 'references' => array(),
'return' => array(), 'return' => array(),
@ -50,4 +53,5 @@ class InviteInteraction extends AbstractInteraction
'errorCode' => 0, 'errorCode' => 0,
), ),
); );
}
} }

View File

@ -36,11 +36,14 @@ class UsersInteraction extends AbstractInteraction
} }
/** /**
* Defines obligatory arguments and default values for them * Defines mandatory arguments and default values for them.
* @var array *
* @see \Mibew\API\Interaction\AbstractInteraction::$obligatoryArgumnents * @return array
* @see \Mibew\API\Interaction\AbstractInteraction::mandatoryArguments
*/ */
protected $obligatoryArguments = array( protected function mandatoryArguments()
{
return array(
'*' => array( '*' => array(
'agentId' => null, 'agentId' => null,
'references' => array(), 'references' => array(),
@ -53,4 +56,5 @@ class UsersInteraction extends AbstractInteraction
'errorCode' => 0, 'errorCode' => 0,
), ),
); );
}
} }