/*
   +----------------------------------------------------------------------+
   | Copyright © The PHP Group and Contributors.                          |
   +----------------------------------------------------------------------+
   | This source file is subject to the Modified BSD License that is      |
   | bundled with this package in the file LICENSE, and is available      |
   | through the World Wide Web at <https://www.php.net/license/>.        |
   |                                                                      |
   | SPDX-License-Identifier: BSD-3-Clause                                |
   +----------------------------------------------------------------------+
   | Authors: Kirti Velankar <kirtig@yahoo-inc.com>                       |
   +----------------------------------------------------------------------+
*/
#include <unicode/udat.h>

extern "C" {
#include "dateformat_class.h"
#include "php_intl.h"
#include "dateformat_data.h"
#include "dateformat.h"
#include "dateformat_arginfo.h"
}

#include <zend_exceptions.h>

zend_class_entry *IntlDateFormatter_ce_ptr = NULL;
static zend_object_handlers IntlDateFormatter_handlers;

/*
 * Auxiliary functions needed by objects of 'IntlDateFormatter' class
 */

/* {{{ IntlDateFormatter_objects_free */
void IntlDateFormatter_object_free( zend_object *object )
{
	IntlDateFormatter_object* dfo = php_intl_dateformatter_fetch_object(object);

	zend_object_std_dtor( &dfo->zo );

	if (dfo->requested_locale) {
		efree( dfo->requested_locale );
	}

	dateformat_data_free( &dfo->datef_data );
}
/* }}} */

/* {{{ IntlDateFormatter_object_create */
zend_object *IntlDateFormatter_object_create(zend_class_entry *ce)
{
	IntlDateFormatter_object*     intern;

	intern = reinterpret_cast<IntlDateFormatter_object *>(zend_object_alloc(sizeof(IntlDateFormatter_object), ce));
	dateformat_data_init( &intern->datef_data );
	zend_object_std_init( &intern->zo, ce );
	object_properties_init(&intern->zo, ce);
	intern->date_type			= 0;
	intern->time_type			= 0;
	intern->calendar			= -1;
	intern->requested_locale	= NULL;

	return &intern->zo;
}
/* }}} */

/* {{{ IntlDateFormatter_object_clone */
zend_object *IntlDateFormatter_object_clone(zend_object *object)
{
	const IntlDateFormatter_object     *dfo = php_intl_dateformatter_fetch_object(object);
	zend_object              *new_obj = IntlDateFormatter_ce_ptr->create_object(object->ce);
	IntlDateFormatter_object *new_dfo = php_intl_dateformatter_fetch_object(new_obj);

	/* clone standard parts */
	zend_objects_clone_members(&new_dfo->zo, &dfo->zo);

	/* clone formatter object */
	if (DATE_FORMAT_OBJECT(dfo) != NULL) {
		UErrorCode error = U_ZERO_ERROR;
		DATE_FORMAT_OBJECT(new_dfo) = udat_clone(DATE_FORMAT_OBJECT(dfo), &error);

		if (U_FAILURE(error)) {
			zend_throw_error(NULL, "Failed to clone IntlDateFormatter");
		}
	} else {
		zend_throw_error(NULL, "Cannot clone uninitialized IntlDateFormatter");
	}
	return new_obj;
}
/* }}} */

/*
 * 'IntlDateFormatter' class registration structures & functions
 */

/* {{{ dateformat_register_class
 * Initialize 'IntlDateFormatter' class
 */
void dateformat_register_IntlDateFormatter_class( void )
{
	/* Create and register 'IntlDateFormatter' class. */
	IntlDateFormatter_ce_ptr = register_class_IntlDateFormatter();
	IntlDateFormatter_ce_ptr->create_object = IntlDateFormatter_object_create;
	IntlDateFormatter_ce_ptr->default_object_handlers = &IntlDateFormatter_handlers;

	memcpy(&IntlDateFormatter_handlers, &std_object_handlers,
		sizeof IntlDateFormatter_handlers);
	IntlDateFormatter_handlers.offset = offsetof(IntlDateFormatter_object, zo);
	IntlDateFormatter_handlers.clone_obj = IntlDateFormatter_object_clone;
	IntlDateFormatter_handlers.free_obj = IntlDateFormatter_object_free;
}
/* }}} */
