| 1 |
<?php |
| 2 |
/** |
| 3 |
* Print format resolver. |
| 4 |
* |
| 5 |
* Single source of truth for the wire format a print job uses, given a |
| 6 |
* printer and a template. PrintNode chooses between PDF and raw ESC/POS based |
| 7 |
* on the printer's configured format; other providers delegate to Provider. |
| 8 |
* |
| 9 |
* @package WCPOS\WooCommercePOS\Services |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace WCPOS\WooCommercePOS\Services; |
| 13 |
|
| 14 |
/** |
| 15 |
* Print_Format_Resolver class. |
| 16 |
*/ |
| 17 |
class Print_Format_Resolver { |
| 18 |
/** |
| 19 |
* Resolve the wire format and HTTP content type for a print job. |
| 20 |
* |
| 21 |
* @param array $printer Printer configuration. |
| 22 |
* @param array $template Template configuration. |
| 23 |
* |
| 24 |
* @return array{kind:string, content_type:string} |
| 25 |
*/ |
| 26 |
public function resolve( array $printer, array $template ): array { |
| 27 |
$provider = (string) ( $printer['provider'] ?? '' ); |
| 28 |
$engine = (string) ( $template['engine'] ?? '' ); |
| 29 |
|
| 30 |
if ( 'printnode' === $provider ) { |
| 31 |
if ( 'thermal' !== $engine ) { |
| 32 |
return array( |
| 33 |
'kind' => 'pdf', |
| 34 |
'content_type' => 'application/pdf', |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
$format = (string) ( $printer['printnode_format'] ?? 'pdf' ); |
| 39 |
if ( 'raw' === $format ) { |
| 40 |
return array( |
| 41 |
'kind' => 'escpos', |
| 42 |
'content_type' => 'application/octet-stream', |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
return array( |
| 47 |
'kind' => 'pdf', |
| 48 |
'content_type' => 'application/pdf', |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
$wire = Provider::wire_format( $provider, $engine ); |
| 53 |
if ( null === $wire ) { |
| 54 |
return array( |
| 55 |
'kind' => '', |
| 56 |
'content_type' => '', |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
return array( |
| 61 |
'kind' => $wire, |
| 62 |
'content_type' => Provider::content_type( $provider ), |
| 63 |
); |
| 64 |
} |
| 65 |
} |
| 66 |
|