Split mibew_api.php into several files

This commit is contained in:
Dmitriy Simushev 2012-08-22 10:01:22 +00:00
parent 2f18eb65b1
commit 41d40e3c04
6 changed files with 283 additions and 252 deletions

View File

@ -1,6 +1,6 @@
<?php <?php
require_once dirname(__FILE__) . '/../../../webim/libs/mibew_api.php'; require_once dirname(__FILE__) . '/../../../webim/libs/classes/mibew_api_execution_context.php';
/** /**
* Test class for MibewAPIExecutionContext. * Test class for MibewAPIExecutionContext.

View File

@ -1,6 +1,8 @@
<?php <?php
require_once dirname(__FILE__) . '/../../../webim/libs/mibew_api.php'; require_once dirname(__FILE__) . '/../../../webim/libs/classes/mibew_api.php';
require_once dirname(__FILE__) . '/../../../webim/libs/classes/mibew_api_interaction.php';
require_once dirname(__FILE__) . '/../../../webim/libs/classes/mibew_api_window_interaction.php';
/** /**
* Test class for MibewAPI. * Test class for MibewAPI.
@ -9,15 +11,15 @@ require_once dirname(__FILE__) . '/../../../webim/libs/mibew_api.php';
class MibewAPITest extends PHPUnit_Framework_TestCase { class MibewAPITest extends PHPUnit_Framework_TestCase {
public function testGetAPI() { public function testGetAPI() {
$mibew_api = MibewAPI::getAPI('base'); $mibew_api = MibewAPI::getAPI('MibewAPIWindowInteraction');
$this->assertEquals($mibew_api, MibewAPI::getAPI('base')); $this->assertEquals($mibew_api, MibewAPI::getAPI('MibewAPIWindowInteraction'));
} }
/** /**
* @depends testGetAPI * @depends testGetAPI
*/ */
public function testCheckFunction() { public function testCheckFunction() {
$api = MibewAPI::getAPI('base'); $api = MibewAPI::getAPI('MibewAPIWindowInteraction');
// Wrong function. Function name is absent // Wrong function. Function name is absent
$wrong_function = array(); $wrong_function = array();
@ -165,7 +167,7 @@ class MibewAPITest extends PHPUnit_Framework_TestCase {
* @depends testCheckFunction * @depends testCheckFunction
*/ */
public function testCheckRequest($correct_function) { public function testCheckRequest($correct_function) {
$api = MibewAPI::getAPI('base'); $api = MibewAPI::getAPI('MibewAPIWindowInteraction');
// Wrong request. Its 'token' element is absent. // Wrong request. Its 'token' element is absent.
$wrong_request = array(); $wrong_request = array();
@ -243,7 +245,7 @@ class MibewAPITest extends PHPUnit_Framework_TestCase {
* @depends testCheckRequest * @depends testCheckRequest
*/ */
public function testCheckPackage($correct_request) { public function testCheckPackage($correct_request) {
$api = MibewAPI::getAPI('base'); $api = MibewAPI::getAPI('MibewAPIWindowInteraction');
$trusted_signatures = array('some_trusted_signature'); $trusted_signatures = array('some_trusted_signature');
// Wrong package. Signature element is absent. // Wrong package. Signature element is absent.
@ -415,7 +417,7 @@ class MibewAPITest extends PHPUnit_Framework_TestCase {
* @depends testCheckPackage * @depends testCheckPackage
*/ */
public function testEncodePackage($correct_package) { public function testEncodePackage($correct_package) {
$api = MibewAPI::getAPI('base'); $api = MibewAPI::getAPI('MibewAPIWindowInteraction');
// Get package values // Get package values
$requests = $correct_package['requests']; $requests = $correct_package['requests'];
@ -437,7 +439,7 @@ class MibewAPITest extends PHPUnit_Framework_TestCase {
* @depends testEncodePackage * @depends testEncodePackage
*/ */
public function testDecodePackage($vars) { public function testDecodePackage($vars) {
$api = MibewAPI::getAPI('base'); $api = MibewAPI::getAPI('MibewAPIWindowInteraction');
// Try to catch MibewAPIException with MibewAPIException::NOT_VALID_JSON code // Try to catch MibewAPIException with MibewAPIException::NOT_VALID_JSON code
try { try {
@ -470,7 +472,7 @@ class MibewAPITest extends PHPUnit_Framework_TestCase {
* @depends testGetAPI * @depends testGetAPI
*/ */
public function testGetResultFunction() { public function testGetResultFunction() {
$api = MibewAPI::getAPI('base'); $api = MibewAPI::getAPI('MibewAPIWindowInteraction');
// Wrong functions list. More than one result function. // Wrong functions list. More than one result function.
$functions_list = array( $functions_list = array(
@ -578,7 +580,7 @@ class MibewAPITest extends PHPUnit_Framework_TestCase {
* @depends testGetAPI * @depends testGetAPI
*/ */
public function testBuildResult() { public function testBuildResult() {
$api = MibewAPI::getAPI('base'); $api = MibewAPI::getAPI('MibewAPIWindowInteraction');
$token = 'some_test_token'; $token = 'some_test_token';
$package = array( $package = array(

View File

@ -15,6 +15,8 @@
* limitations under the License. * limitations under the License.
*/ */
//require_once('mibew_api_interaction.php');
/** /**
* Implements Mibew API specification version 1.0 * Implements Mibew API specification version 1.0
* *
@ -43,31 +45,30 @@ Class MibewAPI {
/** /**
* Returns MibewAPI object * Returns MibewAPI object
* *
* @param string $interaction_type A name of the interaction type * @param string $class_name A name of the interaction type class
* @return MibeAPI object * @return MibeAPI object
* @throws MibewAPIException * @throws MibewAPIException
*/ */
public static function getAPI($interaction_type) { public static function getAPI($class_name) {
$class_name = "MibewAPI". ucfirst($interaction_type) . "Interaction";
if (! class_exists($class_name)) { if (! class_exists($class_name)) {
throw new MibewAPIException( throw new MibewAPIException(
"Wrong interaction type", "Wrong interaction type",
MibewAPIException::WRONG_INTERACTION_TYPE MibewAPIException::WRONG_INTERACTION_TYPE
); );
} }
if (empty(self::$interactions[$interaction_type])) { if (empty(self::$interactions[$class_name])) {
self::$interactions[$interaction_type] = new self(new $class_name()); self::$interactions[$class_name] = new self(new $class_name());
} }
return self::$interactions[$interaction_type]; return self::$interactions[$class_name];
} }
/** /**
* Class constructor * Class constructor
* *
* @param MibewAPIInteraction $interaction_type Interaction type object * @param MibewAPIInteraction $interaction Interaction type object
*/ */
protected function __construct(MibewAPIInteraction $interaction_type) { protected function __construct(MibewAPIInteraction $interaction) {
$this->interaction = $interaction_type; $this->interaction = $interaction;
} }
/** /**
@ -332,238 +333,6 @@ Class MibewAPI {
} }
} }
/**
* Implements functions execution context
*/
Class MibewAPIExecutionContext {
/**
* Values which returns after execution of all functions in request
* @var array
*/
protected $return = array();
/**
* Results of execution of all function in request
* @var array
*/
protected $functions_results = array();
/**
* Returns requets results
*
* @return array Request results
* @see MibewAPIExecutionContext::$return
*/
public function getResults () {
return $this->return;
}
/**
* Build arguments list by replace all references by values of execution context
*
* @param array $function Function array. See MibewAPI for details.
* @return array Arguments list
* @throws MibewAPIException
*/
public function getArgumentsList ($function) {
$arguments = $function['arguments'];
$references = $function['arguments']['references'];
foreach ($references as $variable => $func_num) {
// Check target function in context
if (! isset($this->functions_results[$func_num - 1])) {
// Wrong function num
throw new MibewAPIException(
"Wrong reference in '{$function['function']}' function. " .
"Function #{$func_num} does not call yet.",
MibewAPIException::WRONG_FUNCTION_NUM_IN_REFERENCE
);
}
// Check reference
if (empty($arguments[$variable])) {
// Empty argument that should contains reference
throw new MibewAPIException(
"Wrong reference in '{$function['function']}' function. " .
"Empty {$variable} argument.",
MibewAPIException::EMPTY_VARIABLE_IN_REFERENCE
);
}
$reference_to = $arguments[$variable];
// Check target value
if (! isset($this->functions_results[$func_num - 1][$reference_to])) {
// Undefined target value
throw new MibewAPIException(
"Wrong reference in '{$function['function']}' function. " .
"There is no '{$reference_to}' argument in #{$func_num} " .
"function results",
MibewAPIException::VARIABLE_IS_UNDEFINED_IN_REFERENCE
);
}
// Replace reference by target value
$arguments[$variable] = $this->functions_results[$func_num - 1][$reference_to];
}
return $arguments;
}
/**
* Stores functions results in execution context and add values to request result
*
* @param array $function Function array. See MibewAPI for details.
* @param array $results Associative array of the function results.
* @throws MibewAPIException
*/
public function storeFunctionResults ($function, $results) {
// Add value to request results
foreach ($function['arguments']['return'] as $name => $alias) {
if (! isset($results[$name])) {
// Value that defined in 'return' argument is undefined
throw new MibewAPIException(
"Variable with name '{$name}' is undefined in the " .
"results of the '{$function['function']}' function",
MibewAPIException::VARIABLE_IS_UNDEFINED_IN_RESULT
);
}
$this->return[$alias] = $results[$name];
}
// Store function results in execution context
$this->functions_results[] = $results;
}
}
/**
* Encapsulates interaction type
*/
abstract class MibewAPIInteraction {
/**
* 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:
* <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
* ),
* 'result' => array( // There is an additional argument for the result function
* 'errorCode' => 0 // This is 'error_code' with 0 by default
* )
* );
* </code>
*/
protected $obligatoryArguments = array();
/**
* 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
*/
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 {
/**
* 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'
);
}
/**
* 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'
);
}
/** /**
* Mibew API Exception class. * Mibew API Exception class.
*/ */

View File

@ -0,0 +1,119 @@
<?php
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Implements functions execution context
*/
Class MibewAPIExecutionContext {
/**
* Values which returns after execution of all functions in request
* @var array
*/
protected $return = array();
/**
* Results of execution of all function in request
* @var array
*/
protected $functions_results = array();
/**
* Returns requets results
*
* @return array Request results
* @see MibewAPIExecutionContext::$return
*/
public function getResults () {
return $this->return;
}
/**
* Build arguments list by replace all references by values of execution context
*
* @param array $function Function array. See MibewAPI for details.
* @return array Arguments list
* @throws MibewAPIException
*/
public function getArgumentsList ($function) {
$arguments = $function['arguments'];
$references = $function['arguments']['references'];
foreach ($references as $variable => $func_num) {
// Check target function in context
if (! isset($this->functions_results[$func_num - 1])) {
// Wrong function num
throw new MibewAPIException(
"Wrong reference in '{$function['function']}' function. " .
"Function #{$func_num} does not call yet.",
MibewAPIException::WRONG_FUNCTION_NUM_IN_REFERENCE
);
}
// Check reference
if (empty($arguments[$variable])) {
// Empty argument that should contains reference
throw new MibewAPIException(
"Wrong reference in '{$function['function']}' function. " .
"Empty {$variable} argument.",
MibewAPIException::EMPTY_VARIABLE_IN_REFERENCE
);
}
$reference_to = $arguments[$variable];
// Check target value
if (! isset($this->functions_results[$func_num - 1][$reference_to])) {
// Undefined target value
throw new MibewAPIException(
"Wrong reference in '{$function['function']}' function. " .
"There is no '{$reference_to}' argument in #{$func_num} " .
"function results",
MibewAPIException::VARIABLE_IS_UNDEFINED_IN_REFERENCE
);
}
// Replace reference by target value
$arguments[$variable] = $this->functions_results[$func_num - 1][$reference_to];
}
return $arguments;
}
/**
* Stores functions results in execution context and add values to request result
*
* @param array $function Function array. See MibewAPI for details.
* @param array $results Associative array of the function results.
* @throws MibewAPIException
*/
public function storeFunctionResults ($function, $results) {
// Add value to request results
foreach ($function['arguments']['return'] as $name => $alias) {
if (! isset($results[$name])) {
// Value that defined in 'return' argument is undefined
throw new MibewAPIException(
"Variable with name '{$name}' is undefined in the " .
"results of the '{$function['function']}' function",
MibewAPIException::VARIABLE_IS_UNDEFINED_IN_RESULT
);
}
$this->return[$alias] = $results[$name];
}
// Store function results in execution context
$this->functions_results[] = $results;
}
}
?>

View File

@ -0,0 +1,97 @@
<?php
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Encapsulates interaction type
*/
abstract class MibewAPIInteraction {
/**
* 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:
* <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
* ),
* 'result' => array( // There is an additional argument for the result function
* 'errorCode' => 0 // This is 'error_code' with 0 by default
* )
* );
* </code>
*/
protected $obligatoryArguments = array();
/**
* 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
*/
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;
}
}
?>

View File

@ -0,0 +1,44 @@
<?php
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* 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'
);
}
?>