ONPHP_METHOD(DBField, setTable) { zval *table; if (Z_TYPE_P(ONPHP_READ_PROPERTY(getThis(), "table")) != IS_NULL) { ONPHP_THROW( WrongStateException, "you should not override setted table" ); } ONPHP_GET_ARGS("z", &table); if (!ONPHP_INSTANCEOF(table, DialectString)) { zval *from_table; ONPHP_MAKE_FOREIGN_OBJECT("FromTable", from_table); ONPHP_CALL_METHOD_1_NORET(from_table, "__construct", NULL, table); if (EG(exception)) { ZVAL_FREE(from_table); return; } ONPHP_UPDATE_PROPERTY(getThis(), "table", from_table); zval_ptr_dtor(&from_table); } else { ONPHP_UPDATE_PROPERTY(getThis(), "table", table); } RETURN_THIS; }
ONPHP_METHOD(Ternary, decide) { zval *true, *false, *null, *trinity = ONPHP_READ_PROPERTY(getThis(), "trinity"); ONPHP_GET_ARGS("zz|z", &true, &false, &null); if (Z_TYPE_P(trinity) == IS_BOOL) { if (zval_is_true(trinity)) { RETURN_ZVAL(true, 1, 0); } else { RETURN_ZVAL(false, 1, 0); } } else if (Z_TYPE_P(trinity) == IS_NULL) { if (ZEND_NUM_ARGS() == 3) { RETURN_ZVAL(null, 1, 0); } else { RETURN_NULL(); } } ONPHP_THROW(WrongStateException, NULL); }
ONPHP_METHOD(QuerySkeleton, where) { zval *exp, *logic, *where = ONPHP_READ_PROPERTY(getThis(), "where"); if (Z_TYPE_P(where) != IS_ARRAY) { ONPHP_THROW(WrongStateException, NULL); } zend_bool where_not_empty = ( (Z_TYPE_P(where) == IS_ARRAY) && (zend_hash_num_elements(Z_ARRVAL_P(where)) > 0) ); ONPHP_GET_ARGS("O|z", &exp, onphp_ce_LogicalObject, &logic); if ( (ZEND_NUM_ARGS() == 1) && where_not_empty ) { ONPHP_THROW( WrongArgumentException, "you have to specify expression logic" ); } else { zval *whereLogic = ONPHP_READ_PROPERTY(getThis(), "whereLogic"); if (Z_TYPE_P(whereLogic) != IS_ARRAY) { ONPHP_THROW(WrongStateException, NULL); } if (!where_not_empty || (ZEND_NUM_ARGS() == 1)) { add_next_index_null(whereLogic); } else { ONPHP_ARRAY_ADD(whereLogic, logic); } ONPHP_ARRAY_ADD(where, exp); } RETURN_THIS; }
ONPHP_METHOD(Ternary, toString) { zval *trinity = ONPHP_READ_PROPERTY(getThis(), "trinity"); if (Z_TYPE_P(trinity) == IS_BOOL) { if (zval_is_true(trinity)) { RETURN_STRINGL("true", 4, 1); } else { RETURN_STRINGL("false", 5, 1); } } else if (Z_TYPE_P(trinity) == IS_NULL) { RETURN_STRINGL("null", 4, 1); } ONPHP_THROW(WrongStateException, NULL); }
ONPHP_METHOD(Singleton, getInstance) { char *name; int length, argc = ZEND_NUM_ARGS(); zend_class_entry **cep; zval *object, *args; zval **stored; zval ***params = NULL; if (argc < 1) { WRONG_PARAM_COUNT; } params = safe_emalloc(sizeof(zval **), argc, 0); if (zend_get_parameters_array_ex(argc, params) == FAILURE) { efree(params); ONPHP_THROW( BaseException, "Failed to get calling arguments for object creation" ); } // replica of historical Singleton's behaviour if (argc > 2) { int i; ALLOC_INIT_ZVAL(args); array_init(args); for (i = 1; i < argc; ++i) { add_next_index_zval(args, *params[i]); } params[1] = &args; argc = 2; } if (Z_TYPE_PP(params[0]) != IS_STRING) { ONPHP_THROW(WrongArgumentException, "strange class name given"); } name = estrdup(Z_STRVAL_PP(params[0])); length = strlen(name); if ( zend_hash_find( Z_ARRVAL_P(instances), name, length + 1, (void **) &stored ) == SUCCESS ) { efree(params); efree(name); object = *stored; zval_copy_ctor(object); } else { // stolen from Reflection's newInstance() if (zend_lookup_class(name, length, &cep TSRMLS_CC) == SUCCESS) { zval *retval_ptr; zend_fcall_info fci; zend_fcall_info_cache fcc; zend_class_entry *ce = *cep; // can use ce->name instead now efree(name); if (!instanceof_function(ce, onphp_ce_Singleton TSRMLS_CC)) { efree(params); ONPHP_THROW( WrongArgumentException, "Class '%s' is something not a Singleton's child", ce->name ); } // we can call protected consturctors, // since all classes are childs of Singleton if (ce->constructor->common.fn_flags & ZEND_ACC_PRIVATE) { efree(params); ONPHP_THROW( BaseException, "Can not call private constructor for '%s' creation", ce->name ); } else if (ce->constructor->common.fn_flags & ZEND_ACC_PUBLIC) { efree(params); ONPHP_THROW( BaseException, "Don't want to deal with '%s' class " "due to public constructor there", ce->name ); } ALLOC_INIT_ZVAL(object); object_init_ex(object, ce); fci.size = sizeof(fci); fci.function_table = EG(function_table); fci.function_name = NULL; fci.symbol_table = NULL; fci.object_pp = &object; fci.retval_ptr_ptr = &retval_ptr; fci.param_count = argc - 1; fci.params = params + 1; fcc.initialized = 1; fcc.function_handler = ce->constructor; fcc.calling_scope = EG(scope); fcc.object_pp = &object; if (zend_call_function(&fci, &fcc TSRMLS_CC) == FAILURE) { zend_throw_exception_ex( onphp_ce_BaseException, 0 TSRMLS_CC, "Failed to call '%s' constructor", ce->name ); } efree(params); if (retval_ptr) { zval_ptr_dtor(&retval_ptr); } if (EG(exception)) { return; } add_assoc_zval_ex(instances, ce->name, length + 1, object); } } RETURN_ZVAL(object, 1, 0); }
ONPHP_METHOD(QueryIdentification, setId) { ONPHP_THROW(UnsupportedMethodException, NULL); }
ONPHP_METHOD(ExtractPart, __construct) { zval *what, *from, *fromField; zend_class_entry **cep; ONPHP_GET_ARGS("zz", &what, &from); if (ONPHP_INSTANCEOF(from, DialectString)) { if ( !( ONPHP_INSTANCEOF(from, DBValue) || ONPHP_INSTANCEOF(from, DBField) ) ) { ONPHP_THROW(WrongArgumentException, NULL); } } ONPHP_MAKE_OBJECT(DBField, fromField); ONPHP_CALL_METHOD_1_NORET(fromField, "__construct", NULL, from); if (EG(exception)) { ZVAL_FREE(fromField); return; } ONPHP_UPDATE_PROPERTY(getThis(), "from", fromField); zval_ptr_dtor(&fromField); ONPHP_FIND_FOREIGN_CLASS("DatePart", cep); if ( !( (Z_TYPE_P(what) == IS_OBJECT) && instanceof_function(Z_OBJCE_P(what), *cep TSRMLS_CC) ) ) { zval *whatPart; ALLOC_INIT_ZVAL(whatPart); object_init_ex(whatPart, *cep); Z_TYPE_P(whatPart) = IS_OBJECT; ONPHP_CALL_METHOD_1_NORET(whatPart, "__construct", NULL, what); if (EG(exception)) { ZVAL_FREE(whatPart); return; } ONPHP_UPDATE_PROPERTY(getThis(), "what", whatPart); zval_ptr_dtor(&whatPart); } else { ONPHP_UPDATE_PROPERTY(getThis(), "what", what); zval_ptr_dtor(&what); } }