Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macros for printer common tasks #1197

Open
wants to merge 1 commit into
base: development
Choose a base branch
from

Conversation

cesarreyes3
Copy link

@cesarreyes3 cesarreyes3 commented Aug 10, 2022

It would be very helpful if we could create macros on the printer class, it could simplify common tasks, for example:

Here, you create 'Item' class for formating a line

$items = array(
new item("Example item #1", "4.00"),
new item("Another thing", "3.50"),
new item("Something else", "1.00"),
new item("A final item", "4.45"),
);

public function getAsString($width = 48)
{
$rightCols = 10;
$leftCols = $width - $rightCols;
if ($this->dollarSign) {
$leftCols = $leftCols / 2 - $rightCols / 2;
}
$left = str_pad($this->name, $leftCols);
$sign = ($this->dollarSign ? '$ ' : '');
$right = str_pad($sign . $this->price, $rightCols, ' ', STR_PAD_LEFT);
return "$left$right\n";
}

With a macro we can do

Printer::macro('item', function($name = '', $price = '', $dollarSign = false, $width = 48){
        $rightCols = 10;
        $leftCols = $width - $rightCols;
        if ($dollarSign) {
            $leftCols = $leftCols / 2 - $rightCols / 2;
        }
        $left = str_pad($name, $leftCols);

        $sign = ($dollarSign ? '$ ' : '');
        $right = str_pad($sign . $price, $rightCols, ' ', STR_PAD_LEFT);
        return "$left$right\n";
});

//Example
$printer->item('Total', '14.25', true);

@mike42 I could add some test and examples if they are needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant