| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\ShortCodeParser\Parsers; |
| 4 |
|
| 5 |
use FluentCart\App\Helpers\Helper; |
| 6 |
use FluentCart\Framework\Support\Arr; |
| 7 |
|
| 8 |
class ItemParser extends BaseParser |
| 9 |
{ |
| 10 |
private $item; |
| 11 |
private $order; |
| 12 |
|
| 13 |
public function __construct($data) |
| 14 |
{ |
| 15 |
$this->item = Arr::get($data, 'item', []); |
| 16 |
$this->order = Arr::get($data, 'order'); |
| 17 |
parent::__construct($data); |
| 18 |
} |
| 19 |
|
| 20 |
public function parse($accessor = null, $code = null, $transformer = null): ?string |
| 21 |
{ |
| 22 |
$item = $this->item; |
| 23 |
|
| 24 |
switch ($accessor) { |
| 25 |
case 'sl': |
| 26 |
return (string) ($item['sl'] ?? ''); |
| 27 |
case 'name': |
| 28 |
return esc_html(!empty($item['post_title']) ? $item['post_title'] : ($item['title'] ?? '')); |
| 29 |
case 'variant': |
| 30 |
return esc_html($item['title'] ?? ''); |
| 31 |
case 'quantity': |
| 32 |
return esc_html($item['quantity'] ?? ''); |
| 33 |
case 'price': |
| 34 |
return isset($item['line_total']) ? (string) ($item['line_total'] / 100) : ''; |
| 35 |
case 'price_formatted': |
| 36 |
return $item['formatted_total'] ?? ''; |
| 37 |
case 'unit_price': |
| 38 |
return isset($item['unit_price']) ? (string) ($item['unit_price'] / 100) : ''; |
| 39 |
case 'unit_price_formatted': |
| 40 |
$unitPrice = isset($item['unit_price']) ? Helper::toDecimal($item['unit_price']) : ''; |
| 41 |
return esc_html($unitPrice); |
| 42 |
case 'subtotal': |
| 43 |
return isset($item['subtotal']) ? (string) ($item['subtotal'] / 100) : ''; |
| 44 |
case 'subtotal_formatted': |
| 45 |
$subtotal = isset($item['subtotal']) ? Helper::toDecimal($item['subtotal']) : ''; |
| 46 |
return esc_html($subtotal); |
| 47 |
case 'payment_info': |
| 48 |
return wp_kses_post($item['payment_info'] ?? ''); |
| 49 |
case 'payment_type': |
| 50 |
return esc_html($item['payment_type'] ?? ''); |
| 51 |
default: |
| 52 |
return Arr::get($item, $accessor, ''); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|