pub struct Arg<'a> { /* private fields */ }Expand description
Represents an argument to a function.
Implementations§
Source§impl<'a> Arg<'a>
impl<'a> Arg<'a>
Sourcepub fn new<T: Into<String>>(name: T, type: DataType) -> Self
pub fn new<T: Into<String>>(name: T, type: DataType) -> Self
Creates a new argument.
§Parameters
name- The name of the parameter._type- The type of the parameter.
Sourcepub fn of<T: FromZvalMut<'a>>(name: impl Into<String>) -> Self
pub fn of<T: FromZvalMut<'a>>(name: impl Into<String>) -> Self
Creates an argument whose type, nullability and pass-by-reference flag come from the Rust type it is extracted into.
§Parameters
name- The name of the parameter.
Sourcepub fn untyped<T: Into<String>>(name: T) -> Self
pub fn untyped<T: Into<String>>(name: T) -> Self
Creates an argument declared without a type, as PHP declares
function f($value): reflection reports no type, and a method that
overrides it may declare its parameter with any type or none.
The Rust side still converts the value as its own type, usually
&Zval.
Sourcepub fn or(self, type: DataType) -> Self
pub fn or(self, type: DataType) -> Self
Adds r#type to the argument’s type, making it a union:
Arg::new("value", DataType::Long).or(DataType::Double) declares
int|float $value.
The argument’s type and every member are bool, int, float,
string, array or object, and a member can also be null. Any
other type (a class, iterable, resource, mixed, …), or a union
on an Arg::untyped argument, fails
FunctionBuilder::build with
Error::InvalidUnionType.
The type is what reflection and PHP’s inheritance checks see: release builds of PHP do not check an internal function’s arguments against it, so the handler receives what the caller passed and must check it.
Sourcepub fn is_variadic(self) -> Self
pub fn is_variadic(self) -> Self
Sets the argument as variadic.
Sourcepub fn sensitive(self) -> Self
pub fn sensitive(self) -> Self
Marks the argument #[\SensitiveParameter]: exception traces show a
SensitiveParameterValue in its place instead of the value. It has no
effect before PHP 8.2, which introduced the attribute.
Sourcepub fn allow_null(self) -> Self
pub fn allow_null(self) -> Self
Sets the argument as nullable.
Sourcepub fn default<T: Into<String>>(self, default: T) -> Self
pub fn default<T: Into<String>>(self, default: T) -> Self
Sets the default value for the argument.
Sourcepub fn consume<T>(self) -> Result<T, Self>where
for<'b> T: FromZvalMut<'b>,
pub fn consume<T>(self) -> Result<T, Self>where
for<'b> T: FromZvalMut<'b>,
Attempts to consume the argument, converting the inner type into T.
Upon success, the result is returned in a Result.
As this function consumes, it cannot return a reference to the underlying zval.
§Errors
If the conversion fails (or the argument contains no value), the
argument is returned in an Err variant.
Sourcepub fn val<T>(&'a mut self) -> Option<T>where
T: FromZvalMut<'a>,
pub fn val<T>(&'a mut self) -> Option<T>where
T: FromZvalMut<'a>,
Attempts to retrieve the value of the argument.
This will be None until the ArgParser is used to parse
the arguments.
Sourcepub fn variadic_vals<T>(&'a mut self) -> Vec<T>where
T: FromZvalMut<'a>,
pub fn variadic_vals<T>(&'a mut self) -> Vec<T>where
T: FromZvalMut<'a>,
Gets all the variadic values for this Rust argument. The result does not
contain a value that cannot be converted into T.
try_variadic_vals rejects it.
Sourcepub fn try_variadic_vals<T>(&'a mut self) -> Result<Vec<T>>where
T: FromZvalMut<'a>,
pub fn try_variadic_vals<T>(&'a mut self) -> Result<Vec<T>>where
T: FromZvalMut<'a>,
Gets all the variadic values for this Rust argument.
§Errors
Returns the error of the first value that cannot be converted into T,
see FromZvalMut::try_from_zval_mut.
Sourcepub fn zval(&mut self) -> Option<&mut &'a mut Zval>
pub fn zval(&mut self) -> Option<&mut &'a mut Zval>
Attempts to return a reference to the arguments internal Zval.
§Returns
Some(&Zval)- The internal zval.None- The argument was empty.
Sourcepub fn try_call(&self, params: Vec<&dyn IntoZvalDyn>) -> Result<Zval>
pub fn try_call(&self, params: Vec<&dyn IntoZvalDyn>) -> Result<Zval>
Attempts to call the argument as a callable with a list of arguments to pass to the function. Note that a thrown exception inside the callable is not detectable, therefore you should check if the return value is valid rather than unwrapping. Returns a result containing the return value of the function, or an error.
You should not call this function directly, rather through the
call_user_func macro.
§Parameters
params- A list of parameters to call the function with.
§Errors
Error::Callable- The argument is not callable.