use crate::{
args::Arg,
class::PhpClass,
convert::FromZvalMut,
error::{Error, Result},
exception::PhpException,
flags::DataType,
types::{ClassRef, ClassRefMut, ZendClassObject, Zval},
zend::ce,
};
mod sealed {
pub trait Sealed {}
impl Sealed for () {}
impl<T> Sealed for Option<T> {}
}
pub trait ArgumentHolder: Sized + sealed::Sealed {
const INIT: Self;
}
impl ArgumentHolder for () {
const INIT: Self = ();
}
impl<T> ArgumentHolder for Option<T> {
const INIT: Self = None;
}
#[diagnostic::on_unimplemented(
message = "`{Self}` cannot be used as a PHP function argument",
label = "no `FromZval` or `FromZvalMut` implementation for `{Self}`",
note = "a `#[php_class]` parameter is `&T`, `&mut T`, `Option<&T>`, `Option<&mut T>`, `ClassRef<T>` or `ClassRefMut<T>`",
note = "a variadic parameter must be spelled `&[T]` in the signature, a type alias hiding the slice is not detected"
)]
pub trait FunctionArgument<'a, 'h, const FROM_ZVAL: bool>: Sized {
type Holder: ArgumentHolder;
const TYPE: DataType;
const NULLABLE: bool;
const BY_REF: bool;
fn extract(zval: &'a mut Zval, holder: &'h mut Self::Holder) -> Result<Self>;
fn missing() -> Option<Self>;
}
impl<'a, 'h, T: FromZvalMut<'a>> FunctionArgument<'a, 'h, true> for T {
type Holder = ();
const TYPE: DataType = T::TYPE;
const NULLABLE: bool = T::NULLABLE;
const BY_REF: bool = T::BY_REF;
#[inline]
fn extract(zval: &'a mut Zval, &mut (): &'h mut ()) -> Result<Self> {
T::try_from_zval_mut(zval)
}
#[inline]
fn missing() -> Option<Self> {
T::from_missing()
}
}
impl<'a: 'h, 'h, T: PhpClass> FunctionArgument<'a, 'h, false> for &'h T {
type Holder = Option<ClassRef<'a, T>>;
const TYPE: DataType = DataType::object(T::CLASS_NAME);
const NULLABLE: bool = false;
const BY_REF: bool = false;
#[inline]
fn extract(zval: &'a mut Zval, holder: &'h mut Self::Holder) -> Result<Self> {
let guard = ZendClassObject::<T>::from_zval_uninit(zval)?.try_borrow()?;
Ok(&**holder.insert(guard))
}
#[inline]
fn missing() -> Option<Self> {
None
}
}
impl<'a: 'h, 'h, T: PhpClass> FunctionArgument<'a, 'h, false> for &'h mut T {
type Holder = Option<ClassRefMut<'a, T>>;
const TYPE: DataType = DataType::object(T::CLASS_NAME);
const NULLABLE: bool = false;
const BY_REF: bool = false;
#[inline]
fn extract(zval: &'a mut Zval, holder: &'h mut Self::Holder) -> Result<Self> {
let guard = ZendClassObject::<T>::from_zval_uninit(zval)?.try_borrow_mut()?;
Ok(&mut **holder.insert(guard))
}
#[inline]
fn missing() -> Option<Self> {
None
}
}
impl<'a: 'h, 'h, T: PhpClass> FunctionArgument<'a, 'h, false> for Option<&'h T> {
type Holder = Option<ClassRef<'a, T>>;
const TYPE: DataType = DataType::object(T::CLASS_NAME);
const NULLABLE: bool = true;
const BY_REF: bool = false;
#[inline]
fn extract(zval: &'a mut Zval, holder: &'h mut Self::Holder) -> Result<Self> {
if zval.is_null() {
return Ok(None);
}
<&T as FunctionArgument<'a, 'h, false>>::extract(zval, holder).map(Some)
}
#[inline]
fn missing() -> Option<Self> {
Some(None)
}
}
impl<'a: 'h, 'h, T: PhpClass> FunctionArgument<'a, 'h, false> for Option<&'h mut T> {
type Holder = Option<ClassRefMut<'a, T>>;
const TYPE: DataType = DataType::object(T::CLASS_NAME);
const NULLABLE: bool = true;
const BY_REF: bool = false;
#[inline]
fn extract(zval: &'a mut Zval, holder: &'h mut Self::Holder) -> Result<Self> {
if zval.is_null() {
return Ok(None);
}
<&mut T as FunctionArgument<'a, 'h, false>>::extract(zval, holder).map(Some)
}
#[inline]
fn missing() -> Option<Self> {
Some(None)
}
}
#[inline]
#[must_use]
pub fn holder<'a, 'h, T, const FROM_ZVAL: bool>() -> T::Holder
where
T: FunctionArgument<'a, 'h, FROM_ZVAL>,
{
<T::Holder as ArgumentHolder>::INIT
}
#[inline]
pub fn extract<'a, 'h, T, const FROM_ZVAL: bool>(
zval: &'a mut Zval,
holder: &'h mut T::Holder,
) -> Result<T>
where
T: FunctionArgument<'a, 'h, FROM_ZVAL>,
{
T::extract(zval, holder)
}
#[inline]
#[must_use]
pub fn missing<'a, 'h, T, const FROM_ZVAL: bool>() -> Option<T>
where
T: FunctionArgument<'a, 'h, FROM_ZVAL>,
{
T::missing()
}
#[must_use]
pub const fn nullable<'a, 'h, T, const FROM_ZVAL: bool>() -> bool
where
T: FunctionArgument<'a, 'h, FROM_ZVAL>,
{
T::NULLABLE
}
#[must_use]
pub const fn data_type<'a, 'h, T, const FROM_ZVAL: bool>() -> DataType
where
T: FunctionArgument<'a, 'h, FROM_ZVAL>,
{
T::TYPE
}
pub fn arg<'z, 'a, 'h, T, const FROM_ZVAL: bool>(name: impl Into<String>) -> Arg<'z>
where
T: FunctionArgument<'a, 'h, FROM_ZVAL>,
{
Arg::typed(name, T::TYPE, T::NULLABLE, T::BY_REF)
}
#[must_use]
pub fn argument_error(name: &str, error: &Error) -> PhpException {
match error {
Error::Borrow(borrow) => (*borrow).into(),
_ => PhpException::new(
format!("Invalid value given for argument `{name}`."),
0,
ce::type_error(),
),
}
}