/** * Appends a NOT BETWEEN condition to the current conditions * *<code> * $criteria->notBetweenWhere('price', 100.25, 200.50); *</code> * * @param string $expr * @param mixed $minimum * @param mixed $maximum * @return Phalcon\Mvc\Model\Query\Builder */ PHP_METHOD(Phalcon_Mvc_Model_Criteria, notBetweenWhere){ zval *expr, *minimum, *maximum, *hidden_param, *one; zval *next_hidden_param, *minimum_key, *maximum_key; zval *conditions, *bind_params; PHALCON_MM_GROW(); phalcon_fetch_params(1, 3, 0, &expr, &minimum, &maximum); PHALCON_OBS_VAR(hidden_param); phalcon_read_property_this(&hidden_param, this_ptr, SL("_hiddenParamNumber"), PH_NOISY_CC); PHALCON_INIT_VAR(one); ZVAL_LONG(one, 1); PHALCON_INIT_VAR(next_hidden_param); phalcon_add_function(next_hidden_param, hidden_param, one TSRMLS_CC); /** * Minimum key with auto bind-params */ PHALCON_INIT_VAR(minimum_key); PHALCON_CONCAT_SV(minimum_key, "phb", hidden_param); /** * Maximum key with auto bind-params */ PHALCON_INIT_VAR(maximum_key); PHALCON_CONCAT_SV(maximum_key, "phb", next_hidden_param); /** * Create a standard BETWEEN condition with bind params */ PHALCON_INIT_VAR(conditions); PHALCON_CONCAT_VSVSVS(conditions, expr, " NOT BETWEEN :", minimum_key, ": AND :", maximum_key, ":"); PHALCON_INIT_VAR(bind_params); array_init_size(bind_params, 2); phalcon_array_update_zval(&bind_params, minimum_key, &minimum, PH_COPY | PH_SEPARATE TSRMLS_CC); phalcon_array_update_zval(&bind_params, maximum_key, &maximum, PH_COPY | PH_SEPARATE TSRMLS_CC); /** * Append the BETWEEN to the current conditions using and 'and' */ phalcon_call_method_p2_noret(this_ptr, "andwhere", conditions, bind_params); PHALCON_SEPARATE(next_hidden_param); phalcon_increment(next_hidden_param); phalcon_update_property_this(this_ptr, SL("_hiddenParamNumber"), next_hidden_param TSRMLS_CC); RETURN_THIS(); }
/** * Draws a polygon * *<code> * $coordinates = array( array( 'x' => 4, 'y' => 6 ), array( 'x' => 8, 'y' => 10 ) ); * $image->polygon($coordinates); *</code> * * @param array $coordinates array of x and y * @param string $color * @return Phalcon\Image\Adapter\GD */ PHP_METHOD(Phalcon_Image_Adapter_GD, polygon){ zval *coordinates, *color = NULL, image = {}, rgb = {}, r = {}, g = {}, b = {}, imagecolor = {}, *point, points = {}, num_points = {}; phalcon_fetch_params(0, 1, 1, &coordinates, &color); if (!color) { color = &PHALCON_GLOBAL(z_null); } if (!phalcon_fast_count_ev(coordinates)) { PHALCON_THROW_EXCEPTION_STRW(phalcon_image_exception_ce, "Coordinates must be not empty"); return; } phalcon_return_property(&image, getThis(), SL("_image")); if (Z_TYPE(image) == IS_RESOURCE) { PHALCON_CALL_METHODW(&rgb, getThis(), "getcolorrbg", color); phalcon_array_fetch_long(&r, &rgb, 0, PH_NOISY); phalcon_array_fetch_long(&g, &rgb, 1, PH_NOISY); phalcon_array_fetch_long(&b, &rgb, 2, PH_NOISY); PHALCON_CALL_FUNCTIONW(&imagecolor, "imagecolorallocate", &image, &r, &g, &b); array_init(&points); ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(coordinates), point) { zval x = {}, y = {}; if (Z_TYPE_P(point) == IS_ARRAY) { if (phalcon_fast_count_int(point) != 2) { PHALCON_THROW_EXCEPTION_STRW(phalcon_image_exception_ce, "Coordinates point error"); return; } if (!phalcon_array_isset_fetch_long(&x, point, 0)) { phalcon_array_fetch_str(&x, point, SL("x"), PH_NOISY); } if (!phalcon_array_isset_fetch_long(&y, point, 0)) { phalcon_array_fetch_str(&y, point, SL("y"), PH_NOISY); } phalcon_array_append(&points, &x, PH_COPY); phalcon_array_append(&points, &y, PH_COPY); } else { phalcon_array_append(&points, &_p->val, PH_COPY); _p++; phalcon_array_append(&points, &_p->val, PH_COPY); } phalcon_increment(&num_points); } ZEND_HASH_FOREACH_END();
/** * Adds a number to a string or increment that number if it already is defined * *<code> * echo Phalcon\Text::increment("a"); // "a_1" * echo Phalcon\Text::increment("a_1"); // "a_2" *</code> * * @param string $str * @param string $separator * @return string */ PHP_METHOD(Phalcon_Text, increment) { zval *str, *separator = NULL, *parts, *number = NULL, *first_part; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 1, &str, &separator); if (!separator) { PHALCON_INIT_VAR(separator); } else { PHALCON_SEPARATE_PARAM(separator); } if (Z_TYPE_P(separator) == IS_NULL) { PHALCON_INIT_NVAR(separator); ZVAL_STRING(separator, "_", 1); } PHALCON_INIT_VAR(parts); phalcon_fast_explode(parts, separator, str); if (phalcon_array_isset_long(parts, 1)) { PHALCON_OBS_VAR(number); phalcon_array_fetch_long(&number, parts, 1, PH_NOISY); SEPARATE_ZVAL(&number); phalcon_increment(number); } else { PHALCON_INIT_VAR(number); ZVAL_LONG(number, 1); } PHALCON_OBS_VAR(first_part); phalcon_array_fetch_long(&first_part, parts, 0, PH_NOISY); PHALCON_CONCAT_VVV(return_value, first_part, separator, number); RETURN_MM(); }
/** * Shows a backtrace item * * @param int $n * @param array $trace */ PHP_METHOD(Phalcon_Debug, showTraceItem){ zval *n, *trace, *space, *two_spaces, *underscore; zval *minus, *html, *class_name, *pattern, *is_phalcon_class; zval *namespace_separator, *prepare_uri_class; zval *class_reflection, *is_internal = NULL, *lower_class_name; zval *prepare_internal_class, *type, *function_name = NULL; zval *function_reflection, *prepared_function_name; zval *trace_args, *arguments, *argument = NULL, *dumped_argument = NULL; zval *span_argument = NULL, *joined_arguments, *one; zval *file, *line, *show_files, *lines, *number_lines; zval *show_file_fragment, *seven, *before_line; zval *first_line = NULL, *five, *after_line, *last_line = NULL; zval *comment_pattern, *utf8, *ent_compat, *tab; zval *comment, *i = NULL, *line_position = NULL, *current_line = NULL; zval *trimmed = NULL, *is_comment = NULL, *spaced_current_line = NULL; zval *escaped_line = NULL; HashTable *ah0; HashPosition hp0; zval **hd; zend_class_entry *ce0, *ce1; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &n, &trace); PHALCON_INIT_VAR(space); ZVAL_STRING(space, " ", 1); PHALCON_INIT_VAR(two_spaces); ZVAL_STRING(two_spaces, " ", 1); PHALCON_INIT_VAR(underscore); ZVAL_STRING(underscore, "_", 1); PHALCON_INIT_VAR(minus); ZVAL_STRING(minus, "-", 1); /** * Every trace in the backtrace have a unique number */ PHALCON_INIT_VAR(html); PHALCON_CONCAT_SVS(html, "<tr><td align=\"right\" valign=\"top\" class=\"error-number\">#", n, "</td><td>"); if (phalcon_array_isset_string(trace, SS("class"))) { PHALCON_OBS_VAR(class_name); phalcon_array_fetch_string(&class_name, trace, SL("class"), PH_NOISY); PHALCON_INIT_VAR(pattern); ZVAL_STRING(pattern, "/^Phalcon/", 1); PHALCON_INIT_VAR(is_phalcon_class); phalcon_preg_match(is_phalcon_class, pattern, class_name, NULL TSRMLS_CC); /** * We assume that classes starting by Phalcon are framework's classes */ if (zend_is_true(is_phalcon_class)) { PHALCON_INIT_VAR(namespace_separator); ZVAL_STRING(namespace_separator, "\\", 1); /** * Prepare the class name according to the Phalcon's conventions */ PHALCON_INIT_VAR(prepare_uri_class); phalcon_fast_str_replace(prepare_uri_class, namespace_separator, underscore, class_name); /** * Generate a link to the official docs */ PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://docs.phalconphp.com/en/latest/api/", prepare_uri_class, ".html\">", class_name, "</a></span>"); } else { ce0 = zend_fetch_class(SL("ReflectionClass"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); PHALCON_INIT_VAR(class_reflection); object_init_ex(class_reflection, ce0); if (phalcon_has_constructor(class_reflection TSRMLS_CC)) { phalcon_call_method_p1_noret(class_reflection, "__construct", class_name); } /** * Check if classes are PHP's classes */ PHALCON_INIT_VAR(is_internal); phalcon_call_method(is_internal, class_reflection, "isinternal"); if (zend_is_true(is_internal)) { PHALCON_INIT_VAR(lower_class_name); phalcon_fast_strtolower(lower_class_name, class_name); PHALCON_INIT_VAR(prepare_internal_class); phalcon_fast_str_replace(prepare_internal_class, underscore, minus, lower_class_name); /** * Generate a link to the official docs */ PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://php.net/manual/en/class.", prepare_internal_class, ".php\">", class_name, "</a></span>"); } else { PHALCON_SCONCAT_SVS(html, "<span class=\"error-class\">", class_name, "</span>"); } } /** * Object access operator: static/instance */ PHALCON_OBS_VAR(type); phalcon_array_fetch_string(&type, trace, SL("type"), PH_NOISY); phalcon_concat_self(&html, type TSRMLS_CC); } /** * Normally the backtrace contains only classes */ if (phalcon_array_isset_string(trace, SS("class"))) { PHALCON_OBS_VAR(function_name); phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY); PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>"); } else { PHALCON_OBS_NVAR(function_name); phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY); /** * Check if the function exists */ if (phalcon_function_exists(function_name TSRMLS_CC) == SUCCESS) { ce1 = zend_fetch_class(SL("ReflectionFunction"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); PHALCON_INIT_VAR(function_reflection); object_init_ex(function_reflection, ce1); if (phalcon_has_constructor(function_reflection TSRMLS_CC)) { phalcon_call_method_p1_noret(function_reflection, "__construct", function_name); } PHALCON_INIT_NVAR(is_internal); phalcon_call_method(is_internal, function_reflection, "isinternal"); /** * Internal functions links to the PHP documentation */ if (zend_is_true(is_internal)) { /** * Prepare function's name according to the conventions in the docs */ PHALCON_INIT_VAR(prepared_function_name); phalcon_fast_str_replace(prepared_function_name, underscore, minus, function_name); PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-function\"><a target=\"_new\" href=\"http://php.net/manual/en/function.", prepared_function_name, ".php\">", function_name, "</a></span>"); } else { PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>"); } } else { PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>"); } } /** * Check for arguments in the function */ if (phalcon_array_isset_string(trace, SS("args"))) { PHALCON_OBS_VAR(trace_args); phalcon_array_fetch_string(&trace_args, trace, SL("args"), PH_NOISY); if (phalcon_fast_count_ev(trace_args TSRMLS_CC)) { PHALCON_INIT_VAR(arguments); array_init(arguments); phalcon_is_iterable(trace_args, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(argument); /** * Every argument is generated using _getVarDump */ PHALCON_INIT_NVAR(dumped_argument); phalcon_call_method_p1(dumped_argument, this_ptr, "_getvardump", argument); PHALCON_INIT_NVAR(span_argument); PHALCON_CONCAT_SVS(span_argument, "<span class=\"error-parameter\">", dumped_argument, "</span>"); /** * Append the HTML generated to the argument's list */ phalcon_array_append(&arguments, span_argument, PH_SEPARATE); zend_hash_move_forward_ex(ah0, &hp0); } /** * Join all the arguments */ PHALCON_INIT_VAR(joined_arguments); phalcon_fast_join_str(joined_arguments, SL(", "), arguments TSRMLS_CC); PHALCON_SCONCAT_SVS(html, "(", joined_arguments, ")"); } else { phalcon_concat_self_str(&html, SL("()") TSRMLS_CC); } } /** * When 'file' is present, it usually means the function is provided by the user */ if (phalcon_array_isset_string(trace, SS("file"))) { PHALCON_INIT_VAR(one); ZVAL_LONG(one, 1); PHALCON_OBS_VAR(file); phalcon_array_fetch_string(&file, trace, SL("file"), PH_NOISY); PHALCON_OBS_VAR(line); phalcon_array_fetch_string(&line, trace, SL("line"), PH_NOISY); /** * Realpath to the file and its line using a special header */ PHALCON_SCONCAT_SVSVS(html, "<br/><div class=\"error-file\">", file, " (", line, ")</div>"); PHALCON_OBS_VAR(show_files); phalcon_read_property_this(&show_files, this_ptr, SL("_showFiles"), PH_NOISY_CC); /** * The developer can change if the files must be opened or not */ if (zend_is_true(show_files)) { /** * Open the file to an array using 'file', this respects the openbase-dir directive */ PHALCON_INIT_VAR(lines); phalcon_call_func_p1(lines, "file", file); PHALCON_INIT_VAR(number_lines); phalcon_fast_count(number_lines, lines TSRMLS_CC); PHALCON_OBS_VAR(show_file_fragment); phalcon_read_property_this(&show_file_fragment, this_ptr, SL("_showFileFragment"), PH_NOISY_CC); /** * File fragments just show a piece of the file where the exception is located */ if (zend_is_true(show_file_fragment)) { /** * Take seven lines back to the current exception's line, @TODO add an option for * this */ PHALCON_INIT_VAR(seven); ZVAL_LONG(seven, 7); PHALCON_INIT_VAR(before_line); sub_function(before_line, line, seven TSRMLS_CC); /** * Check for overflows */ if (PHALCON_LT_LONG(before_line, 1)) { PHALCON_CPY_WRT(first_line, one); } else { PHALCON_CPY_WRT(first_line, before_line); } /** * Take five lines after the current exception's line, @TODO add an option for this */ PHALCON_INIT_VAR(five); ZVAL_LONG(five, 5); PHALCON_INIT_VAR(after_line); phalcon_add_function(after_line, line, five TSRMLS_CC); /** * Check for overflows */ if (PHALCON_GT(after_line, number_lines)) { PHALCON_CPY_WRT(last_line, number_lines); } else { PHALCON_CPY_WRT(last_line, after_line); } PHALCON_SCONCAT_SVSVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums:", first_line, "'>"); } else { PHALCON_CPY_WRT(first_line, one); PHALCON_CPY_WRT(last_line, number_lines); PHALCON_SCONCAT_SVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums error-scroll'>"); } PHALCON_INIT_VAR(comment_pattern); ZVAL_STRING(comment_pattern, "#\\*\\/$#", 1); /** * We assume the file is utf-8 encoded, @TODO add an option for this */ PHALCON_INIT_VAR(utf8); ZVAL_STRING(utf8, "UTF-8", 1); /** * Don't escape quotes */ PHALCON_INIT_VAR(ent_compat); ZVAL_LONG(ent_compat, 2); PHALCON_INIT_VAR(tab); ZVAL_STRING(tab, "\t", 1); PHALCON_INIT_VAR(comment); ZVAL_STRING(comment, "* /", 1); PHALCON_CPY_WRT(i, first_line); while (1) { if (PHALCON_LE(i, last_line)) { } else { break; } /** * Current line in the file */ PHALCON_INIT_NVAR(line_position); sub_function(line_position, i, one TSRMLS_CC); /** * Current line content in the piece of file */ PHALCON_OBS_NVAR(current_line); phalcon_array_fetch(¤t_line, lines, line_position, PH_NOISY); /** * File fragments are cleaned, removing tabs and comments */ if (zend_is_true(show_file_fragment)) { if (PHALCON_IS_EQUAL(i, first_line)) { PHALCON_INIT_NVAR(trimmed); phalcon_fast_trim(trimmed, current_line, PHALCON_TRIM_RIGHT TSRMLS_CC); PHALCON_INIT_NVAR(is_comment); phalcon_preg_match(is_comment, comment_pattern, current_line, NULL TSRMLS_CC); if (zend_is_true(is_comment)) { PHALCON_INIT_NVAR(spaced_current_line); phalcon_fast_str_replace(spaced_current_line, comment, space, current_line); PHALCON_CPY_WRT(current_line, spaced_current_line); } } } /** * Print a non break space if the current line is a line break, this allows to show * the html zebra properly */ if (PHALCON_IS_STRING(current_line, "\n")) { phalcon_concat_self_str(&html, SL(" \n") TSRMLS_CC); } else { if (PHALCON_IS_STRING(current_line, "\r\n")) { phalcon_concat_self_str(&html, SL(" \n") TSRMLS_CC); } else { PHALCON_INIT_NVAR(spaced_current_line); phalcon_fast_str_replace(spaced_current_line, tab, two_spaces, current_line); PHALCON_INIT_NVAR(escaped_line); phalcon_call_func_p3(escaped_line, "htmlentities", spaced_current_line, ent_compat, utf8); phalcon_concat_self(&html, escaped_line TSRMLS_CC); } } PHALCON_SEPARATE(i); phalcon_increment(i); } phalcon_concat_self_str(&html, SL("</pre>") TSRMLS_CC); } } phalcon_concat_self_str(&html, SL("</td></tr>") TSRMLS_CC); RETURN_CTOR(html); }
/** * Appends a NOT IN condition to the current conditions * *<code> * $criteria->notInWhere('id', [1, 2, 3]); *</code> * * @param string $expr * @param array $values * @return Phalcon\Mvc\Model\Query\Builder */ PHP_METHOD(Phalcon_Mvc_Model_Criteria, notInWhere){ zval *expr, *values, *hidden_param, *bind_params; zval *bind_keys, *value = NULL, *key = NULL, *query_key = NULL, *joined_keys; zval *conditions; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &expr, &values); if (Z_TYPE_P(values) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Values must be an array"); return; } PHALCON_OBS_VAR(hidden_param); phalcon_read_property_this(&hidden_param, this_ptr, SL("_hiddenParamNumber"), PH_NOISY_CC); PHALCON_INIT_VAR(bind_params); array_init(bind_params); PHALCON_INIT_VAR(bind_keys); array_init(bind_keys); phalcon_is_iterable(values, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(value); /** * Key with auto bind-params */ PHALCON_INIT_NVAR(key); PHALCON_CONCAT_SV(key, "phi", hidden_param); PHALCON_INIT_NVAR(query_key); PHALCON_CONCAT_SVS(query_key, ":", key, ":"); phalcon_array_append(&bind_keys, query_key, PH_SEPARATE TSRMLS_CC); phalcon_array_update_zval(&bind_params, key, &value, PH_COPY | PH_SEPARATE TSRMLS_CC); PHALCON_SEPARATE(hidden_param); phalcon_increment(hidden_param); zend_hash_move_forward_ex(ah0, &hp0); } PHALCON_INIT_VAR(joined_keys); phalcon_fast_join_str(joined_keys, SL(", "), bind_keys TSRMLS_CC); /** * Create a standard IN condition with bind params */ PHALCON_INIT_VAR(conditions); PHALCON_CONCAT_VSVS(conditions, expr, " NOT IN (", joined_keys, ")"); /** * Append the IN to the current conditions using and 'and' */ phalcon_call_method_p2_noret(this_ptr, "andwhere", conditions, bind_params); phalcon_update_property_this(this_ptr, SL("_hiddenParamNumber"), hidden_param TSRMLS_CC); RETURN_THIS(); }
/** * Executes the validator * * @param Phalcon\Mvc\ModelInterface $record * @return boolean */ PHP_METHOD(Phalcon_Mvc_Model_Validator_Uniqueness, validate){ zval *record, *option = NULL, *field, *dependency_injector; zval *service, *meta_data, *bind_types, *bind_data_types; zval *column_map = NULL, *conditions, *bind_params; zval *number = NULL, *compose_field = NULL, *column_field = NULL; zval *exception_message = NULL, *value = NULL, *compose_condition = NULL; zval *bind_type = NULL, *condition = NULL, *operation_made; zval *primary_fields, *primary_field = NULL, *attribute_field = NULL; zval *join_conditions, *params, *class_name; zval *message = NULL, *join_fields, *type; HashTable *ah0, *ah1; HashPosition hp0, hp1; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &record); PHALCON_INIT_VAR(option); ZVAL_STRING(option, "field", 1); PHALCON_INIT_VAR(field); phalcon_call_method_p1(field, this_ptr, "getoption", option); PHALCON_INIT_VAR(dependency_injector); phalcon_call_method(dependency_injector, record, "getdi"); PHALCON_INIT_VAR(service); ZVAL_STRING(service, "modelsMetadata", 1); PHALCON_INIT_VAR(meta_data); phalcon_call_method_p1(meta_data, dependency_injector, "getshared", service); /** * PostgreSQL check if the compared constant has the same type as the column, so we * make cast to the data passed to match those column types */ PHALCON_INIT_VAR(bind_types); array_init(bind_types); PHALCON_INIT_VAR(bind_data_types); phalcon_call_method_p1(bind_data_types, meta_data, "getbindtypes", record); if (PHALCON_GLOBAL(orm).column_renaming) { PHALCON_INIT_VAR(column_map); phalcon_call_method_p1(column_map, meta_data, "getreversecolumnmap", record); } else { PHALCON_INIT_NVAR(column_map); } PHALCON_INIT_VAR(conditions); array_init(conditions); PHALCON_INIT_VAR(bind_params); array_init(bind_params); PHALCON_INIT_VAR(number); ZVAL_LONG(number, 0); if (Z_TYPE_P(field) == IS_ARRAY) { /** * The field can be an array of values */ phalcon_is_iterable(field, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(compose_field); /** * The reversed column map is used in the case to get real column name */ if (Z_TYPE_P(column_map) == IS_ARRAY) { if (phalcon_array_isset(column_map, compose_field)) { PHALCON_OBS_NVAR(column_field); phalcon_array_fetch(&column_field, column_map, compose_field, PH_NOISY); } else { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Column '", compose_field, "\" isn't part of the column map"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } } else { PHALCON_CPY_WRT(column_field, compose_field); } /** * Some database systems require that we pass the values using bind casting */ if (!phalcon_array_isset(bind_data_types, column_field)) { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Column '", column_field, "\" isn't part of the table columns"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } /** * The attribute could be "protected" so we read using "readattribute" */ PHALCON_INIT_NVAR(value); phalcon_call_method_p1(value, record, "readattribute", compose_field); PHALCON_INIT_NVAR(compose_condition); PHALCON_CONCAT_SVSV(compose_condition, "[", compose_field, "] = ?", number); phalcon_array_append(&conditions, compose_condition, PH_SEPARATE); phalcon_array_append(&bind_params, value, PH_SEPARATE); PHALCON_OBS_NVAR(bind_type); phalcon_array_fetch(&bind_type, bind_data_types, column_field, PH_NOISY); phalcon_array_append(&bind_types, bind_type, PH_SEPARATE); phalcon_increment(number); zend_hash_move_forward_ex(ah0, &hp0); } } else { /** * The reversed column map is used in the case to get real column name */ if (Z_TYPE_P(column_map) == IS_ARRAY) { if (phalcon_array_isset(column_map, field)) { PHALCON_OBS_NVAR(column_field); phalcon_array_fetch(&column_field, column_map, field, PH_NOISY); } else { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Column '", field, "\" isn't part of the column map"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } } else { PHALCON_CPY_WRT(column_field, field); } /** * Some database systems require that we pass the values using bind casting */ if (!phalcon_array_isset(bind_data_types, column_field)) { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Column '", column_field, "\" isn't part of the table columns"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } /** * We're checking the uniqueness with only one field */ PHALCON_INIT_NVAR(value); phalcon_call_method_p1(value, record, "readattribute", field); PHALCON_INIT_VAR(condition); PHALCON_CONCAT_SVS(condition, "[", field, "] = ?0"); phalcon_array_append(&conditions, condition, PH_SEPARATE); phalcon_array_append(&bind_params, value, PH_SEPARATE); PHALCON_OBS_NVAR(bind_type); phalcon_array_fetch(&bind_type, bind_data_types, column_field, PH_NOISY); phalcon_array_append(&bind_types, bind_type, PH_SEPARATE); phalcon_increment(number); } /** * If the operation is update, there must be values in the object */ PHALCON_INIT_VAR(operation_made); phalcon_call_method(operation_made, record, "getoperationmade"); if (PHALCON_IS_LONG(operation_made, 2)) { /** * We build a query with the primary key attributes */ if (PHALCON_GLOBAL(orm).column_renaming) { PHALCON_INIT_NVAR(column_map); phalcon_call_method_p1(column_map, meta_data, "getcolumnmap", record); } else { PHALCON_INIT_NVAR(column_map); } PHALCON_INIT_VAR(primary_fields); phalcon_call_method_p1(primary_fields, meta_data, "getprimarykeyattributes", record); phalcon_is_iterable(primary_fields, &ah1, &hp1, 0, 0); while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_HVALUE(primary_field); if (!phalcon_array_isset(bind_data_types, primary_field)) { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Column '", primary_field, "\" isn't part of the table columns"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } /** * Rename the column if there is a column map */ if (Z_TYPE_P(column_map) == IS_ARRAY) { if (phalcon_array_isset(column_map, primary_field)) { PHALCON_OBS_NVAR(attribute_field); phalcon_array_fetch(&attribute_field, column_map, primary_field, PH_NOISY); } else { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Column '", primary_field, "\" isn't part of the column map"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } } else { PHALCON_CPY_WRT(attribute_field, primary_field); } /** * Create a condition based on the renamed primary key */ PHALCON_INIT_NVAR(value); phalcon_call_method_p1(value, record, "readattribute", primary_field); PHALCON_INIT_NVAR(condition); PHALCON_CONCAT_SVSV(condition, "[", attribute_field, "] <> ?", number); phalcon_array_append(&conditions, condition, PH_SEPARATE); phalcon_array_append(&bind_params, value, PH_SEPARATE); PHALCON_OBS_NVAR(bind_type); phalcon_array_fetch(&bind_type, bind_data_types, primary_field, PH_NOISY); phalcon_array_append(&bind_types, bind_type, PH_SEPARATE); phalcon_increment(number); zend_hash_move_forward_ex(ah1, &hp1); } } PHALCON_INIT_VAR(join_conditions); phalcon_fast_join_str(join_conditions, SL(" AND "), conditions TSRMLS_CC); /** * We don't trust the user, so we pass the parameters as bound parameters */ PHALCON_INIT_VAR(params); array_init(params); phalcon_array_update_string(¶ms, SL("di"), &dependency_injector, PH_COPY | PH_SEPARATE); phalcon_array_update_string(¶ms, SL("conditions"), &join_conditions, PH_COPY | PH_SEPARATE); phalcon_array_update_string(¶ms, SL("bind"), &bind_params, PH_COPY | PH_SEPARATE); phalcon_array_update_string(¶ms, SL("bindTypes"), &bind_types, PH_COPY | PH_SEPARATE); PHALCON_INIT_VAR(class_name); phalcon_get_class(class_name, record, 0 TSRMLS_CC); /** * Check using a standard count */ phalcon_call_zval_str_static_p1(number, class_name, "count", params); if (!PHALCON_IS_LONG(number, 0)) { /** * Check if the developer has defined a custom message */ PHALCON_INIT_NVAR(option); ZVAL_STRING(option, "message", 1); PHALCON_INIT_VAR(message); phalcon_call_method_p1(message, this_ptr, "getoption", option); if (!zend_is_true(message)) { if (Z_TYPE_P(field) == IS_ARRAY) { PHALCON_INIT_VAR(join_fields); phalcon_fast_join_str(join_fields, SL(", "), field TSRMLS_CC); PHALCON_INIT_NVAR(message); PHALCON_CONCAT_SVS(message, "Value of fields: '", join_fields, "' are already present in another record"); } else { PHALCON_INIT_NVAR(message); PHALCON_CONCAT_SVS(message, "Value of field: '", field, "' is already present in another record"); } } /** * Append the message to the validator */ PHALCON_INIT_VAR(type); ZVAL_STRING(type, "Unique", 1); phalcon_call_method_p3_noret(this_ptr, "appendmessage", message, field, type); RETURN_MM_FALSE; } RETURN_MM_TRUE; }
/** * Returns a slice of the resultset to show in the pagination * * @return stdClass */ PHP_METHOD(Phalcon_Paginator_Adapter_Model, getPaginate){ zval *one, *zero, *show, *config, *items, *page_number = NULL; zval *n, *page, *last_show_page, *start; zval *possible_pages = NULL, *total_pages, *page_items; zval *i, *valid = NULL, *current = NULL, *maximum_pages, *next = NULL, *additional_page; zval *before = NULL, *remainder, *pages_total = NULL; zval *r0 = NULL; PHALCON_MM_GROW(); PHALCON_INIT_VAR(one); ZVAL_LONG(one, 1); PHALCON_INIT_VAR(zero); ZVAL_LONG(zero, 0); PHALCON_OBS_VAR(show); phalcon_read_property_this(&show, this_ptr, SL("_limitRows"), PH_NOISY_CC); PHALCON_OBS_VAR(config); phalcon_read_property_this(&config, this_ptr, SL("_config"), PH_NOISY_CC); PHALCON_OBS_VAR(items); phalcon_array_fetch_string(&items, config, SL("data"), PH_NOISY); PHALCON_OBS_VAR(page_number); phalcon_read_property_this(&page_number, this_ptr, SL("_page"), PH_NOISY_CC); if (Z_TYPE_P(page_number) == IS_NULL) { PHALCON_CPY_WRT(page_number, one); } if (PHALCON_LT(show, zero)) { PHALCON_THROW_EXCEPTION_STR(phalcon_paginator_exception_ce, "The start page number is zero or less"); return; } PHALCON_INIT_VAR(n); phalcon_fast_count(n, items TSRMLS_CC); PHALCON_INIT_VAR(page); object_init(page); PHALCON_INIT_VAR(last_show_page); sub_function(last_show_page, page_number, one TSRMLS_CC); PHALCON_INIT_VAR(start); mul_function(start, show, last_show_page TSRMLS_CC); PHALCON_INIT_VAR(possible_pages); div_function(possible_pages, n, show TSRMLS_CC); PHALCON_INIT_VAR(total_pages); phalcon_call_func_p1(total_pages, "ceil", possible_pages); if (Z_TYPE_P(items) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_paginator_exception_ce, "Invalid data for paginator"); return; } if (!zend_is_true(page_number)) { PHALCON_CPY_WRT(page_number, one); } PHALCON_INIT_VAR(page_items); array_init(page_items); if (PHALCON_GT(n, zero)) { /** * Seek to the desired position */ if (PHALCON_LE(start, n)) { phalcon_call_method_p1_noret(items, "seek", start); } else { phalcon_call_method_p1_noret(items, "seek", one); PHALCON_CPY_WRT(page_number, one); } /** * The record must be iterable */ PHALCON_INIT_VAR(i); ZVAL_LONG(i, 1); while (1) { PHALCON_INIT_NVAR(r0); phalcon_call_method(r0, items, "valid"); PHALCON_CPY_WRT(valid, r0); if (PHALCON_IS_NOT_FALSE(valid)) { } else { break; } PHALCON_INIT_NVAR(current); phalcon_call_method(current, items, "current"); phalcon_array_append(&page_items, current, PH_SEPARATE); if (PHALCON_GE(i, show)) { break; } phalcon_increment(i); } } phalcon_update_property_zval(page, SL("items"), page_items TSRMLS_CC); PHALCON_INIT_VAR(maximum_pages); phalcon_add_function(maximum_pages, start, show TSRMLS_CC); if (PHALCON_LT(maximum_pages, n)) { PHALCON_INIT_VAR(next); phalcon_add_function(next, page_number, one TSRMLS_CC); } else { if (PHALCON_IS_EQUAL(maximum_pages, n)) { PHALCON_CPY_WRT(next, n); } else { div_function(possible_pages, n, show TSRMLS_CC); PHALCON_INIT_VAR(additional_page); phalcon_add_function(additional_page, possible_pages, one TSRMLS_CC); PHALCON_INIT_NVAR(next); ZVAL_LONG(next, phalcon_get_intval(additional_page)); } } if (PHALCON_GT(next, total_pages)) { PHALCON_CPY_WRT(next, total_pages); } phalcon_update_property_zval(page, SL("next"), next TSRMLS_CC); if (PHALCON_GT(page_number, one)) { PHALCON_INIT_VAR(before); sub_function(before, page_number, one TSRMLS_CC); } else { PHALCON_CPY_WRT(before, one); } phalcon_update_property_zval(page, SL("first"), one TSRMLS_CC); phalcon_update_property_zval(page, SL("before"), before TSRMLS_CC); phalcon_update_property_zval(page, SL("current"), page_number TSRMLS_CC); PHALCON_INIT_VAR(remainder); mod_function(remainder, n, show TSRMLS_CC); PHALCON_INIT_NVAR(possible_pages); div_function(possible_pages, n, show TSRMLS_CC); if (!PHALCON_IS_LONG(remainder, 0)) { PHALCON_INIT_NVAR(next); phalcon_add_function(next, possible_pages, one TSRMLS_CC); PHALCON_INIT_VAR(pages_total); ZVAL_LONG(pages_total, phalcon_get_intval(next)); } else { PHALCON_CPY_WRT(pages_total, possible_pages); } phalcon_update_property_zval(page, SL("last"), pages_total TSRMLS_CC); phalcon_update_property_zval(page, SL("total_pages"), pages_total TSRMLS_CC); phalcon_update_property_zval(page, SL("total_items"), n TSRMLS_CC); RETURN_CTOR(page); }
/** * Shows a backtrace item * * @param int $n * @param array $trace */ PHP_METHOD(Phalcon_Debug, showTraceItem){ zval *n, *trace, *link_format, *space, *two_spaces, *underscore; zval *minus, *html, *class_name; zval *namespace_separator, *prepare_uri_class; zval *lower_class_name, *prepared_function_name; zval *prepare_internal_class, *type, *function_name = NULL; zval *trace_args, *arguments, *argument = NULL, *dumped_argument = NULL; zval *span_argument = NULL, *joined_arguments, *z_one; zval *file, *line, *show_files, *lines = NULL, *number_lines; zval *show_file_fragment, *before_context, *before_line; zval *first_line = NULL, *after_context, *after_line, *last_line = NULL; zval *comment_pattern, *charset, *tab; zval *comment, *i = NULL, *line_position = NULL, *current_line = NULL; zval *trimmed = NULL, *is_comment = NULL, *spaced_current_line = NULL; zval *escaped_line = NULL, *formatted_file = NULL; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 3, 0, &n, &trace, &link_format); PHALCON_INIT_VAR(space); ZVAL_STRING(space, " ", 1); PHALCON_INIT_VAR(two_spaces); ZVAL_STRING(two_spaces, " ", 1); PHALCON_INIT_VAR(underscore); ZVAL_STRING(underscore, "_", 1); PHALCON_INIT_VAR(minus); ZVAL_STRING(minus, "-", 1); /** * Every trace in the backtrace have a unique number */ PHALCON_INIT_VAR(html); PHALCON_CONCAT_SVS(html, "<tr><td align=\"right\" valign=\"top\" class=\"error-number\">#", n, "</td><td>"); if (phalcon_array_isset_string(trace, SS("class"))) { zend_class_entry *class_ce; PHALCON_OBS_VAR(class_name); phalcon_array_fetch_string(&class_name, trace, SL("class"), PH_NOISY); class_ce = zend_fetch_class(Z_STRVAL_P(class_name), Z_STRLEN_P(class_name), ZEND_FETCH_CLASS_AUTO | ZEND_FETCH_CLASS_SILENT TSRMLS_CC); if (!class_ce) { /* Unable to load the class, should never happen */ } else if (is_phalcon_class(class_ce)) { PHALCON_INIT_VAR(namespace_separator); ZVAL_STRING(namespace_separator, "\\", 1); /* Prepare the class name according to the Phalcon's conventions */ PHALCON_INIT_VAR(prepare_uri_class); phalcon_fast_str_replace(prepare_uri_class, namespace_separator, underscore, class_name); /* Generate a link to the official docs */ PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://docs.phalconphp.com/en/latest/api/", prepare_uri_class, ".html\">", class_name, "</a></span>"); } else if (class_ce->type == ZEND_INTERNAL_CLASS) { PHALCON_INIT_VAR(lower_class_name); phalcon_fast_strtolower(lower_class_name, class_name); PHALCON_INIT_VAR(prepare_internal_class); phalcon_fast_str_replace(prepare_internal_class, underscore, minus, lower_class_name); /* Generate a link to the official docs */ PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://php.net/manual/en/class.", prepare_internal_class, ".php\">", class_name, "</a></span>"); } else { PHALCON_SCONCAT_SVS(html, "<span class=\"error-class\">", class_name, "</span>"); } /** * Object access operator: static/instance */ PHALCON_OBS_VAR(type); phalcon_array_fetch_string(&type, trace, SL("type"), PH_NOISY); phalcon_concat_self(&html, type TSRMLS_CC); } /** * Normally the backtrace contains only classes */ if (phalcon_array_isset_string(trace, SS("class"))) { PHALCON_OBS_VAR(function_name); phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY); PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>"); } else { zend_function *func; PHALCON_OBS_NVAR(function_name); phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY); convert_to_string(function_name); /** * Check if the function exists */ if (phalcon_fetch_function(&func, Z_STRVAL_P(function_name), Z_STRLEN_P(function_name) TSRMLS_CC) == SUCCESS) { /** * Internal functions links to the PHP documentation */ if (func->type == ZEND_INTERNAL_FUNCTION) { /** * Prepare function's name according to the conventions in the docs */ PHALCON_INIT_VAR(prepared_function_name); phalcon_fast_str_replace(prepared_function_name, underscore, minus, function_name); PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-function\"><a target=\"_new\" href=\"http://php.net/manual/en/function.", prepared_function_name, ".php\">", function_name, "</a></span>"); } else { PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>"); } } else { PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>"); } } /** * Check for arguments in the function */ if (phalcon_array_isset_string(trace, SS("args"))) { PHALCON_OBS_VAR(trace_args); phalcon_array_fetch_string(&trace_args, trace, SL("args"), PH_NOISY); if (phalcon_fast_count_ev(trace_args TSRMLS_CC)) { PHALCON_INIT_VAR(arguments); array_init(arguments); phalcon_is_iterable(trace_args, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(argument); /** * Every argument is generated using _getVarDump */ PHALCON_CALL_METHOD(&dumped_argument, this_ptr, "_getvardump", argument); PHALCON_INIT_NVAR(span_argument); PHALCON_CONCAT_SVS(span_argument, "<span class=\"error-parameter\">", dumped_argument, "</span>"); /** * Append the HTML generated to the argument's list */ phalcon_array_append(&arguments, span_argument, PH_COPY); zend_hash_move_forward_ex(ah0, &hp0); } /** * Join all the arguments */ PHALCON_INIT_VAR(joined_arguments); phalcon_fast_join_str(joined_arguments, SL(", "), arguments TSRMLS_CC); PHALCON_SCONCAT_SVS(html, "(", joined_arguments, ")"); } else { phalcon_concat_self_str(&html, SL("()") TSRMLS_CC); } } /** * When 'file' is present, it usually means the function is provided by the user */ if (phalcon_array_isset_string(trace, SS("file"))) { z_one = PHALCON_GLOBAL(z_one); PHALCON_OBS_VAR(file); phalcon_array_fetch_string(&file, trace, SL("file"), PH_NOISY); PHALCON_OBS_VAR(line); phalcon_array_fetch_string(&line, trace, SL("line"), PH_NOISY); PHALCON_CALL_METHOD(&formatted_file, getThis(), "getfilelink", file, line, link_format); /** * Realpath to the file and its line using a special header */ PHALCON_SCONCAT_SVSVS(html, "<br/><div class=\"error-file\">", formatted_file, " (", line, ")</div>"); PHALCON_OBS_VAR(show_files); phalcon_read_property_this(&show_files, this_ptr, SL("_showFiles"), PH_NOISY TSRMLS_CC); /** * The developer can change if the files must be opened or not */ if (zend_is_true(show_files)) { /** * Open the file to an array using 'file', this respects the openbase-dir directive */ PHALCON_CALL_FUNCTION(&lines, "file", file); PHALCON_INIT_VAR(number_lines); phalcon_fast_count(number_lines, lines TSRMLS_CC); PHALCON_OBS_VAR(show_file_fragment); phalcon_read_property_this(&show_file_fragment, this_ptr, SL("_showFileFragment"), PH_NOISY TSRMLS_CC); /** * File fragments just show a piece of the file where the exception is located */ if (zend_is_true(show_file_fragment)) { /** * Take lines back to the current exception's line */ before_context = phalcon_fetch_nproperty_this(getThis(), SL("_beforeContext"), PH_NOISY TSRMLS_CC); PHALCON_INIT_VAR(before_line); phalcon_sub_function(before_line, line, before_context); /** * Check for overflows */ if (PHALCON_LT_LONG(before_line, 1)) { PHALCON_CPY_WRT_CTOR(first_line, z_one); } else { PHALCON_CPY_WRT(first_line, before_line); } /** * Take lines after the current exception's line */ after_context = phalcon_fetch_nproperty_this(getThis(), SL("_afterContext"), PH_NOISY TSRMLS_CC); PHALCON_INIT_VAR(after_line); phalcon_add_function(after_line, line, after_context); /** * Check for overflows */ if (PHALCON_GT(after_line, number_lines)) { PHALCON_CPY_WRT(last_line, number_lines); } else { PHALCON_CPY_WRT(last_line, after_line); } PHALCON_SCONCAT_SVSVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums:", first_line, "'>"); } else { PHALCON_CPY_WRT_CTOR(first_line, z_one); PHALCON_CPY_WRT(last_line, number_lines); PHALCON_SCONCAT_SVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums error-scroll'>"); } PHALCON_INIT_VAR(comment_pattern); ZVAL_STRING(comment_pattern, "#\\*\\/$#", 1); charset = phalcon_fetch_static_property_ce(phalcon_debug_ce, SL("_charset") TSRMLS_CC); PHALCON_INIT_VAR(tab); ZVAL_STRING(tab, "\t", 1); PHALCON_INIT_VAR(comment); ZVAL_STRING(comment, "* /", 1); PHALCON_CPY_WRT(i, first_line); while (PHALCON_LE(i, last_line)) { /** * Current line in the file */ PHALCON_INIT_NVAR(line_position); phalcon_sub_function(line_position, i, z_one); /** * Current line content in the piece of file */ PHALCON_OBS_NVAR(current_line); phalcon_array_fetch(¤t_line, lines, line_position, PH_NOISY); /** * File fragments are cleaned, removing tabs and comments */ if (zend_is_true(show_file_fragment)) { if (PHALCON_IS_EQUAL(i, first_line)) { PHALCON_INIT_NVAR(trimmed); phalcon_fast_trim(trimmed, current_line, NULL, PHALCON_TRIM_RIGHT TSRMLS_CC); PHALCON_INIT_NVAR(is_comment); RETURN_MM_ON_FAILURE(phalcon_preg_match(is_comment, comment_pattern, current_line, NULL TSRMLS_CC)); if (zend_is_true(is_comment)) { PHALCON_INIT_NVAR(spaced_current_line); phalcon_fast_str_replace(spaced_current_line, comment, space, current_line); PHALCON_CPY_WRT(current_line, spaced_current_line); } } } /** * Print a non break space if the current line is a line break, this allows to show * the html zebra properly */ if (PHALCON_IS_STRING(current_line, "\n")) { phalcon_concat_self_str(&html, SL(" \n") TSRMLS_CC); } else { if (PHALCON_IS_STRING(current_line, "\r\n")) { phalcon_concat_self_str(&html, SL(" \n") TSRMLS_CC); } else { PHALCON_INIT_NVAR(spaced_current_line); phalcon_fast_str_replace(spaced_current_line, tab, two_spaces, current_line); PHALCON_INIT_NVAR(escaped_line); phalcon_htmlentities(escaped_line, spaced_current_line, NULL, charset TSRMLS_CC); phalcon_concat_self(&html, escaped_line TSRMLS_CC); } } phalcon_increment(i); } phalcon_concat_self_str(&html, SL("</pre>") TSRMLS_CC); } } phalcon_concat_self_str(&html, SL("</td></tr>") TSRMLS_CC); RETURN_CTOR(html); }
/** * Returns a slice of the resultset to show in the pagination * * @return stdClass */ PHP_METHOD(Phalcon_Paginator_Adapter_NativeArray, getPaginate){ zval *one, *config, *items, *show, *page_number = NULL, *page; zval *number, *rounded_total, *total_pages, *before_page_number; zval *start, *slice, *next = NULL, *before = NULL; PHALCON_MM_GROW(); /** * TODO: Rewrite the whole method! */ PHALCON_INIT_VAR(one); ZVAL_LONG(one, 1); PHALCON_OBS_VAR(config); phalcon_read_property_this(&config, this_ptr, SL("_config"), PH_NOISY_CC); PHALCON_OBS_VAR(items); phalcon_array_fetch_string(&items, config, SL("data"), PH_NOISY); if (Z_TYPE_P(items) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_paginator_exception_ce, "Invalid data for paginator"); return; } PHALCON_OBS_VAR(show); phalcon_read_property_this(&show, this_ptr, SL("_limitRows"), PH_NOISY_CC); PHALCON_OBS_VAR(page_number); phalcon_read_property_this(&page_number, this_ptr, SL("_page"), PH_NOISY_CC); if (!zend_is_true(page_number)) { PHALCON_CPY_WRT(page_number, one); } PHALCON_INIT_VAR(page); object_init(page); PHALCON_INIT_VAR(number); phalcon_fast_count(number, items TSRMLS_CC); PHALCON_INIT_VAR(rounded_total); div_function(rounded_total, number, show TSRMLS_CC); PHALCON_INIT_VAR(total_pages); ZVAL_LONG(total_pages, phalcon_get_intval(rounded_total)); /** * Increase total_pages if wasn't integer */ if (!PHALCON_IS_EQUAL(total_pages, rounded_total)) { phalcon_increment(total_pages); } PHALCON_INIT_VAR(before_page_number); sub_function(before_page_number, page_number, one TSRMLS_CC); PHALCON_INIT_VAR(start); mul_function(start, show, before_page_number TSRMLS_CC); PHALCON_INIT_VAR(slice); phalcon_call_func_p3(slice, "array_slice", items, start, show); phalcon_update_property_zval(page, SL("items"), slice TSRMLS_CC); if (PHALCON_LT(page_number, total_pages)) { PHALCON_INIT_VAR(next); phalcon_add_function(next, page_number, one TSRMLS_CC); } else { PHALCON_CPY_WRT(next, total_pages); } phalcon_update_property_zval(page, SL("next"), next TSRMLS_CC); if (PHALCON_GT(page_number, one)) { PHALCON_INIT_VAR(before); sub_function(before, page_number, one TSRMLS_CC); } else { PHALCON_CPY_WRT(before, one); } phalcon_update_property_zval(page, SL("first"), one TSRMLS_CC); phalcon_update_property_zval(page, SL("before"), before TSRMLS_CC); phalcon_update_property_zval(page, SL("current"), page_number TSRMLS_CC); phalcon_update_property_zval(page, SL("last"), total_pages TSRMLS_CC); phalcon_update_property_zval(page, SL("total_pages"), total_pages TSRMLS_CC); phalcon_update_property_zval(page, SL("total_items"), number TSRMLS_CC); RETURN_CTOR(page); }