選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

124 行
3.3 KiB
PHP

<?php
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
class Receipt {
private $lines = [];
private $header = [];
private $footer = [];
function __construct() {
}
function appendLine(ReceiptLine $line) {
$this->lines[] = $line;
}
function appendLines($lines) {
foreach ($lines as $l) {
$this->lines[] = $l;
}
}
function appendHeader(ReceiptLine $line) {
$this->header[] = $line;
}
function appendFooter(ReceiptLine $line) {
$this->footer[] = $line;
}
function appendBreak() {
$this->lines[] = new ReceiptLine("", "", "", ReceiptLine::LINEFORMAT_HR);
}
function appendBlank() {
$this->lines[] = new ReceiptLine("", "", "", ReceiptLine::LINEFORMAT_BLANK);
}
function getHtml($title = "") {
global $SECURE_NONCE;
$html = <<<END
<!DOCTYPE html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>$title</title>
<style nonce="$SECURE_NONCE">
.flex {
display: flex;
justify-content: space-between;
margin: 0;
}
.bold {
font-weight: bold;
}
.centered {
justify-content: center;
}
</style>
END;
if (count($this->header) > 0) {
foreach ($this->header as $line) {
$html .= $line->getHtml() . "\n";
}
$html .= (new ReceiptLine("", "", "", ReceiptLine::LINEFORMAT_HR))->getHtml();
}
foreach ($this->lines as $line) {
$html .= $line->getHtml() . "\n";
}
if (count($this->footer) > 0) {
$html .= (new ReceiptLine("", "", "", ReceiptLine::LINEFORMAT_HR))->getHtml();
foreach ($this->footer as $line) {
$html .= $line->getHtml() . "\n";
}
}
return $html;
}
function getPlainText($width) {
$lines = [];
if (count($this->header) > 0) {
foreach ($this->header as $line) {
$lines[] = $line->getPlainText($width);
}
$lines[] = (new ReceiptLine("", "", "", ReceiptLine::LINEFORMAT_HR))->getPlainText($width);
}
foreach ($this->lines as $line) {
$lines[] = $line->getPlainText($width);
}
if (count($this->footer) > 0) {
$lines[] = (new ReceiptLine("", "", "", ReceiptLine::LINEFORMAT_HR))->getPlainText($width);
foreach ($this->footer as $line) {
$lines[] = $line->getPlainText($width);
}
}
return implode("\n", $lines);
}
function getArray($width = 64) {
$header = [];
$lines = [];
$footer = [];
foreach ($this->header as $line) {
$header[] = $line->getArray($width);
}
foreach ($this->lines as $line) {
$lines[] = $line->getArray($width);
}
foreach ($this->footer as $line) {
$footer[] = $line->getArray($width);
}
return ["header" => $header, "lines" => $lines, "footer" => $footer];
}
function getJson($width = 64) {
return json_encode($this->getArray($width));
}
}