work on payments
This commit is contained in:
@@ -55,33 +55,55 @@ function module_exists(string $moduleName): bool
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an anonymous class that acts as a dynamic object.
|
||||
* Create an anonymous dynamic object with both properties and callable methods.
|
||||
*
|
||||
* @param mixed ...$arguments Key-value pairs passed as an associative array.
|
||||
* @return object Anonymous class instance with dynamic properties.
|
||||
* You can define any mix of:
|
||||
* - scalar or object properties
|
||||
* - closures (which become methods bound to $this)
|
||||
*
|
||||
* Example:
|
||||
* ```php
|
||||
* $user = emptyClass(
|
||||
* name: 'Ali',
|
||||
* greet: fn(): string => "Hello, {$this->name}",
|
||||
* setName: fn(string $n): void => $this->name = $n,
|
||||
* );
|
||||
*
|
||||
* echo $user->greet(); // Hello, Ali
|
||||
* $user->setName('Ahmet');
|
||||
* echo $user->greet(); // Hello, Ahmet
|
||||
* ```
|
||||
*
|
||||
* @template T of object
|
||||
*
|
||||
* @param mixed ...$arguments Key-value pairs (property name => value or Closure)
|
||||
* @return T Anonymous class instance exposing dynamic properties/methods.
|
||||
*/
|
||||
function emptyClass(...$arguments): object
|
||||
function emptyClass(mixed ...$arguments): object
|
||||
{
|
||||
/**
|
||||
* @var array<string, mixed> $arguments
|
||||
*/
|
||||
/** @var T */
|
||||
return new class($arguments)
|
||||
{
|
||||
/**
|
||||
* Internal data storage.
|
||||
* Internal data store.
|
||||
*
|
||||
* @var array<string, mixed>
|
||||
*/
|
||||
private array $data = [];
|
||||
|
||||
/**
|
||||
* Constructor that sets properties.
|
||||
* Constructor that sets up dynamic properties and binds closures.
|
||||
*
|
||||
* @param array<string, mixed> $props
|
||||
*/
|
||||
public function __construct(array $props)
|
||||
{
|
||||
foreach ($props as $key => $value) {
|
||||
if ($value instanceof Closure) {
|
||||
/** @var Closure $value */
|
||||
$value = $value->bindTo($this, self::class);
|
||||
}
|
||||
|
||||
$this->data[$key] = $value;
|
||||
}
|
||||
}
|
||||
@@ -111,5 +133,27 @@ function emptyClass(...$arguments): object
|
||||
{
|
||||
return isset($this->data[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic method caller — invokes stored closures as methods.
|
||||
*
|
||||
* @param array<int, mixed> $args
|
||||
*
|
||||
* @throws BadMethodCallException if the method does not exist or is not callable.
|
||||
*/
|
||||
public function __call(string $method, array $args): mixed
|
||||
{
|
||||
$callable = $this->data[$method] ?? null;
|
||||
|
||||
if ($callable instanceof Closure) {
|
||||
return $callable(...$args);
|
||||
}
|
||||
|
||||
throw new BadMethodCallException(sprintf(
|
||||
'Method %s() does not exist on %s.',
|
||||
$method,
|
||||
self::class
|
||||
));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user