mirror of
https://github.com/Mibew/mibew.git
synced 2025-02-01 05:44:41 +03:00
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:
parent
26b2d74cc4
commit
b5778a8be2
@ -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;
|
||||
|
@ -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").
|
||||
|
@ -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<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=
|
||||
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])};
|
||||
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;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");
|
||||
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};
|
||||
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.decodePackage=function(a){a=JSON.parse(decodeURIComponent(a.replace(/\+/gi," ")));this.checkPackage(a);return a};function MibewAPIInteraction(){this.obligatoryArguments={};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.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};
|
||||
function MibewAPIExecutionContext(){this.returnValues={};this.functionsResults=[]}
|
||||
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 '"+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 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)};
|
||||
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 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.mandatoryArguments=function(){return{}};this.getReservedFunctionsNames=function(){return[]}}
|
||||
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.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=[]}
|
||||
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 '"+
|
||||
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};
|
||||
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)};
|
||||
|
@ -5,4 +5,4 @@
|
||||
You may obtain a copy of the License at
|
||||
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;
|
||||
|
@ -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
|
||||
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.
|
||||
Licensed under the Apache License, Version 2.0 (the "License").
|
||||
|
@ -12,16 +12,18 @@
|
||||
* @constructor
|
||||
*/
|
||||
MibewAPIChatInteraction = function() {
|
||||
this.obligatoryArguments = {
|
||||
'*': {
|
||||
'threadId': null,
|
||||
'token': null,
|
||||
'return': {},
|
||||
'references': {}
|
||||
},
|
||||
'result': {
|
||||
'errorCode': 0
|
||||
}
|
||||
this.mandatoryArguments = function() {
|
||||
return {
|
||||
'*': {
|
||||
'threadId': null,
|
||||
'token': null,
|
||||
'return': {},
|
||||
'references': {}
|
||||
},
|
||||
'result': {
|
||||
'errorCode': 0
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
this.getReservedFunctionsNames = function() {
|
||||
|
@ -71,22 +71,22 @@ MibewAPI.prototype.checkFunction = function(functionObject, filterReservedFuncti
|
||||
"' function"
|
||||
);
|
||||
}
|
||||
var obligatoryArgumentsCount = 0;
|
||||
var obligatoryArgumentsList = this.interaction.getObligatoryArguments(
|
||||
var mandatoryArgumentsCount = 0;
|
||||
var mandatoryArgumentsList = this.interaction.getMandatoryArguments(
|
||||
functionObject['function']
|
||||
);
|
||||
argumentsLoop:
|
||||
for (var argName in functionObject.arguments){
|
||||
for (var i = 0; i < obligatoryArgumentsList.length; i++) {
|
||||
if (argName == obligatoryArgumentsList[i]) {
|
||||
obligatoryArgumentsCount++;
|
||||
for (var i = 0; i < mandatoryArgumentsList.length; i++) {
|
||||
if (argName == mandatoryArgumentsList[i]) {
|
||||
mandatoryArgumentsCount++;
|
||||
continue argumentsLoop;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (obligatoryArgumentsCount != obligatoryArgumentsList.length) {
|
||||
if (mandatoryArgumentsCount != mandatoryArgumentsList.length) {
|
||||
throw new Error(
|
||||
"Not all obligatory arguments are set in '" +
|
||||
"Not all mandatory arguments are set in '" +
|
||||
functionObject["function"] + "' function"
|
||||
);
|
||||
}
|
||||
@ -223,7 +223,7 @@ MibewAPI.prototype.getResultFunction = function(functionsList, existance){
|
||||
*/
|
||||
MibewAPI.prototype.buildResult = function(resultArguments, token) {
|
||||
var mergedArguments = resultArguments;
|
||||
var defaultArguments = this.interaction.getObligatoryArgumentsDefaults('result');
|
||||
var defaultArguments = this.interaction.getMandatoryArgumentsDefaults('result');
|
||||
for (var argName in defaultArguments) {
|
||||
if (! defaultArguments.hasOwnProperty(argName)) {
|
||||
continue;
|
||||
@ -283,28 +283,32 @@ MibewAPI.prototype.decodePackage = function(encodedPackage){
|
||||
*/
|
||||
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
|
||||
* 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 example:
|
||||
* <code>
|
||||
* this.obligatoryArguments = {
|
||||
* '*': {
|
||||
* 'return': {},
|
||||
* 'references': {}
|
||||
* },
|
||||
* 'result': {
|
||||
* 'errorCode': 0
|
||||
* this.mandatoryArguments = function() {
|
||||
* return {
|
||||
* '*': {
|
||||
* 'return': {},
|
||||
* 'references': {}
|
||||
* },
|
||||
* 'result': {
|
||||
* 'errorCode': 0
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* </code>
|
||||
* @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
|
||||
|
@ -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 [
|
||||
|
@ -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(
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -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:
|
||||
* <code>
|
||||
* 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,
|
||||
* ),
|
||||
* );
|
||||
* }
|
||||
* </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
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user