| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Controllers\Payment; |
| 4 |
|
| 5 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 6 |
use IvyForms\Controllers\Controller; |
| 7 |
use IvyForms\Factory\Payment\PaymentFactory; |
| 8 |
use IvyForms\Services\Payment\PaymentManager; |
| 9 |
use IvyForms\Services\Translations\BackendStrings; |
| 10 |
use WP_REST_Request; |
| 11 |
use WP_REST_Response; |
| 12 |
|
| 13 |
class SearchPaymentsController extends Controller |
| 14 |
{ |
| 15 |
private PaymentManager $paymentManager; |
| 16 |
|
| 17 |
public function __construct(PaymentManager $paymentManager) |
| 18 |
{ |
| 19 |
$this->paymentManager = $paymentManager; |
| 20 |
} |
| 21 |
|
| 22 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 23 |
{ |
| 24 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 25 |
$params = Sanitizer::sanitizeSearchParams($data->get_query_params()); |
| 26 |
|
| 27 |
$result = $this->paymentManager->searchPayments($params); |
| 28 |
$payments = array_map( |
| 29 |
static function (array $row): array { |
| 30 |
$payment = PaymentFactory::create($row)->toArray(); |
| 31 |
if (isset($row['formName'])) { |
| 32 |
$payment['formName'] = (string) $row['formName']; |
| 33 |
} |
| 34 |
|
| 35 |
return $payment; |
| 36 |
}, |
| 37 |
$result['items'] |
| 38 |
); |
| 39 |
|
| 40 |
return new WP_REST_Response([ |
| 41 |
'message' => BackendStrings::getCommonStrings()['ok'], |
| 42 |
'data' => [ |
| 43 |
'data' => $payments, |
| 44 |
'meta' => $result['meta'], |
| 45 |
'filterCount' => $result['total'], |
| 46 |
], |
| 47 |
], 200); |
| 48 |
} |
| 49 |
} |
| 50 |
|