From d5a5028c00509711920f83524e97714e70bdf89e Mon Sep 17 00:00:00 2001 From: Vladimir Kolesnikov Date: Fri, 9 Aug 2013 02:05:59 +0300 Subject: [PATCH] PHALCON_VERIFY_INTERFACE() and PHALCON_VERIFY_CLASS() (cherry picked from commit 6aed4b1b04f47a84fa76170cad873d87cc4a3a33) --- ext/kernel/main.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/ext/kernel/main.h b/ext/kernel/main.h index 008ce6a5fb0..75aa1d064fc 100644 --- a/ext/kernel/main.h +++ b/ext/kernel/main.h @@ -17,6 +17,8 @@ +------------------------------------------------------------------------+ */ +#include "ext/spl/spl_exceptions.h" + /** Main macros */ #define PH_DEBUG 0 @@ -324,3 +326,34 @@ extern int phalcon_fetch_parameters(int num_args TSRMLS_DC, int required_args, i } \ } +#define PHALCON_VERIFY_INTERFACE(instance, interface_ce) \ + do { \ + if (Z_TYPE_P(instance) != IS_OBJECT || !instanceof_function_ex(Z_OBJCE_P(instance), interface_ce, 1 TSRMLS_CC)) { \ + char *buf; \ + if (Z_TYPE_P(instance) != IS_OBJECT) { \ + spprintf(&buf, 0, "Unexpected value type: expected object implementing %s, %s given", interface_ce->name, zend_zval_type_name(instance)); \ + } \ + else { \ + spprintf(&buf, 0, "Unexpected value type: expected object implementing %s, object of type %s given", interface_ce->name, Z_OBJCE_P(instance)->name); \ + } \ + PHALCON_THROW_EXCEPTION_STR(spl_ce_LogicException, buf); \ + efree(buf); \ + return; \ + } \ + } while (0) + +#define PHALCON_VERIFY_CLASS(instance, class_ce) \ + do { \ + if (Z_TYPE_P(instance) != IS_OBJECT || !instanceof_function_ex(Z_OBJCE_P(instance), class_ce, 0 TSRMLS_CC)) { \ + char *buf; \ + if (Z_TYPE_P(instance) != IS_OBJECT) { \ + spprintf(&buf, 0, "Unexpected value type: expected object of type %s, %s given", class_ce->name, zend_zval_type_name(instance)); \ + } \ + else { \ + spprintf(&buf, 0, "Unexpected value type: expected object of type %s, object of type %s given", class_ce->name, Z_OBJCE_P(instance)->name); \ + } \ + PHALCON_THROW_EXCEPTION_STR(spl_ce_LogicException, buf); \ + efree(buf); \ + return; \ + } \ + } while (0)