PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.1.0
Kirki – Freeform Page Builder, Website Builder & Customizer v6.1.0
6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / libraries / framework / Resource.php
kirki / libraries / framework Last commit date
Collections 3 weeks ago Concerns 1 week ago Console 3 weeks ago Constants 3 weeks ago Contracts 3 weeks ago Database 1 week ago Discovery 3 weeks ago Exceptions 3 weeks ago Filesystem 3 weeks ago Http 3 weeks ago Managers 1 week 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 1 week ago Resource.php 1 week ago Route.php 3 weeks ago Sanitizer.php 3 weeks ago ServiceProvider.php 3 weeks ago helpers.php 3 weeks ago
Resource.php
214 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 use JsonSerializable;
20 abstract class Resource implements Arrayable, Jsonable, JsonSerializable
21 {
22 /**
23 * The resource instance.
24 *
25 * @var object|array
26 *
27 * @since 1.0.0
28 */
29 protected $resource;
30 /**
31 * Create a new resource instance.
32 *
33 * @param object|array $resource The resource to create a new instance of.
34 *
35 * @return void
36 *
37 * @since 1.0.0
38 */
39 public function __construct($resource)
40 {
41 if (\is_array($resource)) {
42 $this->resource = (object) $resource;
43 } else {
44 $this->resource = $resource;
45 }
46 }
47 /**
48 * Convert the resource to an array.
49 *
50 * @return array
51 *
52 * @since 1.0.0
53 */
54 public abstract function to_array();
55 /**
56 * Create a new resource instance, or return null if the resource is null.
57 *
58 * @param mixed $parameters The resource to create a new instance of.
59 *
60 * @return array
61 *
62 * @since 1.0.0
63 */
64 public static function make(...$parameters)
65 {
66 return (new static(...$parameters))->to_array();
67 }
68 /**
69 * Converts an iterable of resources into an array of resource representations.
70 *
71 * This method loops over the iterable and creates a new instance of the resource
72 * class for each item, then calls the to_array method on the resource to
73 * obtain its representation as an array.
74 *
75 * @param mixed $parameters The parameters to pass to the resource constructor.
76 *
77 * @return array The array of resource representations.
78 *
79 * @since 1.0.0
80 */
81 public static function collection(...$parameters)
82 {
83 if (empty($parameters)) {
84 return [];
85 }
86 $resource = \array_shift($parameters);
87 if ($resource instanceof Collection) {
88 $resource = $resource->all();
89 }
90 return Arr::map($resource, fn($resource) => (new static($resource, ...$parameters))->to_array());
91 }
92 /**
93 * Converts a paginator object into an array of resource representations,
94 * including pagination metadata.
95 *
96 * This method loops over the paginator's results and creates a new instance
97 * of the resource class for each item, then calls the to_array method on
98 * the resource to obtain its representation as an array.
99 *
100 * @param Paginator $paginator The paginator object to convert.
101 * @param mixed $parameters The parameters to pass to the resource constructor.
102 *
103 * @return array The array of resource representations, including pagination metadata.
104 *
105 * @since 1.0.0
106 */
107 public static function paginated(Paginator $paginator, ...$parameters)
108 {
109 $paginated_data = $paginator->to_array();
110 foreach ($paginated_data['results'] as $key => $resource) {
111 $paginated_data['results'][$key] = (new static($resource, ...$parameters))->to_array();
112 }
113 return $paginated_data;
114 }
115 /**
116 * Convert the resource to a JSON string.
117 *
118 * Encodes the array form of the resource for straightforward transport or
119 * logging purposes.
120 *
121 * @param mixed $options The options array.
122 *
123 * @return string The JSON-encoded paginator representation
124 *
125 * @since 1.0.0
126 */
127 public function to_json($options = 0)
128 {
129 return Arr::json_encode($this->to_array(), $options);
130 }
131 /**
132 * Check if a property exists on the underlying resource.
133 *
134 * This magic method allows you to check if a property exists on the underlying resource
135 * as if it were a property of the current class. This provides a convenient way of
136 * checking for the existence of resource properties without having to explicitly call a method.
137 *
138 * @param string $name The name of the property to check.
139 *
140 * @return bool True if the property exists, false otherwise.
141 *
142 * @since 1.0.0
143 */
144 public function __isset($name)
145 {
146 return isset($this->resource->{$name});
147 }
148 /**
149 * Convert the resource to a JSON string.
150 *
151 * @return array The JSON-encoded resource representation
152 *
153 * @since 1.0.0
154 */
155 public function jsonSerialize() : array
156 {
157 return $this->to_array();
158 }
159 /**
160 * Dynamically pass properties of the underlying resource to the caller.
161 *
162 * This magic method allows you to access properties of the underlying resource
163 * as if they were properties of the current class. This provides a convenient
164 * way of accessing resource properties without having to explicitly call a method.
165 *
166 * @param string $name The name of the property to access.
167 *
168 * @return mixed The value of the accessed property.
169 *
170 * @since 1.0.0
171 */
172 public function __get($name)
173 {
174 return $this->resource->{$name} ?? null;
175 }
176 /**
177 * Dynamically pass properties of the underlying resource to the caller.
178 *
179 * This magic method allows you to access properties of the underlying resource
180 * as if they were properties of the current class. This provides a convenient
181 * way of accessing resource properties without having to explicitly call a method.
182 *
183 * @param string $name The name of the property to access.
184 * @param mixed $value The value to set.
185 *
186 * @return $this The current instance.
187 *
188 * @since 1.0.0
189 */
190 public function __set($name, $value)
191 {
192 $this->resource->{$name} = $value;
193 return $this;
194 }
195 /**
196 * Dynamically pass method calls of the underlying resource to the caller.
197 *
198 * This magic method allows you to call methods of the underlying resource
199 * as if they were methods of the current class. This provides a convenient
200 * way of accessing resource methods without having to explicitly call a method.
201 *
202 * @param string $method The name of the method to access.
203 * @param array $args The arguments to pass to the method.
204 *
205 * @return mixed The return value of the accessed method.
206 *
207 * @since 1.0.0
208 */
209 public function __call($method, $args)
210 {
211 return $this->resource->{$method}(...$args);
212 }
213 }
214