• Модуль: tasks
  • Путь к файлу: ~/bitrix/modules/tasks/classes/general/restservice.php
  • Класс: CTaskRestService
  • Вызов: CTaskRestService::_parseRestParams
static function _parseRestParams($className, $methodName, $inArgs)
{
	CTaskAssert::assert(is_array($inArgs) && isset(self::$arMethodsMetaInfo[$className][$methodName]));

	$arMethodMetaInfo     = self::$arMethodsMetaInfo[$className][$methodName];
	$arAllowedParams      = $arMethodMetaInfo['params'];
	$mandatoryParamsCount = $arMethodMetaInfo['mandatoryParamsCount'];

	$arDateFields = array();
	if (isset(self::$arManifests[$className]['REST: date fields']))
	{
		$arDateFields = self::$arManifests[$className]['REST: date fields'];
	}

	if ($className == 'ctaskelapseditem' &&
		$methodName == 'getlist' &&
		is_array($inArgs[0]))
	{
		array_unshift($inArgs, 0);
	}

	$outArgs = array();
	foreach ($arAllowedParams as $paramIndex => $paramMetaInfo)
	{
		// No more params given?
		if ( ! array_key_exists($paramIndex, $inArgs) )
		{
			// Set default value, if need
			if (array_key_exists('defaultValue', $paramMetaInfo))
				$inArgs[$paramIndex] = $paramMetaInfo['defaultValue'];
			elseif ($paramIndex < $mandatoryParamsCount)	// Expected mandatory param?
			{
				throw new TasksException(
					'Param #' . $paramIndex . ' (' . $paramMetaInfo['description'] . ')'
					. ' expected by method ' . $className . '::' . $methodName . '(), but not given.',
					TasksException::TE_WRONG_ARGUMENTS
				);
			}
			else
				break;		// no more params to be processed
		}

		// for "galvanic isolation" of input/output
		$paramValue = $inArgs[$paramIndex];

		// Check param type
		/** @noinspection PhpUnusedLocalVariableInspection */
		$isCorrectValue = false;
		switch ($paramMetaInfo['type'])
		{
			case 'boolean':
				if (($paramValue === '0') || ($paramValue === 0))
					$paramValue = false;
				elseif (($paramValue === '1') || ($paramValue === 1))
					$paramValue = true;

				$isCorrectValue = is_bool($paramValue);
			break;

			case 'array':
				$isCorrectValue = is_array($paramValue);
			break;

			case 'string':
				$isCorrectValue = is_string($paramValue);
			break;

			case 'integer':
				$isCorrectValue = CTaskAssert::isLaxIntegers($paramValue);
			break;

			default:
				throw new TasksException(
					'Internal error: unknown param type: ' . $paramMetaInfo['type'],
					TasksException::TE_UNKNOWN_ERROR
				);
			break;
		}

		if ( ! $isCorrectValue )
		{
			throw new TasksException(
				'Param #' . $paramIndex . ' (' . $paramMetaInfo['description'] . ')'
				. ' for method ' . $className . '::' . $methodName . '()'
				. ' expected to be of type "' . $paramMetaInfo['type'] . '",'
				. ' but given something else.',
				TasksException::TE_WRONG_ARGUMENTS
			);
		}

		// add legal aggregated columns in keys & values array
		if(isset($paramMetaInfo['allowedAggregations']) && is_array($paramMetaInfo['allowedAggregations']))
		{
			// for keys
			if(is_array($paramMetaInfo['allowedKeysInAggregation']))
				$fields = $paramMetaInfo['allowedKeysInAggregation'];
			else
				$fields = $paramMetaInfo['allowedKeys'];

			if(is_array($fields))
			{
				$aggrCombos = static::getAllowedAggregateCombos($paramMetaInfo['allowedAggregations'], $fields);
				$paramMetaInfo['allowedKeys'] = array_merge($paramMetaInfo['allowedKeys'], $aggrCombos);
			}

			// for values
			if(is_array($paramMetaInfo['allowedValuesInAggregation']))
				$fields = $paramMetaInfo['allowedValuesInAggregation'];
			else
				$fields = $paramMetaInfo['allowedValues'];

			if(is_array($fields))
			{
				$aggrCombos = static::getAllowedAggregateCombos($paramMetaInfo['allowedAggregations'], $fields);
				$paramMetaInfo['allowedValues'] = array_merge($paramMetaInfo['allowedValues'], $aggrCombos);
			}
		}

		if (isset($paramMetaInfo['allowedKeys']))
		{
			CTaskAssert::assert(is_array($paramValue));	// ensure that $paramValue is array
			/** @var $paramValue array */

			foreach (array_keys($paramValue) as $key)
			{
				// a little fix to be able to pass an empty array in order to "skip" argument
				if((string) $key == '0' && $paramValue[$key] == '')
				{
					unset($paramValue[$key]);
					continue;
				}

				if (isset($paramMetaInfo['allowedKeyPrefixes']))
					$keyWoPrefix = str_replace($paramMetaInfo['allowedKeyPrefixes'], '', $key);
				else
					$keyWoPrefix = $key;

				if ( ! in_array((string) $keyWoPrefix, $paramMetaInfo['allowedKeys'], true) )
				{
					throw new TasksException(
						'Param #' . $paramIndex . ' (' . $paramMetaInfo['description'] . ')'
						. ' for method ' . $className . '::' . $methodName . '()'
						. ' must not contain key "' . $key . '".',
						TasksException::TE_WRONG_ARGUMENTS
					);
				}

				// Additionally convert datetime fields from ISO 8601
				if (in_array((string) $keyWoPrefix, $arDateFields, true) && !in_array($paramValue[$key], array('asc', 'desc'))/*it could be sorting*/)
				{
					$paramValue[$key] = (string) CRestUtil::unConvertDateTime($paramValue[$key], true);
				}
			}
		}

		if (isset($paramMetaInfo['allowedValues']))
		{
			CTaskAssert::assert(is_array($paramValue));

			foreach ($paramValue as $value)
			{
				if ( ($value !== null) && ( ! is_bool($value) ) )
					$value = (string) $value;

				if ( ! in_array($value, $paramMetaInfo['allowedValues'], true) )
				{
					throw new TasksException(
						'Param #' . $paramIndex . ' (' . $paramMetaInfo['description'] . ')'
						. ' for method ' . $className . '::' . $methodName . '()'
						. ' must not contain value "' . $value . '".',
						TasksException::TE_WRONG_ARGUMENTS
					);
				}
			}
		}

		// "galvanic isolation" of input/output
		$outArgs[] = $paramValue;
	}

	// $inArgsCount = count($inArgs);
	// $allowedArgsCount = count($arAllowedParams);
	//
	// if ($inArgsCount > $allowedArgsCount)
	// {
	// 	throw new TasksException(
	// 		"Too much params({$inArgsCount}) given for method {$className}::{$methodName}(), but expected not more than {$allowedArgsCount}.",
	// 		TasksException::TE_WRONG_ARGUMENTS
	// 	);
	// }

	return $outArgs;
}