Skip to content

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.

1 participant