Skip to content

Commit

Permalink
Merge pull request #1178 from charnad/1.3.0
Browse files Browse the repository at this point in the history
NFR #1171 Adding code support validation messages
  • Loading branch information
Phalcon committed Sep 1, 2013
2 parents 0dadbcb + a95d0be commit 529aab8
Show file tree
Hide file tree
Showing 28 changed files with 517 additions and 55 deletions.
52 changes: 45 additions & 7 deletions ext/mvc/model/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
* $text = "A robot cannot be named Peter";
* $field = "name";
* $type = "InvalidValue";
* $message = new Message($text, $field, $type);
* $code = 103;
* $message = new Message($text, $field, $type, $code);
* $this->appendMessage($message);
* }
* }
Expand All @@ -75,6 +76,7 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_Model_Message){
zend_declare_property_null(phalcon_mvc_model_message_ce, SL("_message"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_mvc_model_message_ce, SL("_field"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_mvc_model_message_ce, SL("_model"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_long(phalcon_mvc_model_message_ce, SL("_code"), 0, ZEND_ACC_PROTECTED TSRMLS_CC);

zend_class_implements(phalcon_mvc_model_message_ce TSRMLS_CC, 1, phalcon_mvc_model_messageinterface_ce);

Expand All @@ -91,11 +93,11 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_Model_Message){
*/
PHP_METHOD(Phalcon_Mvc_Model_Message, __construct){

zval *message, *field = NULL, *type = NULL, *model = NULL;
zval *message, *field = NULL, *type = NULL, *model = NULL, *code = NULL;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 3, &message, &field, &type, &model);
phalcon_fetch_params(1, 1, 4, &message, &field, &type, &code, &model);

if (!field) {
PHALCON_INIT_VAR(field);
Expand All @@ -108,13 +110,19 @@ PHP_METHOD(Phalcon_Mvc_Model_Message, __construct){
if (!model) {
PHALCON_INIT_VAR(model);
}

if (!code) {
PHALCON_INIT_VAR(code);
ZVAL_LONG(code, 0);
}

phalcon_update_property_this(this_ptr, SL("_message"), message TSRMLS_CC);
phalcon_update_property_this(this_ptr, SL("_field"), field TSRMLS_CC);
phalcon_update_property_this(this_ptr, SL("_type"), type TSRMLS_CC);
if (Z_TYPE_P(model) == IS_OBJECT) {
phalcon_update_property_this(this_ptr, SL("_model"), model TSRMLS_CC);
}
phalcon_update_property_this(this_ptr, SL("_code"), code TSRMLS_CC);

PHALCON_MM_RESTORE();
}
Expand Down Expand Up @@ -146,6 +154,32 @@ PHP_METHOD(Phalcon_Mvc_Model_Message, getType){
RETURN_MEMBER(this_ptr, "_type");
}

/**
* Sets message code
*
* @param string $code
* @return Phalcon\Mvc\Model\Message
*/
PHP_METHOD(Phalcon_Mvc_Model_Message, setCode){

zval *code;

phalcon_fetch_params(0, 1, 0, &code);

phalcon_update_property_this(this_ptr, SL("_code"), code TSRMLS_CC);
RETURN_THISW();
}

/**
* Returns message code
*
* @return string
*/
PHP_METHOD(Phalcon_Mvc_Model_Message, getCode){

RETURN_MEMBER(this_ptr, "_code");
}

/**
* Sets verbose message
*
Expand Down Expand Up @@ -246,22 +280,26 @@ PHP_METHOD(Phalcon_Mvc_Model_Message, __toString){
*/
PHP_METHOD(Phalcon_Mvc_Model_Message, __set_state){

zval *message, *message_text, *field, *type;
zval *message, *message_text, *field, *type, *code;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 0, &message);

PHALCON_OBS_VAR(message_text);
phalcon_array_fetch_string(&message_text, message, SL("_message"), PH_NOISY);

PHALCON_OBS_VAR(field);
phalcon_array_fetch_string(&field, message, SL("_field"), PH_NOISY);

PHALCON_OBS_VAR(type);
phalcon_array_fetch_string(&type, message, SL("_type"), PH_NOISY);

PHALCON_OBS_VAR(code);
phalcon_array_fetch_string(&code, message, SL("_code"), PH_NOISY);

object_init_ex(return_value, phalcon_mvc_model_message_ce);
phalcon_call_method_p3_noret(return_value, "__construct", message_text, field, type);
phalcon_call_method_p4_noret(return_value, "__construct", message_text, field, type, code);

RETURN_MM();
}
Expand Down
9 changes: 9 additions & 0 deletions ext/mvc/model/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_Model_Message);
PHP_METHOD(Phalcon_Mvc_Model_Message, __construct);
PHP_METHOD(Phalcon_Mvc_Model_Message, setType);
PHP_METHOD(Phalcon_Mvc_Model_Message, getType);
PHP_METHOD(Phalcon_Mvc_Model_Message, setCode);
PHP_METHOD(Phalcon_Mvc_Model_Message, getCode);
PHP_METHOD(Phalcon_Mvc_Model_Message, setMessage);
PHP_METHOD(Phalcon_Mvc_Model_Message, getMessage);
PHP_METHOD(Phalcon_Mvc_Model_Message, setField);
Expand All @@ -38,6 +40,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_mvc_model_message___construct, 0, 0, 1)
ZEND_ARG_INFO(0, field)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, model)
ZEND_ARG_INFO(0, code)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_mvc_model_message_settype, 0, 0, 1)
Expand All @@ -56,6 +59,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_mvc_model_message_setmodel, 0, 0, 1)
ZEND_ARG_INFO(0, model)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_mvc_model_message_setcode, 0, 0, 1)
ZEND_ARG_INFO(0, code)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_mvc_model_message___set_state, 0, 0, 1)
ZEND_ARG_INFO(0, message)
ZEND_END_ARG_INFO()
Expand All @@ -64,6 +71,8 @@ PHALCON_INIT_FUNCS(phalcon_mvc_model_message_method_entry){
PHP_ME(Phalcon_Mvc_Model_Message, __construct, arginfo_phalcon_mvc_model_message___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(Phalcon_Mvc_Model_Message, setType, arginfo_phalcon_mvc_model_message_settype, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Model_Message, getType, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Model_Message, setCode, arginfo_phalcon_mvc_model_message_setcode, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Model_Message, getCode, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Model_Message, setMessage, arginfo_phalcon_mvc_model_message_setmessage, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Model_Message, getMessage, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Model_Message, setField, arginfo_phalcon_mvc_model_message_setfield, ZEND_ACC_PUBLIC)
Expand Down
11 changes: 8 additions & 3 deletions ext/mvc/model/validator.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ PHP_METHOD(Phalcon_Mvc_Model_Validator, __construct){
*/
PHP_METHOD(Phalcon_Mvc_Model_Validator, appendMessage){

zval *message, *field = NULL, *type = NULL, *class_name, *suffix;
zval *message, *field = NULL, *type = NULL, *code = NULL, *class_name, *suffix;
zval *empty_string, *model_message;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 2, &message, &field, &type);
phalcon_fetch_params(1, 1, 3, &message, &field, &type, &code);

if (!field) {
PHALCON_INIT_VAR(field);
Expand All @@ -102,6 +102,11 @@ PHP_METHOD(Phalcon_Mvc_Model_Validator, appendMessage){
} else {
PHALCON_SEPARATE_PARAM(type);
}

if (!code) {
PHALCON_INIT_VAR(code);
ZVAL_LONG(code, 0);
}

if (!zend_is_true(type)) {
PHALCON_INIT_VAR(class_name);
Expand All @@ -119,7 +124,7 @@ PHP_METHOD(Phalcon_Mvc_Model_Validator, appendMessage){

PHALCON_INIT_VAR(model_message);
object_init_ex(model_message, phalcon_mvc_model_message_ce);
phalcon_call_method_p3_noret(model_message, "__construct", message, field, type);
phalcon_call_method_p4_noret(model_message, "__construct", message, field, type, code);

phalcon_update_property_array_append(this_ptr, SL("_messages"), model_message TSRMLS_CC);

Expand Down
20 changes: 18 additions & 2 deletions ext/mvc/model/validator/email.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ PHP_METHOD(Phalcon_Mvc_Model_Validator_Email, validate){

zval *record, *option = NULL, *field_name, *regs, *invalid = NULL;
zval *value, *pattern, *match_pattern, *match_zero;
zval *message = NULL, *type;
zval *message = NULL, *type, *is_set_code, *code;

PHALCON_MM_GROW();

Expand Down Expand Up @@ -148,7 +148,23 @@ PHP_METHOD(Phalcon_Mvc_Model_Validator_Email, validate){

PHALCON_INIT_VAR(type);
ZVAL_STRING(type, "Email", 1);
phalcon_call_method_p3_noret(this_ptr, "appendmessage", message, field_name, type);

/*
* Is code set
*/
PHALCON_INIT_NVAR(option);
ZVAL_STRING(option, "code", 1);

PHALCON_INIT_VAR(is_set_code);
phalcon_call_method_p1(is_set_code, this_ptr, "issetoption", option);
PHALCON_INIT_VAR(code);
if (zend_is_true(is_set_code)) {
phalcon_call_method_p1(code, this_ptr, "getoption", option);
} else {
ZVAL_LONG(code, 0);
}

phalcon_call_method_p4_noret(this_ptr, "appendmessage", message, field_name, type, code);
RETURN_MM_FALSE;
}

Expand Down
20 changes: 18 additions & 2 deletions ext/mvc/model/validator/exclusionin.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_Model_Validator_Exclusionin){
PHP_METHOD(Phalcon_Mvc_Model_Validator_Exclusionin, validate){

zval *record, *option = NULL, *field_name, *is_set, *domain;
zval *value, *message = NULL, *joined_domain, *type;
zval *value, *message = NULL, *joined_domain, *type, *is_set_code, *code;

PHALCON_MM_GROW();

Expand Down Expand Up @@ -153,7 +153,23 @@ PHP_METHOD(Phalcon_Mvc_Model_Validator_Exclusionin, validate){

PHALCON_INIT_VAR(type);
ZVAL_STRING(type, "Exclusion", 1);
phalcon_call_method_p3_noret(this_ptr, "appendmessage", message, field_name, type);

/*
* Is code set
*/
PHALCON_INIT_NVAR(option);
ZVAL_STRING(option, "code", 1);

PHALCON_INIT_VAR(is_set_code);
phalcon_call_method_p1(is_set_code, this_ptr, "issetoption", option);
PHALCON_INIT_VAR(code);
if (zend_is_true(is_set_code)) {
phalcon_call_method_p1(code, this_ptr, "getoption", option);
} else {
ZVAL_LONG(code, 0);
}

phalcon_call_method_p4_noret(this_ptr, "appendmessage", message, field_name, type, code);
RETURN_MM_FALSE;
}

Expand Down
20 changes: 18 additions & 2 deletions ext/mvc/model/validator/inclusionin.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_Model_Validator_Inclusionin){
PHP_METHOD(Phalcon_Mvc_Model_Validator_Inclusionin, validate){

zval *record, *field = NULL, *field_name, *is_set, *domain;
zval *value, *option, *message = NULL, *joined_domain;
zval *value, *option, *message = NULL, *joined_domain, *is_set_code, *code;
zval *type;

PHALCON_MM_GROW();
Expand Down Expand Up @@ -153,7 +153,23 @@ PHP_METHOD(Phalcon_Mvc_Model_Validator_Inclusionin, validate){

PHALCON_INIT_VAR(type);
ZVAL_STRING(type, "Inclusion", 1);
phalcon_call_method_p3_noret(this_ptr, "appendmessage", message, field_name, type);

/*
* Is code set
*/
PHALCON_INIT_NVAR(option);
ZVAL_STRING(option, "code", 1);

PHALCON_INIT_VAR(is_set_code);
phalcon_call_method_p1(is_set_code, this_ptr, "issetoption", option);
PHALCON_INIT_VAR(code);
if (zend_is_true(is_set_code)) {
phalcon_call_method_p1(code, this_ptr, "getoption", option);
} else {
ZVAL_LONG(code, 0);
}

phalcon_call_method_p4_noret(this_ptr, "appendmessage", message, field_name, type, code);
RETURN_MM_FALSE;
}

Expand Down
20 changes: 18 additions & 2 deletions ext/mvc/model/validator/numericality.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_Model_Validator_Numericality){
*/
PHP_METHOD(Phalcon_Mvc_Model_Validator_Numericality, validate){

zval *record, *option = NULL, *field, *value, *message = NULL, *type;
zval *record, *option = NULL, *field, *value, *message = NULL, *type, *is_set_code, *code;

PHALCON_MM_GROW();

Expand Down Expand Up @@ -123,7 +123,23 @@ PHP_METHOD(Phalcon_Mvc_Model_Validator_Numericality, validate){

PHALCON_INIT_VAR(type);
ZVAL_STRING(type, "Numericality", 1);
phalcon_call_method_p3_noret(this_ptr, "appendmessage", message, field, type);

/*
* Is code set
*/
PHALCON_INIT_NVAR(option);
ZVAL_STRING(option, "code", 1);

PHALCON_INIT_VAR(is_set_code);
phalcon_call_method_p1(is_set_code, this_ptr, "issetoption", option);
PHALCON_INIT_VAR(code);
if (zend_is_true(is_set_code)) {
phalcon_call_method_p1(code, this_ptr, "getoption", option);
} else {
ZVAL_LONG(code, 0);
}

phalcon_call_method_p4_noret(this_ptr, "appendmessage", message, field, type, code);
RETURN_MM_FALSE;
}

Expand Down
20 changes: 18 additions & 2 deletions ext/mvc/model/validator/presenceof.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_Model_Validator_PresenceOf){
PHP_METHOD(Phalcon_Mvc_Model_Validator_PresenceOf, validate){

zval *record, *option = NULL, *field_name, *value, *message = NULL;
zval *type;
zval *type, *is_set_code, *code;

PHALCON_MM_GROW();

Expand Down Expand Up @@ -124,7 +124,23 @@ PHP_METHOD(Phalcon_Mvc_Model_Validator_PresenceOf, validate){

PHALCON_INIT_VAR(type);
ZVAL_STRING(type, "PresenceOf", 1);
phalcon_call_method_p3_noret(this_ptr, "appendmessage", message, field_name, type);

/*
* Is code set
*/
PHALCON_INIT_NVAR(option);
ZVAL_STRING(option, "code", 1);

PHALCON_INIT_VAR(is_set_code);
phalcon_call_method_p1(is_set_code, this_ptr, "issetoption", option);
PHALCON_INIT_VAR(code);
if (zend_is_true(is_set_code)) {
phalcon_call_method_p1(code, this_ptr, "getoption", option);
} else {
ZVAL_LONG(code, 0);
}

phalcon_call_method_p4_noret(this_ptr, "appendmessage", message, field_name, type, code);
RETURN_MM_FALSE;
}

Expand Down
20 changes: 18 additions & 2 deletions ext/mvc/model/validator/regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ PHP_METHOD(Phalcon_Mvc_Model_Validator_Regex, validate){

zval *record, *option = NULL, *field_name, *is_set, *value;
zval *failed = NULL, *matches, *pattern, *match_pattern;
zval *match_zero, *message = NULL, *type;
zval *match_zero, *message = NULL, *type, *is_set_code, *code;

PHALCON_MM_GROW();

Expand Down Expand Up @@ -165,7 +165,23 @@ PHP_METHOD(Phalcon_Mvc_Model_Validator_Regex, validate){

PHALCON_INIT_VAR(type);
ZVAL_STRING(type, "Regex", 1);
phalcon_call_method_p3_noret(this_ptr, "appendmessage", message, field_name, type);

/*
* Is code set
*/
PHALCON_INIT_NVAR(option);
ZVAL_STRING(option, "code", 1);

PHALCON_INIT_VAR(is_set_code);
phalcon_call_method_p1(is_set_code, this_ptr, "issetoption", option);
PHALCON_INIT_VAR(code);
if (zend_is_true(is_set_code)) {
phalcon_call_method_p1(code, this_ptr, "getoption", option);
} else {
ZVAL_LONG(code, 0);
}

phalcon_call_method_p4_noret(this_ptr, "appendmessage", message, field_name, type, code);
RETURN_MM_FALSE;
}

Expand Down
Loading

0 comments on commit 529aab8

Please sign in to comment.