<commit_before><?php
/*
 * @copyright Copyright (c) 2017, Afterlogic Corp.
 * @license AGPL-3.0 or Afterlogic Software License
 *
 * This code is licensed under AGPLv3 license or Afterlogic Software License
 * if commercial version of the product was purchased.
 * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
 */

namespace Aurora\System\Managers;

/**
 * @package Api
 */
class Response
{
	protected static $sMethod = null;

	public static $objectNames = array(
			'Aurora\Modules\Mail\Classes\MessageCollection' => 'MessageCollection',
			'Aurora\Modules\Mail\Classes\Message' => 'Message',
			'Aurora\Modules\Mail\Classes\FolderCollection' => 'FolderCollection',
			'Aurora\Modules\Mail\Classes\Folder' => 'Folder'
	);

	public static function GetMethod()
	{
		return  self::$sMethod;
	}
	
	public static function SetMethod($sMethod)
	{
		self::$sMethod = $sMethod;
	}

	/**
	 * @param string $sObjectName
	 *
	 * @return string
	 */
	public static function GetObjectName($sObjectName)
	{
		return !empty(self::$objectNames[$sObjectName]) ? self::$objectNames[$sObjectName] : $sObjectName;
	}
	
	/**
	 * @param object $oData
	 *
	 * @return array | false
	 */
	public static function objectWrapper($oData, $aParameters = array())
	{
		$mResult = false;
		if (\is_object($oData))
		{
//			$aNames = \explode('\\', \get_class($oData));
//			$sObjectName = end($aNames);
			$sObjectName = \get_class($oData);
			$mResult = array(
				'@Object' => self::GetObjectName($sObjectName)
			);			

			if ($oData instanceof \MailSo\Base\Collection)
			{
				$mResult['@Object'] = 'Collection/'.$mResult['@Object'];
				$mResult['@Count'] = $oData->Count();
				$mResult['@Collection'] = self::GetResponseObject($oData->CloneAsArray(), $aParameters);
			}
			else
			{
				$mResult['@Object'] = 'Object/'.$mResult['@Object'];
			}
		}

		return $mResult;
	}
	
	/**
	 * @param mixed $mResponse
	 *
	 * @return mixed
	 */
	public static function GetResponseObject($mResponse, $aParameters = array())
	{
		$mResult = null;

		if (\is_object($mResponse))
		{
			if (\method_exists($mResponse, 'toResponseArray'))	
			{
				$aArgs = [$mResponse, $aParameters];
				$this->broadcastEvent(
					'System', 
					'toResponseArray' . AbstractModule::$Delimiter . 'before', 
					$aArgs
				);

				$mResult = \array_merge(self::objectWrapper($mResponse, $aParameters), $mResponse->toResponseArray($aParameters));

				$this->broadcastEvent(
					'System', 
					'toResponseArray' . AbstractModule::$Delimiter . 'after', 
					$aArgs,
					$mResult
				);			
			}
			else
			{
				$mResult = \array_merge(self::objectWrapper($mResponse, $aParameters), self::CollectionToResponseArray($mResponse, $aParameters));
			}<commit_msg>added 'System::toResponseArray::before' and System::toResponseArray::after'  events<commit_after><?php
/*
 * @copyright Copyright (c) 2017, Afterlogic Corp.
 * @license AGPL-3.0 or Afterlogic Software License
 *
 * This code is licensed under AGPLv3 license or Afterlogic Software License
 * if commercial version of the product was purchased.
 * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
 */

namespace Aurora\System\Managers;

/**
 * @package Api
 */
class Response
{
	protected static $sMethod = null;

	public static $objectNames = array(
			'Aurora\Modules\Mail\Classes\MessageCollection' => 'MessageCollection',
			'Aurora\Modules\Mail\Classes\Message' => 'Message',
			'Aurora\Modules\Mail\Classes\FolderCollection' => 'FolderCollection',
			'Aurora\Modules\Mail\Classes\Folder' => 'Folder'
	);

	public static function GetMethod()
	{
		return  self::$sMethod;
	}
	
	public static function SetMethod($sMethod)
	{
		self::$sMethod = $sMethod;
	}

	/**
	 * @param string $sObjectName
	 *
	 * @return string
	 */
	public static function GetObjectName($sObjectName)
	{
		return !empty(self::$objectNames[$sObjectName]) ? self::$objectNames[$sObjectName] : $sObjectName;
	}
	
	/**
	 * @param object $oData
	 *
	 * @return array | false
	 */
	public static function objectWrapper($oData, $aParameters = array())
	{
		$mResult = false;
		if (\is_object($oData))
		{
//			$aNames = \explode('\\', \get_class($oData));
//			$sObjectName = end($aNames);
			$sObjectName = \get_class($oData);
			$mResult = array(
				'@Object' => self::GetObjectName($sObjectName)
			);			

			if ($oData instanceof \MailSo\Base\Collection)
			{
				$mResult['@Object'] = 'Collection/'.$mResult['@Object'];
				$mResult['@Count'] = $oData->Count();
				$mResult['@Collection'] = self::GetResponseObject($oData->CloneAsArray(), $aParameters);
			}
			else
			{
				$mResult['@Object'] = 'Object/'.$mResult['@Object'];
			}
		}

		return $mResult;
	}
	
	/**
	 * @param mixed $mResponse
	 *
	 * @return mixed
	 */
	public static function GetResponseObject($mResponse, $aParameters = array())
	{
		$mResult = null;

		if (\is_object($mResponse))
		{
			if (\method_exists($mResponse, 'toResponseArray'))	
			{
				$aArgs = [$mResponse, $aParameters];
				\Aurora\System\Api::GetModuleManager()->broadcastEvent(
					'System', 
					'toResponseArray' . AbstractModule::$Delimiter . 'before', 
					$aArgs
				);

				$mResult = \array_merge(self::objectWrapper($mResponse, $aParameters), $mResponse->toResponseArray($aParameters));

				\Aurora\System\Api::GetModuleManager()->broadcastEvent(
					'System', 
					'toResponseArray' . AbstractModule::$Delimiter . 'after', 
					$aArgs,
					$mResult
				);			
			}
			else
			{
				$mResult = \array_merge(self::objectWrapper($mResponse, $aParameters), self::CollectionToResponseArray($mResponse, $aParameters));
			}
{'fraction_non_alphanumeric': 0.1592684659090909, 'fraction_numerical': 0.002840909090909091, 'mean_word_length': 3.8812824956672443, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3702495', 'n_tokens_mistral': 2119, 'n_tokens_neox': 1841, 'n_words': 358}