Enum ext_php_rs::props::Property
source · pub enum Property<'a, T> {
Field(Box<dyn Fn(&mut T) -> &mut dyn Prop<'_> + Send + Sync>),
Method {
get: PropertyGetter<'a, T>,
set: PropertySetter<'a, T>,
},
}
Expand description
Represents a property added to a PHP class.
There are two types of properties:
- Field properties, where the data is stored inside a struct field.
- Method properties, where getter and/or setter functions are provided, which are used to get and set the value of the property.
Variants§
Implementations§
source§impl<'a, T: 'a> Property<'a, T>
impl<'a, T: 'a> Property<'a, T>
sourcepub fn field<F>(f: F) -> Selfwhere
F: Fn(&mut T) -> &mut dyn Prop<'_> + Send + Sync + 'static,
pub fn field<F>(f: F) -> Selfwhere
F: Fn(&mut T) -> &mut dyn Prop<'_> + Send + Sync + 'static,
Creates a field property.
Parameters
f
- The function used to get a mutable reference to the property.
Examples
struct Test {
pub a: i32,
}
let prop: Property<Test> = Property::field(|test: &mut Test| &mut test.a);
sourcepub fn method<V>(
get: Option<fn(_: &T) -> V>,
set: Option<fn(_: &mut T, _: V)>
) -> Selfwhere
for<'b> V: IntoZval + FromZval<'b> + 'a,
pub fn method<V>(
get: Option<fn(_: &T) -> V>,
set: Option<fn(_: &mut T, _: V)>
) -> Selfwhere
for<'b> V: IntoZval + FromZval<'b> + 'a,
Creates a method property with getters and setters.
If either the getter or setter is not given, an exception will be thrown when attempting to retrieve/set the property.
Parameters
get
- Function used to get the value of the property, in anOption
.set
- Function used to set the value of the property, in anOption
.
Examples
struct Test;
impl Test {
pub fn get_prop(&self) -> String {
"Hello".into()
}
pub fn set_prop(&mut self, val: String) {
println!("{}", val);
}
}
let prop: Property<Test> = Property::method(Some(Test::get_prop), Some(Test::set_prop));
sourcepub fn get(&self, self_: &'a mut T, retval: &mut Zval) -> PhpResult
pub fn get(&self, self_: &'a mut T, retval: &mut Zval) -> PhpResult
Attempts to retrieve the value of the property from the given object
self_
.
The value of the property, if successfully retrieved, is loaded into the
given Zval
retval
. If unsuccessful, a PhpException
is
returned inside the error variant of a result.
Parameters
self_
- The object to retrieve the property from.retval
- TheZval
to load the value of the property into.
Returns
Nothing upon success, a PhpException
inside an error variant when
the property could not be retrieved.
Examples
struct Test {
pub a: i32,
}
let prop: Property<Test> = Property::field(|obj: &mut Test| &mut obj.a);
let mut test = Test { a: 500 };
let mut zv = Zval::new();
prop.get(&mut test, &mut zv).unwrap();
assert_eq!(zv.long(), Some(500));
Examples found in repository?
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
unsafe fn internal<T: RegisteredClass>(
object: *mut ZendObject,
member: *mut ZendStr,
type_: c_int,
cache_slot: *mut *mut c_void,
rv: *mut Zval,
) -> PhpResult<*mut Zval> {
let obj = object
.as_mut()
.and_then(|obj| ZendClassObject::<T>::from_zend_obj_mut(obj))
.ok_or("Invalid object pointer given")?;
let prop_name = member
.as_ref()
.ok_or("Invalid property name pointer given")?;
let self_ = &mut **obj;
let props = T::get_metadata().get_properties();
let prop = props.get(prop_name.as_str()?);
// retval needs to be treated as initialized, so we set the type to null
let rv_mut = rv.as_mut().ok_or("Invalid return zval given")?;
rv_mut.u1.type_info = ZvalTypeFlags::Null.bits();
Ok(match prop {
Some(prop) => {
prop.get(self_, rv_mut)?;
rv
}
None => zend_std_read_property(object, member, type_, cache_slot, rv),
})
}
match internal::<T>(object, member, type_, cache_slot, rv) {
Ok(rv) => rv,
Err(e) => {
let _ = e.throw();
(*rv).set_null();
rv
}
}
}
unsafe extern "C" fn write_property<T: RegisteredClass>(
object: *mut ZendObject,
member: *mut ZendStr,
value: *mut Zval,
cache_slot: *mut *mut c_void,
) -> *mut Zval {
#[inline(always)]
unsafe fn internal<T: RegisteredClass>(
object: *mut ZendObject,
member: *mut ZendStr,
value: *mut Zval,
cache_slot: *mut *mut c_void,
) -> PhpResult<*mut Zval> {
let obj = object
.as_mut()
.and_then(|obj| ZendClassObject::<T>::from_zend_obj_mut(obj))
.ok_or("Invalid object pointer given")?;
let prop_name = member
.as_ref()
.ok_or("Invalid property name pointer given")?;
let self_ = &mut **obj;
let props = T::get_metadata().get_properties();
let prop = props.get(prop_name.as_str()?);
let value_mut = value.as_mut().ok_or("Invalid return zval given")?;
Ok(match prop {
Some(prop) => {
prop.set(self_, value_mut)?;
value
}
None => zend_std_write_property(object, member, value, cache_slot),
})
}
match internal::<T>(object, member, value, cache_slot) {
Ok(rv) => rv,
Err(e) => {
let _ = e.throw();
value
}
}
}
unsafe extern "C" fn get_properties<T: RegisteredClass>(
object: *mut ZendObject,
) -> *mut ZendHashTable {
#[inline(always)]
unsafe fn internal<T: RegisteredClass>(
object: *mut ZendObject,
props: &mut ZendHashTable,
) -> PhpResult {
let obj = object
.as_mut()
.and_then(|obj| ZendClassObject::<T>::from_zend_obj_mut(obj))
.ok_or("Invalid object pointer given")?;
let self_ = &mut **obj;
let struct_props = T::get_metadata().get_properties();
for (name, val) in struct_props {
let mut zv = Zval::new();
if val.get(self_, &mut zv).is_err() {
continue;
}
props.insert(name, zv).map_err(|e| {
format!("Failed to insert value into properties hashtable: {:?}", e)
})?;
}
Ok(())
}
let props = zend_std_get_properties(object)
.as_mut()
.or_else(|| Some(ZendHashTable::new().into_raw()))
.expect("Failed to get property hashtable");
if let Err(e) = internal::<T>(object, props) {
let _ = e.throw();
}
props
}
unsafe extern "C" fn has_property<T: RegisteredClass>(
object: *mut ZendObject,
member: *mut ZendStr,
has_set_exists: c_int,
cache_slot: *mut *mut c_void,
) -> c_int {
#[inline(always)]
unsafe fn internal<T: RegisteredClass>(
object: *mut ZendObject,
member: *mut ZendStr,
has_set_exists: c_int,
cache_slot: *mut *mut c_void,
) -> PhpResult<c_int> {
let obj = object
.as_mut()
.and_then(|obj| ZendClassObject::<T>::from_zend_obj_mut(obj))
.ok_or("Invalid object pointer given")?;
let prop_name = member
.as_ref()
.ok_or("Invalid property name pointer given")?;
let props = T::get_metadata().get_properties();
let prop = props.get(prop_name.as_str()?);
let self_ = &mut **obj;
match has_set_exists {
//
// * 0 (has) whether property exists and is not NULL
0 => {
if let Some(val) = prop {
let mut zv = Zval::new();
val.get(self_, &mut zv)?;
if !zv.is_null() {
return Ok(1);
}
}
}
//
// * 1 (set) whether property exists and is true
1 => {
if let Some(val) = prop {
let mut zv = Zval::new();
val.get(self_, &mut zv)?;
if zend_is_true(&mut zv) == 1 {
return Ok(1);
}
}
}
//
// * 2 (exists) whether property exists
2 => {
if prop.is_some() {
return Ok(1);
}
}
_ => return Err(
"Invalid value given for `has_set_exists` in struct `has_property` function."
.into(),
),
};
Ok(zend_std_has_property(
object,
member,
has_set_exists,
cache_slot,
))
}
sourcepub fn set(&self, self_: &'a mut T, value: &Zval) -> PhpResult
pub fn set(&self, self_: &'a mut T, value: &Zval) -> PhpResult
Attempts to set the value of the property inside the given object
self_
.
The new value of the property is supplied inside the given Zval
value
. If unsuccessful, a PhpException
is returned inside the
error variant of a result.
Parameters
self_
- The object to set the property in.value
- TheZval
containing the new content for the property.
Returns
Nothing upon success, a PhpException
inside an error variant when
the property could not be set.
Examples
struct Test {
pub a: i32,
}
let prop: Property<Test> = Property::field(|obj: &mut Test| &mut obj.a);
let mut test = Test { a: 500 };
let zv = 100.into_zval(false).unwrap();
prop.set(&mut test, &zv).unwrap();
assert_eq!(test.a, 100);
Examples found in repository?
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
unsafe fn internal<T: RegisteredClass>(
object: *mut ZendObject,
member: *mut ZendStr,
value: *mut Zval,
cache_slot: *mut *mut c_void,
) -> PhpResult<*mut Zval> {
let obj = object
.as_mut()
.and_then(|obj| ZendClassObject::<T>::from_zend_obj_mut(obj))
.ok_or("Invalid object pointer given")?;
let prop_name = member
.as_ref()
.ok_or("Invalid property name pointer given")?;
let self_ = &mut **obj;
let props = T::get_metadata().get_properties();
let prop = props.get(prop_name.as_str()?);
let value_mut = value.as_mut().ok_or("Invalid return zval given")?;
Ok(match prop {
Some(prop) => {
prop.set(self_, value_mut)?;
value
}
None => zend_std_write_property(object, member, value, cache_slot),
})
}