Collections
3 weeks ago
Concerns
3 weeks ago
Console
3 weeks ago
Constants
3 weeks ago
Contracts
3 weeks ago
Database
3 weeks ago
Discovery
3 weeks ago
Exceptions
3 weeks ago
Filesystem
3 weeks ago
Http
3 weeks ago
Managers
3 weeks ago
Middlewares
3 weeks ago
Polyfill
3 weeks ago
Supports
3 weeks ago
Validation
3 weeks ago
Wordpress
3 weeks ago
ApiExceptionHandler.php
3 weeks ago
Application.php
3 weeks ago
Container.php
3 weeks ago
CoreServiceProvider.php
3 weeks ago
DTO.php
3 weeks ago
Facade.php
3 weeks ago
Listener.php
3 weeks ago
Resource.php
3 weeks ago
Route.php
3 weeks ago
Sanitizer.php
3 weeks ago
ServiceProvider.php
3 weeks ago
helpers.php
3 weeks ago
Resource.php
207 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Abstract API resource transformer that maps models or arrays to public response shapes. |
| 5 | * Offers static make, collection, and paginated helpers for batch serialization. |
| 6 | * Delegates property access to the underlying resource via magic methods. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @since 1.0.0 |
| 10 | */ |
| 11 | namespace Kirki\Framework; |
| 12 | |
| 13 | \defined('ABSPATH') || exit; |
| 14 | use Kirki\Framework\Contracts\Support\Arrayable; |
| 15 | use Kirki\Framework\Contracts\Support\Jsonable; |
| 16 | use Kirki\Framework\Collections\Collection; |
| 17 | use Kirki\Framework\Database\Query\Paginator; |
| 18 | use Kirki\Framework\Supports\Arr; |
| 19 | abstract class Resource implements Arrayable, Jsonable |
| 20 | { |
| 21 | /** |
| 22 | * The resource instance. |
| 23 | * |
| 24 | * @var object|array |
| 25 | * |
| 26 | * @since 1.0.0 |
| 27 | */ |
| 28 | protected $resource; |
| 29 | /** |
| 30 | * Create a new resource instance. |
| 31 | * |
| 32 | * @param object|array $resource The resource to create a new instance of. |
| 33 | * |
| 34 | * @return void |
| 35 | * |
| 36 | * @since 1.0.0 |
| 37 | */ |
| 38 | public function __construct($resource) |
| 39 | { |
| 40 | if (\is_array($resource)) { |
| 41 | $this->resource = (object) $resource; |
| 42 | } else { |
| 43 | $this->resource = $resource; |
| 44 | } |
| 45 | } |
| 46 | /** |
| 47 | * Convert the resource to an array. |
| 48 | * |
| 49 | * @return array |
| 50 | * |
| 51 | * @since 1.0.0 |
| 52 | */ |
| 53 | public abstract function to_array(); |
| 54 | /** |
| 55 | * Create a new resource instance, or return null if the resource is null. |
| 56 | * |
| 57 | * @param mixed $resource The resource to create a new instance of. |
| 58 | * |
| 59 | * @return array|null |
| 60 | * |
| 61 | * @since 1.0.0 |
| 62 | */ |
| 63 | public static function make($resource) |
| 64 | { |
| 65 | if ($resource === null) { |
| 66 | return null; |
| 67 | } |
| 68 | return (new static($resource))->to_array(); |
| 69 | } |
| 70 | /** |
| 71 | * Converts an iterable of resources into an array of resource representations. |
| 72 | * |
| 73 | * This method loops over the iterable and creates a new instance of the resource |
| 74 | * class for each item, then calls the to_array method on the resource to |
| 75 | * obtain its representation as an array. |
| 76 | * |
| 77 | * @param iterable $resources The iterable of resources to convert. |
| 78 | * |
| 79 | * @return array The array of resource representations. |
| 80 | * |
| 81 | * @since 1.0.0 |
| 82 | */ |
| 83 | public static function collection($resources) |
| 84 | { |
| 85 | $data = []; |
| 86 | if (empty($resources)) { |
| 87 | return $data; |
| 88 | } |
| 89 | if ($resources instanceof Collection) { |
| 90 | $resources = $resources->all(); |
| 91 | } |
| 92 | foreach ($resources as $resource) { |
| 93 | $data[] = (new static($resource))->to_array(); |
| 94 | } |
| 95 | return $data; |
| 96 | } |
| 97 | /** |
| 98 | * Converts a paginator object into an array of resource representations, |
| 99 | * including pagination metadata. |
| 100 | * |
| 101 | * This method loops over the paginator's results and creates a new instance |
| 102 | * of the resource class for each item, then calls the to_array method on |
| 103 | * the resource to obtain its representation as an array. |
| 104 | * |
| 105 | * @param Paginator $paginator The paginator object to convert. |
| 106 | * |
| 107 | * @return array The array of resource representations, including pagination metadata. |
| 108 | * |
| 109 | * @since 1.0.0 |
| 110 | */ |
| 111 | public static function paginated(Paginator $paginator) |
| 112 | { |
| 113 | $paginated_data = $paginator->to_array(); |
| 114 | foreach ($paginated_data['results'] as $key => $resource) { |
| 115 | $paginated_data['results'][$key] = (new static($resource))->to_array(); |
| 116 | } |
| 117 | return $paginated_data; |
| 118 | } |
| 119 | /** |
| 120 | * Convert the resource to a JSON string. |
| 121 | * |
| 122 | * Encodes the array form of the resource for straightforward transport or |
| 123 | * logging purposes. |
| 124 | * |
| 125 | * @param mixed $options The options array. |
| 126 | * |
| 127 | * @return string The JSON-encoded paginator representation |
| 128 | * |
| 129 | * @since 1.0.0 |
| 130 | */ |
| 131 | public function to_json($options = 0) |
| 132 | { |
| 133 | return Arr::json_encode($this->to_array(), $options); |
| 134 | } |
| 135 | /** |
| 136 | * Check if a property exists on the underlying resource. |
| 137 | * |
| 138 | * This magic method allows you to check if a property exists on the underlying resource |
| 139 | * as if it were a property of the current class. This provides a convenient way of |
| 140 | * checking for the existence of resource properties without having to explicitly call a method. |
| 141 | * |
| 142 | * @param string $name The name of the property to check. |
| 143 | * |
| 144 | * @return bool True if the property exists, false otherwise. |
| 145 | * |
| 146 | * @since 1.0.0 |
| 147 | */ |
| 148 | public function __isset($name) |
| 149 | { |
| 150 | return isset($this->resource->{$name}); |
| 151 | } |
| 152 | /** |
| 153 | * Dynamically pass properties of the underlying resource to the caller. |
| 154 | * |
| 155 | * This magic method allows you to access properties of the underlying resource |
| 156 | * as if they were properties of the current class. This provides a convenient |
| 157 | * way of accessing resource properties without having to explicitly call a method. |
| 158 | * |
| 159 | * @param string $name The name of the property to access. |
| 160 | * |
| 161 | * @return mixed The value of the accessed property. |
| 162 | * |
| 163 | * @since 1.0.0 |
| 164 | */ |
| 165 | public function __get($name) |
| 166 | { |
| 167 | return $this->resource->{$name} ?? null; |
| 168 | } |
| 169 | /** |
| 170 | * Dynamically pass properties of the underlying resource to the caller. |
| 171 | * |
| 172 | * This magic method allows you to access properties of the underlying resource |
| 173 | * as if they were properties of the current class. This provides a convenient |
| 174 | * way of accessing resource properties without having to explicitly call a method. |
| 175 | * |
| 176 | * @param string $name The name of the property to access. |
| 177 | * @param mixed $value The value to set. |
| 178 | * |
| 179 | * @return $this The current instance. |
| 180 | * |
| 181 | * @since 1.0.0 |
| 182 | */ |
| 183 | public function __set($name, $value) |
| 184 | { |
| 185 | $this->resource->{$name} = $value; |
| 186 | return $this; |
| 187 | } |
| 188 | /** |
| 189 | * Dynamically pass method calls of the underlying resource to the caller. |
| 190 | * |
| 191 | * This magic method allows you to call methods of the underlying resource |
| 192 | * as if they were methods of the current class. This provides a convenient |
| 193 | * way of accessing resource methods without having to explicitly call a method. |
| 194 | * |
| 195 | * @param string $method The name of the method to access. |
| 196 | * @param array $args The arguments to pass to the method. |
| 197 | * |
| 198 | * @return mixed The return value of the accessed method. |
| 199 | * |
| 200 | * @since 1.0.0 |
| 201 | */ |
| 202 | public function __call($method, $args) |
| 203 | { |
| 204 | return $this->resource->{$method}(...$args); |
| 205 | } |
| 206 | } |
| 207 |