Du kan inte välja fler än 25 ämnen
Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
138 rader
4.1 KiB
PHTML
138 rader
4.1 KiB
PHTML
6 år sedan
|
<?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/.
|
||
|
*/
|
||
|
|
||
|
/**
|
||
|
* A single variable-width line on a receipt, with different formatting options
|
||
|
*/
|
||
|
class ReceiptLine {
|
||
|
|
||
|
const LINEFORMAT_PLAIN = 1;
|
||
|
const LINEFORMAT_BOLD = 2;
|
||
|
const LINEFORMAT_HR = 4;
|
||
|
const LINEFORMAT_CENTER = 8;
|
||
6 år sedan
|
const LINEFORMAT_BLANK = 16;
|
||
6 år sedan
|
|
||
|
private $left = "";
|
||
|
private $middle = "";
|
||
|
private $right = "";
|
||
|
private $format;
|
||
|
|
||
|
function __construct($l = "", $m = "", $r = "", $f = ReceiptLine::LINEFORMAT_PLAIN) {
|
||
|
$this->left = $l;
|
||
|
$this->middle = $m;
|
||
|
$this->right = $r;
|
||
|
$this->format = $f;
|
||
|
}
|
||
|
|
||
|
function getLeft() {
|
||
|
return $this->left;
|
||
|
}
|
||
|
|
||
|
function getMiddle() {
|
||
|
return $this->middle;
|
||
|
}
|
||
|
|
||
|
function getRight() {
|
||
|
return $this->right;
|
||
|
}
|
||
|
|
||
|
function hasFormat($format) {
|
||
|
return ($this->format & $format);
|
||
|
}
|
||
|
|
||
|
function getHtml() {
|
||
|
if ($this->hasFormat($this::LINEFORMAT_HR)) {
|
||
|
return "<hr />";
|
||
|
}
|
||
6 år sedan
|
if ($this->hasFormat($this::LINEFORMAT_BLANK)) {
|
||
|
return "<br />";
|
||
|
}
|
||
6 år sedan
|
$html = "";
|
||
6 år sedan
|
if (!empty($this->left) || $this->left === 0 || $this->left === "0") {
|
||
6 år sedan
|
$html .= '<span>' . htmlspecialchars($this->left) . ' </span>';
|
||
|
}
|
||
6 år sedan
|
if (!empty($this->middle) || $this->middle === 0 || $this->middle === "0") {
|
||
6 år sedan
|
$html .= '<span>' . htmlspecialchars($this->middle) . ' </span>';
|
||
|
}
|
||
6 år sedan
|
if (!empty($this->right) || $this->right === 0 || $this->right === "0") {
|
||
6 år sedan
|
$html .= '<span>' . htmlspecialchars($this->right) . '</span>';
|
||
|
}
|
||
|
$classes = ["flex"];
|
||
|
if ($this->hasFormat($this::LINEFORMAT_BOLD)) {
|
||
|
$classes[] = "bold";
|
||
|
}
|
||
|
if ($this->hasFormat($this::LINEFORMAT_CENTER)) {
|
||
|
$classes[] = "centered";
|
||
|
}
|
||
|
$classstr = implode(" ", $classes);
|
||
|
return "<div class=\"$classstr\">$html</div>";
|
||
|
}
|
||
|
|
||
|
function getPlainText($width) {
|
||
|
if ($this->hasFormat($this::LINEFORMAT_HR)) {
|
||
|
return str_repeat("-", $width);
|
||
|
}
|
||
6 år sedan
|
if ($this->hasFormat($this::LINEFORMAT_BLANK)) {
|
||
|
return str_repeat(" ", $width);
|
||
|
}
|
||
6 år sedan
|
$left = $this->left;
|
||
|
$middle = $this->middle;
|
||
|
$right = $this->right;
|
||
|
$leftln = strlen($left);
|
||
|
$middleln = strlen($middle);
|
||
|
$rightln = strlen($right);
|
||
|
if ($middleln > 0) {
|
||
|
$middleln++;
|
||
|
$middle = " " . $middle;
|
||
|
}
|
||
|
if ($rightln > 0) {
|
||
|
$rightln++;
|
||
|
$right = " " . $right;
|
||
|
}
|
||
|
$strln = $leftln + $middleln + $rightln;
|
||
|
if ($strln < $width) {
|
||
|
if ($this->hasFormat($this::LINEFORMAT_CENTER)) {
|
||
|
return str_pad($left . $middle . $right, $width, " ", STR_PAD_BOTH);
|
||
|
} else {
|
||
|
$middle = str_pad($middle, $width - $leftln - $rightln, " ", STR_PAD_BOTH);
|
||
|
}
|
||
|
} else if ($strln > $width) {
|
||
|
$loseln = $strln - $width;
|
||
|
$left = substr($this->left, 0, $leftln - $loseln);
|
||
|
}
|
||
|
return $left . $middle . $right;
|
||
|
}
|
||
|
|
||
|
function getArray($width = 64) {
|
||
|
$data = [
|
||
|
"left" => $this->left,
|
||
|
"middle" => $this->middle,
|
||
|
"right" => $this->right,
|
||
|
"text" => $this->getPlainText($width),
|
||
|
"format" => []
|
||
|
];
|
||
|
if ($this->hasFormat($this::LINEFORMAT_PLAIN)) {
|
||
|
$data['format'][] = "plain";
|
||
|
}
|
||
|
if ($this->hasFormat($this::LINEFORMAT_BOLD)) {
|
||
|
$data['format'][] = "bold";
|
||
|
}
|
||
|
if ($this->hasFormat($this::LINEFORMAT_HR)) {
|
||
|
$data['format'][] = "hr";
|
||
|
}
|
||
|
if ($this->hasFormat($this::LINEFORMAT_CENTER)) {
|
||
|
$data['format'][] = "center";
|
||
|
}
|
||
6 år sedan
|
if ($this->hasFormat($this::LINEFORMAT_BLANK)) {
|
||
|
$data['format'][] = "blank";
|
||
|
}
|
||
6 år sedan
|
return $data;
|
||
|
}
|
||
|
|
||
|
}
|