| 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 |
* @param int|null $status HTTP status the framework's wpErrorToResponse should use |
| 88 |
* (Route::dispatch converts any returned WP_Error via rest_convert_error_to_response, |
| 89 |
* which reads error_data['status'] and falls back to 500 when absent). |
| 90 |
* @return WP_Error |
| 91 |
*/ |
| 92 |
public static function makeErrorResponse($errors, ?int $status = null): WP_Error |
| 93 |
{ |
| 94 |
$wpError = new WP_Error(); |
| 95 |
|
| 96 |
if (is_array($errors)) { |
| 97 |
foreach ($errors as $key => $error) { |
| 98 |
if (is_array($error)) { |
| 99 |
$data = Arr::get($error, 'data', ''); |
| 100 |
if ($status) { |
| 101 |
$data = is_array($data) ? array_merge($data, ['status' => $status]) : ['status' => $status]; |
| 102 |
} |
| 103 |
$wpError->add( |
| 104 |
Arr::get($error, 'code', $key), |
| 105 |
Arr::get($error, 'message'), |
| 106 |
$data |
| 107 |
); |
| 108 |
} else { |
| 109 |
$wpError->add($key, $error, $status ? ['status' => $status] : ''); |
| 110 |
} |
| 111 |
} |
| 112 |
} else { |
| 113 |
$wpError->add($errors, $errors, $status ? ['status' => $status] : ''); |
| 114 |
} |
| 115 |
return $wpError; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @param string $message |
| 120 |
* @param mixed $data |
| 121 |
* @return array |
| 122 |
*/ |
| 123 |
public static function makeSuccessResponse($data, string $message): array |
| 124 |
{ |
| 125 |
return [ |
| 126 |
'message' => $message, |
| 127 |
'data' => $data |
| 128 |
]; |
| 129 |
} |
| 130 |
|
| 131 |
} |