PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.5.3
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.5.3
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / api / Resource / BaseResourceApi.php

BaseResourceApi.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.5.3, at api/Resource/BaseResourceApi.php

124 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\Api\Resource;
4
5 use BadMethodCallException;
6 use FluentCart\Framework\Database\Orm\Builder;
7 use FluentCart\Framework\Foundation\App;
8 use FluentCart\Framework\Http\Response\Response;
9 use FluentCart\Framework\Pagination\AbstractPaginator;
10 use FluentCart\Framework\Support\Arr;
11 use WP_Error;
12
13
14 abstract class BaseResourceApi
15 {
16 /**
17 * Request Instance
18 * @var Response $response
19 */
20 protected $response = null;
21
22 public static function __callStatic($method, $params)
23 {
24 $class = static::class;
25
26 if (method_exists($class, $method . 'Authorized')) {
27 return call_user_func_array(array(new $class, $method . 'Authorized'), $params);
28 } else {
29 throw new BadMethodCallException();
30 }
31
32 }
33
34 public function __construct()
35 {
36 $this->response = App::getInstance()['response'];
37 }
38
39 /*
40 * @return mixed
41 */
42 abstract public static function get(array $params = []);
43
44 abstract public static function find($id, $params = []);
45
46 private static function findUsing($query): Builder
47 {
48 return static::getQuery()->search($query);
49 }
50
51 public static function search($query, ?callable $modifyQueryUsing = null, $asCollection = false)
52 {
53 $query = static::findUsing($query);
54 if (is_callable($modifyQueryUsing)) {
55 $modifiedQuery = $modifyQueryUsing($query);
56 if (is_object($modifiedQuery) && get_class($query) === Builder::class) {
57 $query = $modifiedQuery;
58 }
59 }
60
61 if (is_subclass_of($query, AbstractPaginator::class)) {
62 if ($asCollection) {
63 return $query;
64 }
65 return $query->toArray();
66 }
67 return $asCollection ? $query->get() : $query->get()->toArray();
68 }
69
70 abstract public static function create($data, $params = []);
71
72 abstract public static function update($data, $id, $params = []);
73
74 abstract public static function delete($id, $params = []);
75
76 abstract static function getQuery(): Builder;
77
78 /**
79 * @param $errors array|string = [
80 * [
81 * 'code' => string|int,
82 * 'message' => string,
83 * 'data' => Optional. Error data. Default empty string.
84 * ],
85 * string
86 * ]
87 * @return WP_Error
88 */
89 public static function makeErrorResponse($errors): WP_Error
90 {
91 $wpError = new WP_Error();
92
93 if (is_array($errors)) {
94 foreach ($errors as $key => $error) {
95 if (is_array($error)) {
96 $wpError->add(
97 Arr::get($error, 'code', $key),
98 Arr::get($error, 'message'),
99 Arr::get($error, 'data', '')
100 );
101 } else {
102 $wpError->add($key, $error);
103 }
104 }
105 } else {
106 $wpError->add($errors, $errors);
107 }
108 return $wpError;
109 }
110
111 /**
112 * @param string $message
113 * @param mixed $data
114 * @return array
115 */
116 public static function makeSuccessResponse($data, string $message): array
117 {
118 return [
119 'message' => $message,
120 'data' => $data
121 ];
122 }
123
124 }