Skip to content

Commit 407af29

Browse files
committed
feat(class): allow implementing internal PHP interfaces
1 parent a5bf383 commit 407af29

4 files changed

Lines changed: 27 additions & 5 deletions

File tree

allowed_bindings.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ bind! {
8383
zend_ce_serializable,
8484
zend_ce_countable,
8585
zend_ce_stringable,
86+
php_json_serializable_ce,
8687
zend_class_entry,
8788
zend_declare_class_constant,
8889
zend_declare_property,

docsrs_bindings.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3853,3 +3853,6 @@ pub type zend_observer_error_cb = ::std::option::Option<
38533853
unsafe extern "C" {
38543854
pub fn zend_observer_error_register(callback: zend_observer_error_cb);
38553855
}
3856+
unsafe extern "C" {
3857+
pub static mut php_json_serializable_ce: *mut zend_class_entry;
3858+
}

src/wrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "zend_ini.h"
4141
#include "zend_observer.h"
4242
#include "main/SAPI.h"
43+
#include "ext/json/php_json.h"
4344

4445
zend_string *ext_php_rs_zend_string_init(const char *str, size_t len, bool persistent);
4546
void ext_php_rs_zend_string_release(zend_string *zs);

src/zend/ce.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
#![allow(clippy::unwrap_used)]
44

55
use crate::ffi::{
6-
zend_ce_aggregate, zend_ce_argument_count_error, zend_ce_arithmetic_error, zend_ce_arrayaccess,
7-
zend_ce_compile_error, zend_ce_countable, zend_ce_division_by_zero_error,
8-
zend_ce_error_exception, zend_ce_exception, zend_ce_iterator, zend_ce_parse_error,
9-
zend_ce_serializable, zend_ce_stringable, zend_ce_throwable, zend_ce_traversable,
10-
zend_ce_type_error, zend_ce_unhandled_match_error, zend_ce_value_error,
6+
php_json_serializable_ce, zend_ce_aggregate, zend_ce_argument_count_error,
7+
zend_ce_arithmetic_error, zend_ce_arrayaccess, zend_ce_compile_error, zend_ce_countable,
8+
zend_ce_division_by_zero_error, zend_ce_error_exception, zend_ce_exception, zend_ce_iterator,
9+
zend_ce_parse_error, zend_ce_serializable, zend_ce_stringable, zend_ce_throwable,
10+
zend_ce_traversable, zend_ce_type_error, zend_ce_unhandled_match_error, zend_ce_value_error,
1111
zend_standard_class_def,
1212
};
1313

@@ -184,6 +184,15 @@ pub fn stringable() -> &'static ClassEntry {
184184
unsafe { zend_ce_stringable.as_ref() }.unwrap()
185185
}
186186

187+
/// Returns the [`JsonSerializable`](https://www.php.net/manual/en/class.jsonserializable.php) interface.
188+
///
189+
/// # Panics
190+
///
191+
/// If jsonserializable [`ClassEntry`] is not available
192+
pub fn jsonserializable() -> &'static ClassEntry {
193+
unsafe { php_json_serializable_ce.as_ref() }.unwrap()
194+
}
195+
187196
#[cfg(test)]
188197
#[cfg(feature = "embed")]
189198
mod tests {
@@ -341,4 +350,12 @@ mod tests {
341350
assert_eq!(stringable.name(), Some("Stringable"));
342351
});
343352
}
353+
354+
#[test]
355+
fn test_jsonserializable() {
356+
Embed::run(|| {
357+
let jsonserializable = jsonserializable();
358+
assert_eq!(jsonserializable.name(), Some("JsonSerializable"));
359+
});
360+
}
344361
}

0 commit comments

Comments
 (0)