Skip to content

Commit d39bf88

Browse files
committed
Merge pull request #705
2 parents 983aed6 + 03af904 commit d39bf88

15 files changed

+33
-44
lines changed

php_phongo.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ static bool process_read_concern(zval *option, bson_t *mongoc_opts TSRMLS_DC)
469469
phongo_throw_exception(
470470
PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC,
471471
"Expected 'readConcern' option to be 'MongoDB\\Driver\\ReadConcern', %s given",
472-
zend_get_type_by_const(Z_TYPE_P(option))
472+
PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(option)
473473
);
474474
return false;
475475
}
@@ -520,7 +520,7 @@ static bool process_read_preference(zval *option, bson_t *mongoc_opts, zval **zr
520520
phongo_throw_exception(
521521
PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC,
522522
"Expected 'readPreference' option to be 'MongoDB\\Driver\\ReadPreference', %s given",
523-
zend_get_type_by_const(Z_TYPE_P(option))
523+
PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(option)
524524
);
525525
return false;
526526
}
@@ -543,7 +543,7 @@ static bool process_write_concern(zval *option, bson_t *mongoc_opts, zval **zwri
543543
phongo_throw_exception(
544544
PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC,
545545
"Expected 'writeConcern' option to be 'MongoDB\\Driver\\WriteConcern', %s given",
546-
zend_get_type_by_const(Z_TYPE_P(option))
546+
PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(option)
547547
);
548548
return false;
549549
}

php_phongo.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#include "bson.h"
2222
#include "mongoc.h"
2323

24+
#include "phongo_compat.h"
25+
#include "php_phongo_classes.h"
26+
2427
#define phpext_mongodb_ptr &mongodb_module_entry
2528
extern zend_module_entry mongodb_module_entry;
2629

@@ -61,8 +64,6 @@ ZEND_TSRMLS_CACHE_EXTERN()
6164

6265
#define PHONGO_WRITE_CONCERN_W_MAJORITY "majority"
6366

64-
#include "php_phongo_classes.h"
65-
6667
/* This enum is necessary since mongoc_server_description_type_t is private and
6768
* we need to translate strings returned by mongoc_server_description_type() to
6869
* Server integer constants. */
@@ -194,6 +195,9 @@ zend_bool phongo_writeconcernerror_init(zval *return_value, bson_t *bson TSRMLS_
194195
} \
195196
} while(0);
196197

198+
#define PHONGO_ZVAL_CLASS_OR_TYPE_NAME(zv) (Z_TYPE(zv) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE(zv)->name) : zend_get_type_by_const(Z_TYPE(zv)))
199+
#define PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(zvp) PHONGO_ZVAL_CLASS_OR_TYPE_NAME(*(zvp))
200+
197201
#endif /* PHONGO_H */
198202

199203

src/BSON/Timestamp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ static PHP_METHOD(Timestamp, __construct)
147147
}
148148

149149
if (Z_TYPE_P(increment) != IS_STRING) {
150-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected increment to be an unsigned 32-bit integer or string, %s given", zend_get_type_by_const(Z_TYPE_P(increment)));
150+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected increment to be an unsigned 32-bit integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(increment));
151151
return;
152152
}
153153

@@ -156,7 +156,7 @@ static PHP_METHOD(Timestamp, __construct)
156156
}
157157

158158
if (Z_TYPE_P(timestamp) != IS_STRING) {
159-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected timestamp to be an unsigned 32-bit integer or string, %s given", zend_get_type_by_const(Z_TYPE_P(timestamp)));
159+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected timestamp to be an unsigned 32-bit integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(timestamp));
160160
return;
161161
}
162162

src/BSON/UTCDateTime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ static PHP_METHOD(UTCDateTime, __construct)
190190
}
191191

192192
if (Z_TYPE_P(milliseconds) != IS_STRING) {
193-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected integer or string, %s given", zend_get_type_by_const(Z_TYPE_P(milliseconds)));
193+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(milliseconds));
194194
return;
195195
}
196196

src/MongoDB/Query.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static bool php_phongo_query_opts_append_string(bson_t *opts, const char *opts_k
3535
zval *value = php_array_fetch(zarr, zarr_key);
3636

3737
if (Z_TYPE_P(value) != IS_STRING) {
38-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"%s\" %s to be string, %s given", zarr_key, zarr_key[0] == '$' ? "modifier" : "option", zend_get_type_by_const(Z_TYPE_P(value)));
38+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"%s\" %s to be string, %s given", zarr_key, zarr_key[0] == '$' ? "modifier" : "option", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(value));
3939
return false;
4040
}
4141

@@ -55,7 +55,7 @@ static bool php_phongo_query_opts_append_document(bson_t *opts, const char *opts
5555
bson_t b = BSON_INITIALIZER;
5656

5757
if (Z_TYPE_P(value) != IS_OBJECT && Z_TYPE_P(value) != IS_ARRAY) {
58-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"%s\" %s to be array or object, %s given", zarr_key, zarr_key[0] == '$' ? "modifier" : "option", zend_get_type_by_const(Z_TYPE_P(value)));
58+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"%s\" %s to be array or object, %s given", zarr_key, zarr_key[0] == '$' ? "modifier" : "option", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(value));
5959
return false;
6060
}
6161

@@ -195,7 +195,7 @@ static bool php_phongo_query_init_readconcern(php_phongo_query_t *intern, zval *
195195
zval *read_concern = php_array_fetchc(options, "readConcern");
196196

197197
if (Z_TYPE_P(read_concern) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(read_concern), php_phongo_readconcern_ce TSRMLS_CC)) {
198-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"readConcern\" option to be %s, %s given", ZSTR_VAL(php_phongo_readconcern_ce->name), zend_get_type_by_const(Z_TYPE_P(read_concern)));
198+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"readConcern\" option to be %s, %s given", ZSTR_VAL(php_phongo_readconcern_ce->name), PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(read_concern));
199199
return false;
200200
}
201201

@@ -262,7 +262,7 @@ static bool php_phongo_query_init(php_phongo_query_t *intern, zval *filter, zval
262262
modifiers = php_array_fetchc(options, "modifiers");
263263

264264
if (Z_TYPE_P(modifiers) != IS_ARRAY) {
265-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"modifiers\" option to be array, %s given", zend_get_type_by_const(Z_TYPE_P(modifiers)));
265+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"modifiers\" option to be array, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(modifiers));
266266
return false;
267267
}
268268
}

src/MongoDB/ReadPreference.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static PHP_METHOD(ReadPreference, __construct)
8080
return;
8181
}
8282
} else {
83-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected mode to be integer or string, %s given", zend_get_type_by_const(Z_TYPE_P(mode)));
83+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected mode to be integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(mode));
8484
return;
8585
}
8686

src/MongoDB/WriteConcern.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static PHP_METHOD(WriteConcern, __construct)
6262
mongoc_write_concern_set_wtag(intern->write_concern, Z_STRVAL_P(w));
6363
}
6464
} else {
65-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected w to be integer or string, %s given", zend_get_type_by_const(Z_TYPE_P(w)));
65+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected w to be integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(w));
6666
return;
6767
}
6868

src/bson-encode.c

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -143,24 +143,18 @@ static void php_phongo_bson_append_object(bson_t *bson, php_phongo_bson_flags_t
143143
if (Z_TYPE(obj_data) != IS_ARRAY && !(Z_TYPE(obj_data) == IS_OBJECT && instanceof_function(Z_OBJCE(obj_data), zend_standard_class_def TSRMLS_CC))) {
144144
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC,
145145
"Expected %s::%s() to return an array or stdClass, %s given",
146-
Z_OBJCE_P(object)->name->val,
146+
ZSTR_VAL(Z_OBJCE_P(object)->name),
147147
BSON_SERIALIZE_FUNC_NAME,
148-
(Z_TYPE(obj_data) == IS_OBJECT
149-
? Z_OBJCE(obj_data)->name->val
150-
: zend_get_type_by_const(Z_TYPE(obj_data))
151-
)
148+
PHONGO_ZVAL_CLASS_OR_TYPE_NAME(obj_data)
152149
);
153150
zval_ptr_dtor(&obj_data);
154151
#else
155152
if (Z_TYPE_P(obj_data) != IS_ARRAY && !(Z_TYPE_P(obj_data) == IS_OBJECT && instanceof_function(Z_OBJCE_P(obj_data), zend_standard_class_def TSRMLS_CC))) {
156153
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC,
157154
"Expected %s::%s() to return an array or stdClass, %s given",
158-
Z_OBJCE_P(object)->name,
155+
ZSTR_VAL(Z_OBJCE_P(object)->name),
159156
BSON_SERIALIZE_FUNC_NAME,
160-
(Z_TYPE_P(obj_data) == IS_OBJECT
161-
? Z_OBJCE_P(obj_data)->name
162-
: zend_get_type_by_const(Z_TYPE_P(obj_data))
163-
)
157+
PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(obj_data)
164158
);
165159
zval_ptr_dtor(&obj_data);
166160
#endif
@@ -453,22 +447,13 @@ void php_phongo_zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
453447
#endif
454448
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC,
455449
"Expected %s::%s() to return an array or stdClass, %s given",
456-
#if PHP_VERSION_ID >= 70000
457-
Z_OBJCE_P(data)->name->val,
458-
#else
459-
Z_OBJCE_P(data)->name,
460-
#endif
450+
ZSTR_VAL(Z_OBJCE_P(data)->name),
461451
BSON_SERIALIZE_FUNC_NAME,
462452
#if PHP_VERSION_ID >= 70000
463-
(Z_TYPE(obj_data) == IS_OBJECT
464-
? Z_OBJCE(obj_data)->name->val
465-
: zend_get_type_by_const(Z_TYPE(obj_data))
453+
PHONGO_ZVAL_CLASS_OR_TYPE_NAME(obj_data)
466454
#else
467-
(Z_TYPE_P(obj_data) == IS_OBJECT
468-
? Z_OBJCE_P(obj_data)->name
469-
: zend_get_type_by_const(Z_TYPE_P(obj_data))
455+
PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(obj_data)
470456
#endif
471-
)
472457
);
473458

474459
goto cleanup;

tests/bson/bson-timestamp_error-006.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Expected increment to be an unsigned 32-bit integer or string, boolean given
3232
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3333
Expected increment to be an unsigned 32-bit integer or string, array given
3434
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
35-
Expected increment to be an unsigned 32-bit integer or string, object given
35+
Expected increment to be an unsigned 32-bit integer or string, stdClass given
3636
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3737
Expected timestamp to be an unsigned 32-bit integer or string, %r(null|NULL)%r given
3838
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
@@ -42,5 +42,5 @@ Expected timestamp to be an unsigned 32-bit integer or string, boolean given
4242
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
4343
Expected timestamp to be an unsigned 32-bit integer or string, array given
4444
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
45-
Expected timestamp to be an unsigned 32-bit integer or string, object given
45+
Expected timestamp to be an unsigned 32-bit integer or string, stdClass given
4646
===DONE===

tests/manager/manager-executeCommand_error-002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ echo throws(function() use ($manager, $command) {
3232
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3333
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', string given
3434
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
35-
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', object given
35+
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', stdClass given
3636
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3737
Unknown option 'unknown'
3838
OK: Got MongoDB\Driver\Exception\InvalidArgumentException

tests/manager/manager-executeQuery_error-002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ echo throws(function() use ($manager, $query) {
3232
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3333
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', string given
3434
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
35-
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', object given
35+
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', stdClass given
3636
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3737
Unknown option 'unknown'
3838
OK: Got MongoDB\Driver\Exception\InvalidArgumentException

tests/query/query-ctor_error-001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Expected "readConcern" option to be MongoDB\Driver\ReadConcern, boolean given
3737
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3838
Expected "readConcern" option to be MongoDB\Driver\ReadConcern, array given
3939
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
40-
Expected "readConcern" option to be MongoDB\Driver\ReadConcern, object given
40+
Expected "readConcern" option to be MongoDB\Driver\ReadConcern, stdClass given
4141
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
4242
Expected "readConcern" option to be MongoDB\Driver\ReadConcern, %r(null|NULL)%r given
4343
===DONE===

tests/server/server-executeCommand_error-001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ echo throws(function() use ($manager, $command) {
3434
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3535
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', string given
3636
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
37-
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', object given
37+
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', stdClass given
3838
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3939
Unknown option 'unknown'
4040
OK: Got MongoDB\Driver\Exception\InvalidArgumentException

tests/server/server-executeQuery_error-001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ echo throws(function() use ($manager, $query) {
3434
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3535
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', string given
3636
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
37-
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', object given
37+
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', stdClass given
3838
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3939
Unknown option 'unknown'
4040
OK: Got MongoDB\Driver\Exception\InvalidArgumentException

tests/writeConcern/writeconcern-ctor_error-002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Expected w to be integer or string, boolean given
3131
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3232
Expected w to be integer or string, array given
3333
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
34-
Expected w to be integer or string, object given
34+
Expected w to be integer or string, stdClass given
3535
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3636
Expected w to be integer or string, %r(null|NULL)%r given
3737
===DONE===

0 commit comments

Comments
 (0)