@@ -46,7 +46,8 @@ PHONGO_API zend_class_entry *php_phongo_objectid_ce;
46
46
47
47
zend_object_handlers php_phongo_handler_objectid ;
48
48
49
- /* Initialize the object with a generated value and return whether it was successful. */
49
+ /* Initialize the object with a generated value and return whether it was
50
+ * successful. */
50
51
static bool php_phongo_objectid_init (php_phongo_objectid_t * intern )
51
52
{
52
53
bson_oid_t oid ;
@@ -59,8 +60,9 @@ static bool php_phongo_objectid_init(php_phongo_objectid_t *intern)
59
60
return true;
60
61
}
61
62
62
- /* Initialize the object from a hex string and return whether it was successful. */
63
- static bool php_phongo_objectid_init_from_hex_string (php_phongo_objectid_t * intern , const char * oid , phongo_zpp_char_len oid_len )
63
+ /* Initialize the object from a hex string and return whether it was successful.
64
+ * An exception will be thrown on error. */
65
+ static bool php_phongo_objectid_init_from_hex_string (php_phongo_objectid_t * intern , const char * oid , phongo_zpp_char_len oid_len TSRMLS_DC )
64
66
{
65
67
char * tid = zend_str_tolower_dup (oid , oid_len );
66
68
@@ -75,28 +77,33 @@ static bool php_phongo_objectid_init_from_hex_string(php_phongo_objectid_t *inte
75
77
return true;
76
78
}
77
79
80
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Invalid BSON ID provided" );
81
+
78
82
efree (tid );
79
83
return false;
80
84
}
81
85
82
- /* Initialize the object from a HashTable and return whether it was successful. */
83
- static bool php_phongo_objectid_init_from_hash (php_phongo_objectid_t * intern , HashTable * props )
86
+ /* Initialize the object from a HashTable and return whether it was successful.
87
+ * An exception will be thrown on error. */
88
+ static bool php_phongo_objectid_init_from_hash (php_phongo_objectid_t * intern , HashTable * props TSRMLS_DC )
84
89
{
85
90
#if PHP_VERSION_ID >= 70000
86
91
zval * z_oid ;
87
92
88
93
z_oid = zend_hash_str_find (props , "oid" , sizeof ("oid" )- 1 );
89
94
90
95
if (z_oid && Z_TYPE_P (z_oid ) == IS_STRING ) {
91
- return php_phongo_objectid_init_from_hex_string (intern , Z_STRVAL_P (z_oid ), Z_STRLEN_P (z_oid ));
96
+ return php_phongo_objectid_init_from_hex_string (intern , Z_STRVAL_P (z_oid ), Z_STRLEN_P (z_oid ) TSRMLS_CC );
92
97
}
93
98
#else
94
99
zval * * z_oid ;
95
100
96
101
if (zend_hash_find (props , "oid" , sizeof ("oid" ), (void * * ) & z_oid ) == SUCCESS && Z_TYPE_PP (z_oid ) == IS_STRING ) {
97
- return php_phongo_objectid_init_from_hex_string (intern , Z_STRVAL_PP (z_oid ), Z_STRLEN_PP (z_oid ));
102
+ return php_phongo_objectid_init_from_hex_string (intern , Z_STRVAL_PP (z_oid ), Z_STRLEN_PP (z_oid ) TSRMLS_CC );
98
103
}
99
104
#endif
105
+
106
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "%s initialization requires \"oid\" string field" , ZSTR_VAL (php_phongo_objectid_ce -> name ));
100
107
return false;
101
108
}
102
109
@@ -120,9 +127,7 @@ PHP_METHOD(ObjectID, __construct)
120
127
zend_restore_error_handling (& error_handling TSRMLS_CC );
121
128
122
129
if (id ) {
123
- if (!php_phongo_objectid_init_from_hex_string (intern , id , id_len )) {
124
- phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "%s" , "Invalid BSON ID provided" );
125
- }
130
+ php_phongo_objectid_init_from_hex_string (intern , id , id_len TSRMLS_CC );
126
131
} else {
127
132
php_phongo_objectid_init (intern );
128
133
}
@@ -146,9 +151,7 @@ PHP_METHOD(ObjectID, __set_state)
146
151
intern = Z_OBJECTID_OBJ_P (return_value );
147
152
props = Z_ARRVAL_P (array );
148
153
149
- if (!php_phongo_objectid_init_from_hash (intern , props )) {
150
- php_error (E_ERROR , "Invalid serialization data for ObjectID object" );
151
- }
154
+ php_phongo_objectid_init_from_hash (intern , props TSRMLS_CC );
152
155
}
153
156
/* }}} */
154
157
@@ -184,9 +187,7 @@ PHP_METHOD(ObjectID, __wakeup)
184
187
intern = Z_OBJECTID_OBJ_P (getThis ());
185
188
props = zend_std_get_properties (getThis () TSRMLS_CC );
186
189
187
- if (!php_phongo_objectid_init_from_hash (intern , props )) {
188
- php_error (E_ERROR , "Invalid serialization data for ObjectID object" );
189
- }
190
+ php_phongo_objectid_init_from_hash (intern , props TSRMLS_CC );
190
191
}
191
192
/* }}} */
192
193
0 commit comments