
#ifdef HAVE_CONFIG_H
#include "../../ext_config.h"
#endif

#include <php.h>
#include "../../php_ext.h"
#include "../../ext.h"

#include <Zend/zend_operators.h>
#include <Zend/zend_exceptions.h>
#include <Zend/zend_interfaces.h>

#include "kernel/main.h"
#include "ext/json/php_json.h"
#include "kernel/memory.h"
#include "kernel/object.h"
#include "kernel/operators.h"
#include "kernel/array.h"


/**
 * This file is part of the Phalcon Framework.
 *
 * (c) Phalcon Team <team@phalcon.io>
 *
 * For the full copyright and license information, please view the LICENSE.txt
 * file that was distributed with this source code.
 */
/**
 * Class Message
 *
 * Stores a message from various components
 *
 * @phpstan-import-type messages_message from MessagesTypes
 * @phpstan-import-type messages_metadata from MessagesTypes
 */
ZEPHIR_INIT_CLASS(Phalcon_Messages_Message)
{
	ZEPHIR_REGISTER_CLASS(Phalcon\\Messages, Message, phalcon, messages_message, phalcon_messages_message_method_entry, 0);

	{
		zval _zc0;
		ZVAL_LONG(&_zc0, 0);
		zephir_declare_typed_property(phalcon_messages_message_ce, SL("code"), &_zc0, ZEND_ACC_PROTECTED, MAY_BE_LONG, NULL, 0);
	}

	{
		zval _zc0;
		ZVAL_STRINGL(&_zc0, "", sizeof("") - 1);
		zephir_declare_typed_property(phalcon_messages_message_ce, SL("field"), &_zc0, ZEND_ACC_PROTECTED, MAY_BE_STRING, NULL, 0);
	}

	{
		zval _zc0;
		ZVAL_UNDEF(&_zc0);
		zephir_declare_typed_property(phalcon_messages_message_ce, SL("message"), &_zc0, ZEND_ACC_PROTECTED, MAY_BE_STRING, NULL, 0);
	}

	{
		zval _zc0;
		array_init_size(&_zc0, 1);
		zephir_declare_typed_property(phalcon_messages_message_ce, SL("metaData"), &_zc0, ZEND_ACC_PROTECTED, MAY_BE_ARRAY, NULL, 0);
	}

	{
		zval _zc0;
		ZVAL_STRINGL(&_zc0, "", sizeof("") - 1);
		zephir_declare_typed_property(phalcon_messages_message_ce, SL("type"), &_zc0, ZEND_ACC_PROTECTED, MAY_BE_STRING, NULL, 0);
	}

	zend_class_implements(phalcon_messages_message_ce, 1, phalcon_messages_messageinterface_ce);
	zend_class_implements(phalcon_messages_message_ce, 1, php_json_serializable_ce);
	return SUCCESS;
}

/**
 * Phalcon\Messages\Message constructor
 *
 * @param messages_metadata $metaData
 */
PHP_METHOD(Phalcon_Messages_Message, __construct)
{
	zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
	zval metaData;
	zend_long code;
	zval message_zv, field_zv, type_zv, *code_param = NULL, *metaData_param = NULL, _0;
	zend_string *message = NULL, *field = NULL, *type = NULL;
	zval *this_ptr = getThis();

	ZVAL_UNDEF(&message_zv);
	ZVAL_UNDEF(&field_zv);
	ZVAL_UNDEF(&type_zv);
	ZVAL_UNDEF(&_0);
	ZVAL_UNDEF(&metaData);
	static zend_string *_zephir_prop_0 = NULL;
	static zend_string *_zephir_prop_1 = NULL;
	static zend_string *_zephir_prop_2 = NULL;
	static zend_string *_zephir_prop_3 = NULL;
	static zend_string *_zephir_prop_4 = NULL;
	if (UNEXPECTED(!_zephir_prop_0)) {
		_zephir_prop_0 = zend_string_init("message", 7, 1);
	}
	if (UNEXPECTED(!_zephir_prop_1)) {
		_zephir_prop_1 = zend_string_init("field", 5, 1);
	}
	if (UNEXPECTED(!_zephir_prop_2)) {
		_zephir_prop_2 = zend_string_init("type", 4, 1);
	}
	if (UNEXPECTED(!_zephir_prop_3)) {
		_zephir_prop_3 = zend_string_init("code", 4, 1);
	}
	if (UNEXPECTED(!_zephir_prop_4)) {
		_zephir_prop_4 = zend_string_init("metaData", 8, 1);
	}

	ZEND_PARSE_PARAMETERS_START(1, 5)
		Z_PARAM_STR(message)
		Z_PARAM_OPTIONAL
		Z_PARAM_STR(field)
		Z_PARAM_STR(type)
		Z_PARAM_LONG(code)
		ZEPHIR_Z_PARAM_ARRAY(metaData, metaData_param)
	ZEND_PARSE_PARAMETERS_END();
	ZEPHIR_METHOD_GLOBALS_PTR = pecalloc(1, sizeof(zephir_method_globals), 0);
	zephir_memory_grow_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__);
	if (ZEND_NUM_ARGS() > 3) {
		code_param = ZEND_CALL_ARG(execute_data, 4);
	}
	if (ZEND_NUM_ARGS() > 4) {
		metaData_param = ZEND_CALL_ARG(execute_data, 5);
	}
	zephir_memory_observe(&message_zv);
	ZVAL_STR_COPY(&message_zv, message);
	if (!field) {
		field = zend_string_init(ZEND_STRL(""), 0);
		zephir_memory_observe(&field_zv);
		ZVAL_STR(&field_zv, field);
	} else {
		zephir_memory_observe(&field_zv);
	ZVAL_STR_COPY(&field_zv, field);
	}
	if (!type) {
		type = zend_string_init(ZEND_STRL(""), 0);
		zephir_memory_observe(&type_zv);
		ZVAL_STR(&type_zv, type);
	} else {
		zephir_memory_observe(&type_zv);
	ZVAL_STR_COPY(&type_zv, type);
	}
	if (!code_param) {
		code = 0;
	} else {
		}
	if (!metaData_param) {
		ZEPHIR_INIT_VAR(&metaData);
		array_init(&metaData);
	} else {
		zephir_get_arrval(&metaData, metaData_param);
	}
	zephir_update_property_zval_cached(this_ptr, _zephir_prop_0, 956, &message_zv);
	zephir_update_property_zval_cached(this_ptr, _zephir_prop_1, 957, &field_zv);
	zephir_update_property_zval_cached(this_ptr, _zephir_prop_2, 958, &type_zv);
	ZVAL_UNDEF(&_0);
	ZVAL_LONG(&_0, code);
	zephir_update_property_zval_cached(this_ptr, _zephir_prop_3, 959, &_0);
	zephir_update_property_zval_cached(this_ptr, _zephir_prop_4, 960, &metaData);
	ZEPHIR_MM_RESTORE();
}

/**
 * Magic __toString method returns verbose message
 */
PHP_METHOD(Phalcon_Messages_Message, __toString)
{

	RETURN_MEMBER_TYPED(getThis(), "message", IS_STRING);
}

/**
 * @return int
 */
PHP_METHOD(Phalcon_Messages_Message, getCode)
{

	RETURN_MEMBER_TYPED(getThis(), "code", IS_LONG);
}

/**
 * @return string
 */
PHP_METHOD(Phalcon_Messages_Message, getField)
{

	RETURN_MEMBER_TYPED(getThis(), "field", IS_STRING);
}

/**
 * @return string
 */
PHP_METHOD(Phalcon_Messages_Message, getMessage)
{

	RETURN_MEMBER_TYPED(getThis(), "message", IS_STRING);
}

/**
 * @return messages_metadata
 */
PHP_METHOD(Phalcon_Messages_Message, getMetaData)
{

	RETURN_MEMBER_TYPED(getThis(), "metaData", IS_ARRAY);
}

/**
 * @return string
 */
PHP_METHOD(Phalcon_Messages_Message, getType)
{

	RETURN_MEMBER_TYPED(getThis(), "type", IS_STRING);
}

/**
 * Serializes the object for json_encode
 *
 * @return messages_message
 */
PHP_METHOD(Phalcon_Messages_Message, jsonSerialize)
{
	zval _0;
	zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
	zval *this_ptr = getThis();

	ZVAL_UNDEF(&_0);
	static zend_string *_zephir_prop_0 = NULL;
	static zend_string *_zephir_prop_1 = NULL;
	static zend_string *_zephir_prop_2 = NULL;
	static zend_string *_zephir_prop_3 = NULL;
	static zend_string *_zephir_prop_4 = NULL;
	if (UNEXPECTED(!_zephir_prop_0)) {
		_zephir_prop_0 = zend_string_init("field", 5, 1);
	}
	if (UNEXPECTED(!_zephir_prop_1)) {
		_zephir_prop_1 = zend_string_init("message", 7, 1);
	}
	if (UNEXPECTED(!_zephir_prop_2)) {
		_zephir_prop_2 = zend_string_init("type", 4, 1);
	}
	if (UNEXPECTED(!_zephir_prop_3)) {
		_zephir_prop_3 = zend_string_init("code", 4, 1);
	}
	if (UNEXPECTED(!_zephir_prop_4)) {
		_zephir_prop_4 = zend_string_init("metaData", 8, 1);
	}
	ZEPHIR_METHOD_GLOBALS_PTR = pecalloc(1, sizeof(zephir_method_globals), 0);
	zephir_memory_grow_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__);

	zephir_create_array(return_value, 5, 0);
	zephir_memory_observe(&_0);
	zephir_read_property_cached(&_0, this_ptr, _zephir_prop_0, 957, PH_NOISY_CC);
	zephir_array_update_string(return_value, SL("field"), &_0, PH_COPY | PH_SEPARATE);
	ZEPHIR_OBS_NVAR(&_0);
	zephir_read_property_cached(&_0, this_ptr, _zephir_prop_1, 956, PH_NOISY_CC);
	zephir_array_update_string(return_value, SL("message"), &_0, PH_COPY | PH_SEPARATE);
	ZEPHIR_OBS_NVAR(&_0);
	zephir_read_property_cached(&_0, this_ptr, _zephir_prop_2, 958, PH_NOISY_CC);
	zephir_array_update_string(return_value, SL("type"), &_0, PH_COPY | PH_SEPARATE);
	ZEPHIR_OBS_NVAR(&_0);
	zephir_read_property_cached(&_0, this_ptr, _zephir_prop_3, 959, PH_NOISY_CC);
	zephir_array_update_string(return_value, SL("code"), &_0, PH_COPY | PH_SEPARATE);
	ZEPHIR_OBS_NVAR(&_0);
	zephir_read_property_cached(&_0, this_ptr, _zephir_prop_4, 960, PH_NOISY_CC);
	zephir_array_update_string(return_value, SL("metaData"), &_0, PH_COPY | PH_SEPARATE);
	RETURN_MM();
}

/**
 * Sets code for the message
 */
PHP_METHOD(Phalcon_Messages_Message, setCode)
{
	zval *code_param = NULL, _0;
	zend_long code;
	zval *this_ptr = getThis();

	ZVAL_UNDEF(&_0);
	static zend_string *_zephir_prop_0 = NULL;
	if (UNEXPECTED(!_zephir_prop_0)) {
		_zephir_prop_0 = zend_string_init("code", 4, 1);
	}

	ZEND_PARSE_PARAMETERS_START(1, 1)
		Z_PARAM_LONG(code)
	ZEND_PARSE_PARAMETERS_END();
	zephir_fetch_params_without_memory_grow(1, 0, &code_param);
	ZVAL_UNDEF(&_0);
	ZVAL_LONG(&_0, code);
	zephir_update_property_zval_cached(this_ptr, _zephir_prop_0, 959, &_0);
	RETURN_THISW();
}

/**
 * Sets field name related to message
 */
PHP_METHOD(Phalcon_Messages_Message, setField)
{
	zval field_zv;
	zend_string *field = NULL;
	zval *this_ptr = getThis();

	ZVAL_UNDEF(&field_zv);
	static zend_string *_zephir_prop_0 = NULL;
	if (UNEXPECTED(!_zephir_prop_0)) {
		_zephir_prop_0 = zend_string_init("field", 5, 1);
	}

	ZEND_PARSE_PARAMETERS_START(1, 1)
		Z_PARAM_STR(field)
	ZEND_PARSE_PARAMETERS_END();
	ZVAL_STR(&field_zv, field);
	zephir_update_property_zval_cached(this_ptr, _zephir_prop_0, 957, &field_zv);
	RETURN_THISW();
}

/**
 * Sets verbose message
 */
PHP_METHOD(Phalcon_Messages_Message, setMessage)
{
	zval message_zv;
	zend_string *message = NULL;
	zval *this_ptr = getThis();

	ZVAL_UNDEF(&message_zv);
	static zend_string *_zephir_prop_0 = NULL;
	if (UNEXPECTED(!_zephir_prop_0)) {
		_zephir_prop_0 = zend_string_init("message", 7, 1);
	}

	ZEND_PARSE_PARAMETERS_START(1, 1)
		Z_PARAM_STR(message)
	ZEND_PARSE_PARAMETERS_END();
	ZVAL_STR(&message_zv, message);
	zephir_update_property_zval_cached(this_ptr, _zephir_prop_0, 956, &message_zv);
	RETURN_THISW();
}

/**
 * Sets message metadata
 *
 * @param messages_metadata $metaData
 */
PHP_METHOD(Phalcon_Messages_Message, setMetaData)
{
	zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
	zval *metaData_param = NULL;
	zval metaData;
	zval *this_ptr = getThis();

	ZVAL_UNDEF(&metaData);
	static zend_string *_zephir_prop_0 = NULL;
	if (UNEXPECTED(!_zephir_prop_0)) {
		_zephir_prop_0 = zend_string_init("metaData", 8, 1);
	}

	ZEND_PARSE_PARAMETERS_START(1, 1)
		ZEPHIR_Z_PARAM_ARRAY(metaData, metaData_param)
	ZEND_PARSE_PARAMETERS_END();
	ZEPHIR_METHOD_GLOBALS_PTR = pecalloc(1, sizeof(zephir_method_globals), 0);
	zephir_memory_grow_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__);
	zephir_fetch_params(1, 1, 0, &metaData_param);
	zephir_get_arrval(&metaData, metaData_param);
	zephir_update_property_zval_cached(this_ptr, _zephir_prop_0, 960, &metaData);
	RETURN_THIS();
}

/**
 * Sets message type
 */
PHP_METHOD(Phalcon_Messages_Message, setType)
{
	zval type_zv;
	zend_string *type = NULL;
	zval *this_ptr = getThis();

	ZVAL_UNDEF(&type_zv);
	static zend_string *_zephir_prop_0 = NULL;
	if (UNEXPECTED(!_zephir_prop_0)) {
		_zephir_prop_0 = zend_string_init("type", 4, 1);
	}

	ZEND_PARSE_PARAMETERS_START(1, 1)
		Z_PARAM_STR(type)
	ZEND_PARSE_PARAMETERS_END();
	ZVAL_STR(&type_zv, type);
	zephir_update_property_zval_cached(this_ptr, _zephir_prop_0, 958, &type_zv);
	RETURN_THISW();
}

