@@ -46,7 +46,8 @@ PHONGO_API zend_class_entry *php_phongo_objectid_ce;
4646
4747zend_object_handlers php_phongo_handler_objectid ;
4848
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. */
5051static bool php_phongo_objectid_init (php_phongo_objectid_t * intern )
5152{
5253 bson_oid_t oid ;
@@ -59,8 +60,9 @@ static bool php_phongo_objectid_init(php_phongo_objectid_t *intern)
5960 return true;
6061}
6162
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 )
6466{
6567 char * tid = zend_str_tolower_dup (oid , oid_len );
6668
@@ -75,28 +77,33 @@ static bool php_phongo_objectid_init_from_hex_string(php_phongo_objectid_t *inte
7577 return true;
7678 }
7779
80+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Invalid BSON ID provided" );
81+
7882 efree (tid );
7983 return false;
8084}
8185
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 )
8489{
8590#if PHP_VERSION_ID >= 70000
8691 zval * z_oid ;
8792
8893 z_oid = zend_hash_str_find (props , "oid" , sizeof ("oid" )- 1 );
8994
9095 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 );
9297 }
9398#else
9499 zval * * z_oid ;
95100
96101 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 );
98103 }
99104#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 ));
100107 return false;
101108}
102109
@@ -120,9 +127,7 @@ PHP_METHOD(ObjectID, __construct)
120127 zend_restore_error_handling (& error_handling TSRMLS_CC );
121128
122129 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 );
126131 } else {
127132 php_phongo_objectid_init (intern );
128133 }
@@ -146,9 +151,7 @@ PHP_METHOD(ObjectID, __set_state)
146151 intern = Z_OBJECTID_OBJ_P (return_value );
147152 props = Z_ARRVAL_P (array );
148153
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 );
152155}
153156/* }}} */
154157
@@ -184,9 +187,7 @@ PHP_METHOD(ObjectID, __wakeup)
184187 intern = Z_OBJECTID_OBJ_P (getThis ());
185188 props = zend_std_get_properties (getThis () TSRMLS_CC );
186189
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 );
190191}
191192/* }}} */
192193
0 commit comments