PHP7.2+ close to the JS code in refactoring ch.1

This commit is contained in:
Pen-y-Fan
2020-05-18 23:11:00 +01:00
parent 5bef5c1bf6
commit d5664e3d37
2 changed files with 50 additions and 50 deletions

View File

@@ -1,63 +1,62 @@
<?php
declare(strict_types=1);
namespace Theatrical;
use Theatrical\Play;
use Theatrical\Invoice;
use Theatrical\Performance;
use Error;
use NumberFormatter;
class StatementPrinter
{
public function print(Invoice $invoice, array $plays)
public function print(Invoice $invoice, array $plays): string
{
$totalAmount = 0;
$volumeCredits = 0;
$result = 'Statement for ' . $invoice->customer . '\n';
$result = "Statement for {$invoice->customer}\n";
$format = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
foreach($invoice->performances as $performance)
{
foreach ($invoice->performances as $performance) {
$play = $plays[$performance->play_id];
$thisAmount = 0;
switch($play->type)
{
case "tragedy":
switch ($play->type) {
case 'tragedy':
$thisAmount = 40000;
if($performance->audience > 30)
{
if ($performance->audience > 30) {
$thisAmount += 1000 * ($performance->audience - 30);
}
break;
case "comedy":
case 'comedy':
$thisAmount = 30000;
if($performance->audience > 20)
{
if ($performance->audience > 20) {
$thisAmount += 10000 + 500 * ($performance->audience - 20);
}
$thisAmount += 300 * $performance->audience;
break;
default:
throw new \Error("Unknow type: $play->type");
throw new Error("Unknown type: {$play->type}");
}
// add volume credits
$volumeCredits += max($performance->audience - 30, 0);
if($play->type == 'comedy')
{
// add extra credit for every ten comedy attendees
if ($play->type === 'comedy') {
$volumeCredits += floor($performance->audience / 5);
}
$thisFinalAmount = $thisAmount / 100;
$result = "$play->name: $thisFinalAmount ($performance->audience seats)\n";
// print line for this order
$result .= " {$play->name}: {$format->formatCurrency($thisAmount / 100, 'USD')} ";
$result .= "({$performance->audience} seats)\n";
$totalAmount += $thisAmount;
}
$finalTotal = ($totalAmount / 100);
$result .= "Amount owed is $finalTotal\n";
$result .= "You earned $volumeCredits credits\n";
$result .= "Amount owed is {$format ->formatCurrency($totalAmount / 100, 'USD')}\n";
$result .= "You earned {$volumeCredits} credits";
return $result;
}
}
}