| Server IP : 104.26.5.103 / Your IP : 216.73.216.4 Web Server : nginx/1.27.1 System : Linux in-5 5.15.0-143-generic #153-Ubuntu SMP Fri Jun 13 19:10:45 UTC 2025 x86_64 User : arabianexpress ( 1872) PHP Version : 8.0.30 Disable Function : exec,passthru,shell_exec,system,proc_open,popen,parse_ini_file,show_source MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /storage/v9321/leapdubai/public_html/wp-content/plugins/kirki/libraries/framework/ |
Upload File : |
<?php
/**
* Abstract API resource transformer that maps models or arrays to public response shapes.
* Offers static make, collection, and paginated helpers for batch serialization.
* Delegates property access to the underlying resource via magic methods.
*
* @package Framework
* @since 1.0.0
*/
namespace Kirki\Framework;
\defined('ABSPATH') || exit;
use Kirki\Framework\Contracts\Support\Arrayable;
use Kirki\Framework\Contracts\Support\Jsonable;
use Kirki\Framework\Collections\Collection;
use Kirki\Framework\Database\Query\Paginator;
use Kirki\Framework\Supports\Arr;
abstract class Resource implements Arrayable, Jsonable
{
/**
* The resource instance.
*
* @var object|array
*
* @since 1.0.0
*/
protected $resource;
/**
* Create a new resource instance.
*
* @param object|array $resource The resource to create a new instance of.
*
* @return void
*
* @since 1.0.0
*/
public function __construct($resource)
{
if (\is_array($resource)) {
$this->resource = (object) $resource;
} else {
$this->resource = $resource;
}
}
/**
* Convert the resource to an array.
*
* @return array
*
* @since 1.0.0
*/
public abstract function to_array();
/**
* Create a new resource instance, or return null if the resource is null.
*
* @param mixed $resource The resource to create a new instance of.
*
* @return array|null
*
* @since 1.0.0
*/
public static function make($resource)
{
if ($resource === null) {
return null;
}
return (new static($resource))->to_array();
}
/**
* Converts an iterable of resources into an array of resource representations.
*
* This method loops over the iterable and creates a new instance of the resource
* class for each item, then calls the to_array method on the resource to
* obtain its representation as an array.
*
* @param iterable $resources The iterable of resources to convert.
*
* @return array The array of resource representations.
*
* @since 1.0.0
*/
public static function collection($resources)
{
$data = [];
if (empty($resources)) {
return $data;
}
if ($resources instanceof Collection) {
$resources = $resources->all();
}
foreach ($resources as $resource) {
$data[] = (new static($resource))->to_array();
}
return $data;
}
/**
* Converts a paginator object into an array of resource representations,
* including pagination metadata.
*
* This method loops over the paginator's results and creates a new instance
* of the resource class for each item, then calls the to_array method on
* the resource to obtain its representation as an array.
*
* @param Paginator $paginator The paginator object to convert.
*
* @return array The array of resource representations, including pagination metadata.
*
* @since 1.0.0
*/
public static function paginated(Paginator $paginator)
{
$paginated_data = $paginator->to_array();
foreach ($paginated_data['results'] as $key => $resource) {
$paginated_data['results'][$key] = (new static($resource))->to_array();
}
return $paginated_data;
}
/**
* Convert the resource to a JSON string.
*
* Encodes the array form of the resource for straightforward transport or
* logging purposes.
*
* @param mixed $options The options array.
*
* @return string The JSON-encoded paginator representation
*
* @since 1.0.0
*/
public function to_json($options = 0)
{
return Arr::json_encode($this->to_array(), $options);
}
/**
* Check if a property exists on the underlying resource.
*
* This magic method allows you to check if a property exists on the underlying resource
* as if it were a property of the current class. This provides a convenient way of
* checking for the existence of resource properties without having to explicitly call a method.
*
* @param string $name The name of the property to check.
*
* @return bool True if the property exists, false otherwise.
*
* @since 1.0.0
*/
public function __isset($name)
{
return isset($this->resource->{$name});
}
/**
* Dynamically pass properties of the underlying resource to the caller.
*
* This magic method allows you to access properties of the underlying resource
* as if they were properties of the current class. This provides a convenient
* way of accessing resource properties without having to explicitly call a method.
*
* @param string $name The name of the property to access.
*
* @return mixed The value of the accessed property.
*
* @since 1.0.0
*/
public function __get($name)
{
return $this->resource->{$name} ?? null;
}
/**
* Dynamically pass properties of the underlying resource to the caller.
*
* This magic method allows you to access properties of the underlying resource
* as if they were properties of the current class. This provides a convenient
* way of accessing resource properties without having to explicitly call a method.
*
* @param string $name The name of the property to access.
* @param mixed $value The value to set.
*
* @return $this The current instance.
*
* @since 1.0.0
*/
public function __set($name, $value)
{
$this->resource->{$name} = $value;
return $this;
}
/**
* Dynamically pass method calls of the underlying resource to the caller.
*
* This magic method allows you to call methods of the underlying resource
* as if they were methods of the current class. This provides a convenient
* way of accessing resource methods without having to explicitly call a method.
*
* @param string $method The name of the method to access.
* @param array $args The arguments to pass to the method.
*
* @return mixed The return value of the accessed method.
*
* @since 1.0.0
*/
public function __call($method, $args)
{
return $this->resource->{$method}(...$args);
}
}