From 030c9ef8f0824ea2f8a33d3839ef9ebe30e44fa4 Mon Sep 17 00:00:00 2001 From: MartkCz Date: Wed, 27 Feb 2019 16:15:55 +0100 Subject: [PATCH] contributte --- .travis.yml | 40 ++++++++++++----- README.md | 26 +++++------ composer.json | 27 +++++++++-- ruleset.xml | 19 ++++++++ src/Calculators/BcCalculator.php | 32 +++++++------ src/Calculators/FloatCalculator.php | 21 +++++---- src/Calculators/ICalculator.php | 6 +-- src/Components/Paginator.php | 34 ++++++++------ src/DI/InvoiceExtension.php | 39 ++++++++-------- src/Data/Account.php | 23 +++++----- src/Data/Company.php | 15 ++++--- src/Data/Customer.php | 9 ++-- src/Data/Item.php | 42 ++++++++--------- src/Data/Order.php | 53 +++++++++++----------- src/Data/PaymentInformation.php | 24 +++++----- src/Data/Subject.php | 33 ++++++++------ src/Formatter.php | 37 +++++++++------ src/IFormatter.php | 9 ++-- src/ITranslator.php | 9 ++-- src/Invoice.php | 48 ++++++++++---------- src/InvoiceException.php | 12 +++-- src/Preview/PreviewFactory.php | 19 ++++---- src/Renderers/Color.php | 39 +++++++++------- src/Renderers/IRenderer.php | 14 +++--- src/Renderers/PDF.php | 26 ++++++----- src/Renderers/PDFRenderer.php | 64 ++++++++++++++++---------- src/Renderers/Settings.php | 41 ++++++++++------- src/Responses/PdfResponse.php | 11 +++-- src/Templates/DefaultTemplate.php | 67 +++++++++++++++------------- src/Templates/ITemplate.php | 21 +++++---- src/Translator.php | 27 ++++++----- tests/_support/AcceptanceTester.php | 14 +++--- tests/_support/FunctionalTester.php | 14 +++--- tests/_support/Helper/Acceptance.php | 1 + tests/_support/Helper/Functional.php | 1 + tests/_support/Helper/Unit.php | 1 + tests/_support/UnitTester.php | 14 +++--- tests/acceptance.suite.yml | 8 ++-- tests/functional.suite.yml | 6 +-- tests/unit.suite.yml | 6 +-- tests/unit/ItemTest.php | 26 ++++++----- tests/unit/OrderTest.php | 28 +++++++----- 42 files changed, 584 insertions(+), 422 deletions(-) create mode 100644 ruleset.xml diff --git a/.travis.yml b/.travis.yml index 832e979..9835dee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,19 +1,35 @@ language: php +php: + - 7.2 + - 7.3 -os: - - linux +before_install: + # Turn off XDebug + - phpenv config-rm xdebug.ini || return 0 -php: - - 7.1 +install: + # Composer + - travis_retry composer install --no-progress --prefer-dist +script: + # Tests + - composer run-script tests -matrix: +jobs: include: - - php: 7.1 - env: PHPSTAN=1 + - env: title="Lowest Dependencies 7.2" + php: 7.2 + install: + - travis_retry composer update --no-progress --prefer-dist --prefer-lowest + script: + - composer run-script tests -install: - - composer self-update - - composer install + - stage: Quality Assurance + php: 7.2 + script: + - composer run-script qa -script: - - if [ "$PHPSTAN" = "1" ]; then vendor/bin/phpstan analyse src --level=6 --ansi --no-progress; fi +sudo: false + +cache: + directories: + - $HOME/.composer/cache diff --git a/README.md b/README.md index 889570c..1009737 100755 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Build Status](https://travis-ci.org/WebChemistry/invoice.svg?branch=master)](https://travis-ci.org/WebChemistry/invoice) +[![Build Status](https://travis-ci.org/Contributte/invoice.svg?branch=master)](https://travis-ci.org/Contributte/invoice) # PHP Invoice @@ -9,7 +9,7 @@ Average output ~20ms php 7.1 ``` -composer require webchemistry/invoice +composer require contributte/invoice ``` ## Usage @@ -17,31 +17,31 @@ composer require webchemistry/invoice ### Company ```php -$company = new WebChemistry\Invoice\Data\Company('John Doe', 'Los Angeles', 'Cavetown', '720 55', 'USA', '0123456789', 'CZ0123456789'); +$company = new Contributte\Invoice\Data\Company('John Doe', 'Los Angeles', 'Cavetown', '720 55', 'USA', '0123456789', 'CZ0123456789'); ``` ### Customer ```php -$customer = new WebChemistry\Invoice\Data\Customer('John Doe', 'Los Angeles', 'Cavetown', '720 55', 'USA'); +$customer = new Contributte\Invoice\Data\Customer('John Doe', 'Los Angeles', 'Cavetown', '720 55', 'USA'); ``` ### Account ```php -$account = new WebChemistry\Invoice\Data\Account('1111', 'CZ4808000000002353462015', 'GIGACZPX'); +$account = new Contributte\Invoice\Data\Account('1111', 'CZ4808000000002353462015', 'GIGACZPX'); ``` ### Payment info ```php -$payment = new WebChemistry\Invoice\Data\PaymentInformation('Kč', '0123456789', '1234', 0.21); +$payment = new Contributte\Invoice\Data\PaymentInformation('Kč', '0123456789', '1234', 0.21); ``` ### Order ```php -$order = new WebChemistry\Invoice\Data\Order('20160001', new \DateTime('+ 14 days'), $account, $payment); +$order = new Contributte\Invoice\Data\Order('20160001', new \DateTime('+ 14 days'), $account, $payment); ``` Adding items @@ -61,7 +61,7 @@ class CustomFormatter implements IFormatter { ## Generating invoices ```php -$invoice = new WebChemistry\Invoice\Invoice($company); +$invoice = new Contributte\Invoice\Invoice($company); header('Content-Type: application/pdf; charset=utf-8'); echo $invoice->create($customer, $order); @@ -69,14 +69,14 @@ echo $invoice->create($customer, $order); shorter ```php -$invoice = new WebChemistry\Invoice\Invoice($company); +$invoice = new Contributte\Invoice\Invoice($company); $invoice->send($customer, $order); ``` nette framework way ```php -$invoice = new WebChemistry\Invoice\Invoice($company); +$invoice = new Contributte\Invoice\Invoice($company); $this->sendResponse($invoice->createResponse($customer, $order)); ``` @@ -84,7 +84,7 @@ $this->sendResponse($invoice->createResponse($customer, $order)); ## Generating preview ```php -$invoice->send(WebChemistry\Invoice\Preview\PreviewFactory::createCustomer(), WebChemistry\Invoice\Preview\PreviewFactory::createOrder()); +$invoice->send(Contributte\Invoice\Preview\PreviewFactory::createCustomer(), Contributte\Invoice\Preview\PreviewFactory::createOrder()); ``` ```php @@ -95,7 +95,7 @@ $invoice->send(WebChemistry\Invoice\Preview\PreviewFactory::createCustomer(), We ```yaml extensions: - invoice: WebChemistry\Invoice\DI\InvoiceExtension + invoice: Contributte\Invoice\DI\InvoiceExtension invoice: lang: en @@ -115,7 +115,7 @@ invoice: class Component { - public function __construct(WebChemistry\Invoice\Invoice $invoice) { + public function __construct(Contributte\Invoice\Invoice $invoice) { // ... } } diff --git a/composer.json b/composer.json index fb9920b..58c8ea4 100755 --- a/composer.json +++ b/composer.json @@ -1,9 +1,9 @@ { - "name": "webchemistry/invoice", + "name": "contributte/invoice", "type": "library", "description": "Library for easily and quickly creating invoices for customers.", "tags": [ - "webchemistry", + "contributte", "invoice" ], "require": { @@ -12,15 +12,36 @@ "setasign/fpdf": "^1.8" }, "require-dev": { + "ninjify/qa": "^0.8.0", + "ninjify/nunjuck": "^0.2.0", "nette/di": "^3.0", "nette/application": "^3.0", "codeception/codeception": "^2.4" }, "autoload": { "psr-4": { - "WebChemistry\\Invoice\\": "src/" + "Contributte\\Invoice\\": "src/" } }, + "scripts": { + "qa": [ + "linter src tests", + "codesniffer src tests" + ], + "tests": [ + "vendor/bin/codecept run" + ], + "phpstan-install": [ + "mkdir -p temp/phpstan", + "composer require -d temp/phpstan phpstan/phpstan:^0.10", + "composer require -d temp/phpstan phpstan/phpstan-deprecation-rules:^0.10", + "composer require -d temp/phpstan phpstan/phpstan-nette:^0.10", + "composer require -d temp/phpstan phpstan/phpstan-strict-rules:^0.10" + ], + "phpstan": [ + "temp/phpstan/vendor/bin/phpstan analyse -l max src" + ] + }, "minimum-stability": "RC", "prefer-stable": true } diff --git a/ruleset.xml b/ruleset.xml new file mode 100644 index 0000000..3540b8e --- /dev/null +++ b/ruleset.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + /tests/ + /src/Data/Account.php + /src/Renderers/PDF.php + diff --git a/src/Calculators/BcCalculator.php b/src/Calculators/BcCalculator.php index a821fd1..6a34bcd 100644 --- a/src/Calculators/BcCalculator.php +++ b/src/Calculators/BcCalculator.php @@ -1,15 +1,19 @@ scale = $scale; } @@ -17,45 +21,45 @@ public function __construct(int $scale = 0) { /** * @param string|int|float $op1 * @param string|int|float $op2 - * @return string */ - public function add($op1, $op2): string { + public function add($op1, $op2): string + { return bcadd((string) $op1, (string) $op2, $this->scale); } /** * @param string|int|float $op1 * @param string|int|float $op2 - * @return string */ - public function mul($op1, $op2): string { + public function mul($op1, $op2): string + { return bcmul((string) $op1, (string) $op2, $this->scale); } /** * @param string|int|float $op1 * @param string|int|float $op2 - * @return string|null */ - public function div($op1, $op2): ?string { + public function div($op1, $op2): ?string + { return bcdiv((string) $op1, (string) $op2, $this->scale); } /** * @param string|int|float $op1 * @param string|int|float $op2 - * @return string */ - public function sub($op1, $op2) { + public function sub($op1, $op2): string + { return bcsub((string) $op1, (string) $op2, $this->scale); } /** * @param string|int|float $op1 * @param string|int|float $op2 - * @return int */ - public function comp($op1, $op2): int { + public function comp($op1, $op2): int + { return bccomp((string) $op1, (string) $op2, $this->scale); } diff --git a/src/Calculators/FloatCalculator.php b/src/Calculators/FloatCalculator.php index 59afccc..024baee 100644 --- a/src/Calculators/FloatCalculator.php +++ b/src/Calculators/FloatCalculator.php @@ -1,15 +1,17 @@ $op2; } diff --git a/src/Calculators/ICalculator.php b/src/Calculators/ICalculator.php index ed39bc2..266e07a 100644 --- a/src/Calculators/ICalculator.php +++ b/src/Calculators/ICalculator.php @@ -1,8 +1,9 @@ items = $items; - $this->totalPages = (int)ceil(count($this->items) / $itemsPerPage); + $this->totalPages = (int) ceil(count($this->items) / $itemsPerPage); $this->itemsPerPage = $itemsPerPage; } - public function getTotalPages(): int { + public function getTotalPages(): int + { return $this->totalPages; } /** * @return Item[] */ - public function getItems(): array { + public function getItems(): array + { $page = $this->currentPage - 1; return array_slice($this->items, $page * $this->itemsPerPage, $page * $this->itemsPerPage + $this->itemsPerPage); } - public function isFirstPage(): bool { + public function isFirstPage(): bool + { return $this->currentPage === 1; } - public function isLastPage(): bool { + public function isLastPage(): bool + { return $this->currentPage >= $this->getTotalPages(); } - public function getCurrentPage(): int { + public function getCurrentPage(): int + { return $this->currentPage; } - public function nextPage(): bool { + public function nextPage(): bool + { if ($this->isLastPage()) { return false; } diff --git a/src/DI/InvoiceExtension.php b/src/DI/InvoiceExtension.php index a7efb94..da4308f 100755 --- a/src/DI/InvoiceExtension.php +++ b/src/DI/InvoiceExtension.php @@ -1,24 +1,23 @@ - [ 'name' => null, @@ -33,7 +32,8 @@ class InvoiceExtension extends CompilerExtension { 'lang' => 'en', ]; - public function loadConfiguration(): void { + public function loadConfiguration(): void + { $builder = $this->getContainerBuilder(); $config = $this->validateConfig($this->defaults); @@ -58,7 +58,6 @@ public function loadConfiguration(): void { $builder->addDefinition($this->prefix('invoice')) ->setFactory(Invoice::class); - } } diff --git a/src/Data/Account.php b/src/Data/Account.php index 77eeb15..ce38773 100755 --- a/src/Data/Account.php +++ b/src/Data/Account.php @@ -1,12 +1,11 @@ -accountNumber = $accountNumber; $this->iBan = $iBan; $this->swift = $swift; } - ///////////////////////////////////////////////////////////////// - - public function getAccountNumber(): string { + public function getAccountNumber(): string + { return $this->accountNumber; } - public function getIBan(): ?string { + public function getIBan(): ?string + { return $this->iBan; } - public function getSwift(): ?string { + public function getSwift(): ?string + { return $this->swift; } diff --git a/src/Data/Company.php b/src/Data/Company.php index 681623c..ebebdac 100755 --- a/src/Data/Company.php +++ b/src/Data/Company.php @@ -1,20 +1,21 @@ -hasTax = $hasTax; } - public function hasTax(): bool { + public function hasTax(): bool + { return $this->hasTax; } diff --git a/src/Data/Customer.php b/src/Data/Customer.php index fdfe50a..56ea5fa 100755 --- a/src/Data/Customer.php +++ b/src/Data/Customer.php @@ -1,9 +1,8 @@ -name = $name; $this->count = $count; $this->price = $price; @@ -45,46 +43,44 @@ public function __construct(string $name, $price, $count, ?float $tax = null) { * @param float|int|string $totalPrice * @return static */ - public function setTotalPrice($totalPrice) { + public function setTotalPrice($totalPrice) + { $this->totalPrice = $totalPrice; return $this; } - /** - * @return float|null - */ - public function getTax(): ?float { + public function getTax(): ?float + { return $this->tax; } - /** - * @return string - */ - public function getName(): string { + public function getName(): string + { return $this->name; } /** * @return int|float */ - public function getCount() { + public function getCount() + { return $this->count; } /** * @return int|float */ - public function getPrice() { + public function getPrice() + { return $this->price; } /** - * @param ICalculator $calculator - * @param bool $useTax * @return float|int|string */ - public function getTotalPrice(ICalculator $calculator, bool $useTax = false) { + public function getTotalPrice(ICalculator $calculator, bool $useTax = false) + { if ($this->totalPrice !== null) { return $calculator->add($this->totalPrice, 0); } diff --git a/src/Data/Order.php b/src/Data/Order.php index 6f09390..fcd684c 100755 --- a/src/Data/Order.php +++ b/src/Data/Order.php @@ -1,14 +1,13 @@ -number = $number; $this->dueDate = $dueDate; $this->account = $account; $this->payment = $payment; - $this->created = $created ? : new DateTime(); + $this->created = $created ?: new DateTime(); } /** - * @param string $name * @param int|float $price * @param int|float $count - * @param float|null $tax - * @return Item */ - public function addItem(string $name, $price, $count = 1, ?float $tax = null): Item { + public function addItem(string $name, $price, $count = 1, ?float $tax = null): Item + { return $this->items[] = new Item($name, $price, $count, $tax ?: $this->getPayment()->getTax()); } - ///////////////////////////////////////////////////////////////// - /** * @param float|int|string|null $totalPrice * @return static */ - public function setTotalPrice($totalPrice) { + public function setTotalPrice($totalPrice) + { $this->totalPrice = $totalPrice; return $this; } - public function getNumber(): string { + public function getNumber(): string + { return $this->number; } - public function getDueDate(): ?DateTime { + public function getDueDate(): ?DateTime + { return $this->dueDate; } - public function getAccount(): ?Account { + public function getAccount(): ?Account + { return $this->account; } - public function getPayment(): PaymentInformation { + public function getPayment(): PaymentInformation + { return $this->payment; } - public function getCreated(): DateTime { + public function getCreated(): DateTime + { return $this->created; } /** * @return Item[] */ - public function getItems(): array { + public function getItems(): array + { return $this->items; } /** - * @param ICalculator $calculator - * @param bool $useTax * @return float|int|string */ - public function getTotalPrice(ICalculator $calculator, bool $useTax = false) { + public function getTotalPrice(ICalculator $calculator, bool $useTax = false) + { if ($this->totalPrice !== null) { return $this->totalPrice; } @@ -109,5 +110,5 @@ public function getTotalPrice(ICalculator $calculator, bool $useTax = false) { return $total; } - + } diff --git a/src/Data/PaymentInformation.php b/src/Data/PaymentInformation.php index 3e585ed..669270f 100755 --- a/src/Data/PaymentInformation.php +++ b/src/Data/PaymentInformation.php @@ -1,12 +1,11 @@ -currency = $currency; $this->variableSymbol = $variableSymbol; $this->constantSymbol = $constantSymbol; $this->tax = $tax; } - public function getCurrency(): string { + public function getCurrency(): string + { return $this->currency; } - public function getVariableSymbol(): ?string { + public function getVariableSymbol(): ?string + { return $this->variableSymbol; } - public function getConstantSymbol(): ?string { + public function getConstantSymbol(): ?string + { return $this->constantSymbol; } - public function getTax(): ?float { + public function getTax(): ?float + { return $this->tax; } diff --git a/src/Data/Subject.php b/src/Data/Subject.php index dfb0013..d4e32d3 100755 --- a/src/Data/Subject.php +++ b/src/Data/Subject.php @@ -1,12 +1,11 @@ -name = $name; $this->town = $town; $this->address = $address; @@ -41,31 +41,38 @@ public function __construct(string $name, ?string $town = null, ?string $address $this->vaTin = $vaTin; } - public function getName(): string { + public function getName(): string + { return $this->name; } - public function getTown(): ?string { + public function getTown(): ?string + { return $this->town; } - public function getAddress(): ?string { + public function getAddress(): ?string + { return $this->address; } - public function getZip(): ?string { + public function getZip(): ?string + { return $this->zip; } - public function getCountry(): ?string { + public function getCountry(): ?string + { return $this->country; } - public function getTin(): ?string { + public function getTin(): ?string + { return $this->tin; } - public function getVaTin(): ?string { + public function getVaTin(): ?string + { return $this->vaTin; } diff --git a/src/Formatter.php b/src/Formatter.php index 0ec61db..c3e25c9 100644 --- a/src/Formatter.php +++ b/src/Formatter.php @@ -1,32 +1,33 @@ - [ 'number' => [ 'dec' => ',', - 'sep' => ' ' + 'sep' => ' ', ], 'money' => '%money %currency', 'date' => 'd.m.Y', ], 'en' => [ 'number' => [ - 'dec' => NULL, - 'sep' => NULL + 'dec' => null, + 'sep' => null, ], 'money' => '%currency %money', 'date' => 'd/m/Y', @@ -36,19 +37,29 @@ class Formatter implements IFormatter { /** @var string */ private $lang; - public function __construct(string $lang = self::ENGLISH) { + public function __construct(string $lang = self::ENGLISH) + { $this->lang = $lang; } - public function formatNumber($number): string { + /** + * @param mixed $number + */ + public function formatNumber($number): string + { return number_format((float) $number, 2, self::$options[$this->lang]['number']['dec'], self::$options[$this->lang]['number']['sep']); } - public function formatMoney($number, string $currency): string { + /** + * @param mixed $number + */ + public function formatMoney($number, string $currency): string + { return strtr(self::$options[$this->lang]['money'], ['%money' => $this->formatNumber($number), '%currency' => $currency]); } - public function formatDate(\DateTime $date): string { + public function formatDate(DateTime $date): string + { return $date->format(self::$options[$this->lang]['date']); } diff --git a/src/IFormatter.php b/src/IFormatter.php index a13eab6..616bf59 100644 --- a/src/IFormatter.php +++ b/src/IFormatter.php @@ -1,12 +1,11 @@ -company = $company; $this->template = $template ?: new DefaultTemplate(); $this->renderer = $renderer ?: new PDFRenderer(); $this->calculator = $calculator ?: new FloatCalculator(); } - public function create(Customer $customer, Order $order): string { + public function create(Customer $customer, Order $order): string + { return $this->template->build($this->calculator, $this->renderer, $customer, $order, $this->company); } - public function send(Customer $customer, Order $order): void { + public function send(Customer $customer, Order $order): void + { header('Content-type: application/pdf'); echo $this->create($customer, $order); } - public function createResponse(Customer $customer, Order $order): IResponse { + public function createResponse(Customer $customer, Order $order): IResponse + { return new PdfResponse($this->create($customer, $order)); } diff --git a/src/InvoiceException.php b/src/InvoiceException.php index 1e1b448..6b0773e 100755 --- a/src/InvoiceException.php +++ b/src/InvoiceException.php @@ -1,15 +1,19 @@ -red = $red; $this->green = $green; $this->blue = $blue; } - public function getRed(): int { + public function getRed(): int + { return $this->red; } - public function getGreen(): int { + public function getGreen(): int + { return $this->green; } - public function getBlue(): int { + public function getBlue(): int + { return $this->blue; } - protected function adjustColor(int $dimension): int { + protected function adjustColor(int $dimension): int + { return max(0, min(255, $dimension)); } - protected function lightenDarken(int $percentage): Color { + protected function lightenDarken(int $percentage): Color + { $percentage = round($percentage / 100, 2); return new Color( @@ -51,23 +56,27 @@ protected function lightenDarken(int $percentage): Color { ); } - public function lighten(int $percentage): Color { + public function lighten(int $percentage): Color + { $percentage = max(0, min(100, $percentage)); return $this->lightenDarken(-$percentage); } - public function darken(int $percentage): Color { + public function darken(int $percentage): Color + { $percentage = max(0, min(100, $percentage)); return $this->lightenDarken($percentage); } - public static function black(): Color { + public static function black(): Color + { return new Color(0, 0, 0); } - public static function white(): Color { + public static function white(): Color + { return new Color(255, 255, 255); } diff --git a/src/Renderers/IRenderer.php b/src/Renderers/IRenderer.php index bb23af1..31e67e8 100644 --- a/src/Renderers/IRenderer.php +++ b/src/Renderers/IRenderer.php @@ -1,12 +1,11 @@ -k = 72/96; + $this->k = 72 / 96; - $this->wPt = $this->w*$this->k; - $this->hPt = $this->h*$this->k; + $this->wPt = $this->w * $this->k; + $this->hPt = $this->h * $this->k; } } - function SetFontPath($fontPath) { + function SetFontPath($fontPath) + { $this->fontpath = $fontPath; } - function Polygon($points, $style = 'D') { + function Polygon($points, $style = 'D') + { //Draw a polygon if ($style == 'F') { $op = 'f'; - } else if ($style == 'FD' || $style == 'DF') { + } elseif ($style == 'FD' || $style == 'DF') { $op = 'b'; } else { $op = 's'; diff --git a/src/Renderers/PDFRenderer.php b/src/Renderers/PDFRenderer.php index e890d7d..5102a09 100644 --- a/src/Renderers/PDFRenderer.php +++ b/src/Renderers/PDFRenderer.php @@ -1,19 +1,18 @@ - null, 'size' => 15, @@ -21,30 +20,36 @@ class PDFRenderer implements IRenderer { 'align' => Settings::ALIGN_JUSTIFY, ]; - public function x(): int { + public function x(): int + { return (int) $this->pdf->GetX(); } - public function y(): int { + public function y(): int + { return (int) $this->pdf->GetY(); } - public function textWidth(string $text, ?callable $setCallback = null): float { + public function textWidth(string $text, ?callable $setCallback = null): float + { $settings = $this->extractSettings($setCallback); $this->setFont($settings); return $this->pdf->GetStringWidth($text); } - public function width(): float { + public function width(): float + { return $this->pdf->GetPageWidth(); } - public function height(): float { + public function height(): float + { return $this->pdf->GetPageHeight(); } - public function createNew(): void { + public function createNew(): void + { $this->pdf = new PDF('P', 'px', 'A4'); $this->pdf->SetFontPath(IRenderer::ASSETS_PATH); $this->pdf->SetAutoPageBreak(false); @@ -52,15 +57,18 @@ public function createNew(): void { $this->addPage(); } - public function addPage(): void { + public function addPage(): void + { $this->pdf->AddPage('P', 'A4'); } - public function addFont(string $family, string $file, string $fontStyle = Settings::FONT_STYLE_NONE): void { + public function addFont(string $family, string $file, string $fontStyle = Settings::FONT_STYLE_NONE): void + { $this->pdf->AddFont($family, $fontStyle, $file); } - public function rect(float $x, float $y, float $width, float $height, ?callable $setCallback = null): void { + public function rect(float $x, float $y, float $width, float $height, ?callable $setCallback = null): void + { $settings = $this->extractSettings($setCallback); $this->setDrawing($settings); @@ -68,7 +76,11 @@ public function rect(float $x, float $y, float $width, float $height, ?callable $this->pdf->Rect($x, $y, $width, $height, 'DF'); } - public function polygon(array $points, ?callable $setCallback = null): void { + /** + * @param mixed[] $points + */ + public function polygon(array $points, ?callable $setCallback = null): void + { $settings = $this->extractSettings($setCallback); $this->setDrawing($settings); @@ -79,10 +91,10 @@ public function polygon(array $points, ?callable $setCallback = null): void { /** * (border, fill, align) */ - public function cell(float $x, float $y, float $width, ?float $height, ?string $text, ?callable $setCallback = null): void { + public function cell(float $x, float $y, float $width, ?float $height, ?string $text, ?callable $setCallback = null): void + { $text = iconv('UTF-8', 'WINDOWS-1250//TRANSLIT', (string) $text); - $settings = $this->extractSettings($setCallback); $this->restoreSettings($settings, 'align'); @@ -96,11 +108,13 @@ public function cell(float $x, float $y, float $width, ?float $height, ?string $ } } - public function output(): string { + public function output(): string + { return $this->pdf->Output('S'); } - protected function setFont(Settings $settings): void { + protected function setFont(Settings $settings): void + { if ($settings->fontFamily === null && $settings->fontSize === null && $settings->fontColor === null) { return; } @@ -116,7 +130,8 @@ protected function setFont(Settings $settings): void { } } - protected function setDrawing(Settings $settings): void { + protected function setDrawing(Settings $settings): void + { if ($settings->drawColor !== null) { $color = $settings->drawColor; @@ -129,7 +144,8 @@ protected function setDrawing(Settings $settings): void { } } - protected function extractSettings(?callable $callback = null): Settings { + protected function extractSettings(?callable $callback = null): Settings + { $settings = new Settings(); if ($callback !== null) { $callback($settings); @@ -138,7 +154,8 @@ protected function extractSettings(?callable $callback = null): Settings { return $settings; } - protected function restoreSettings(Settings $settings, string $name): void { + protected function restoreSettings(Settings $settings, string $name): void + { if ($settings->$name === null) { $settings->$name = $this->cache[$name]; } else { @@ -146,5 +163,4 @@ protected function restoreSettings(Settings $settings, string $name): void { } } - } diff --git a/src/Renderers/Settings.php b/src/Renderers/Settings.php index f0deb75..a361de9 100644 --- a/src/Renderers/Settings.php +++ b/src/Renderers/Settings.php @@ -1,62 +1,71 @@ -drawColor = $this->fillColor = $color; + + return $this; } } diff --git a/src/Responses/PdfResponse.php b/src/Responses/PdfResponse.php index 015aa04..14cb044 100644 --- a/src/Responses/PdfResponse.php +++ b/src/Responses/PdfResponse.php @@ -1,21 +1,24 @@ content = $content; } - public function send(IRequest $httpRequest, NetteIResponse $httpResponse): void { + public function send(IRequest $httpRequest, NetteIResponse $httpResponse): void + { $httpResponse->setContentType('application/pdf', 'utf-8'); echo $this->content; } diff --git a/src/Templates/DefaultTemplate.php b/src/Templates/DefaultTemplate.php index 325eb28..9259b9e 100644 --- a/src/Templates/DefaultTemplate.php +++ b/src/Templates/DefaultTemplate.php @@ -1,25 +1,24 @@ -primary = new Color(6, 178, 194); $this->font = new Color(52, 52, 53); $this->even = new Color(241, 240, 240); @@ -65,7 +65,8 @@ public function __construct(?ITranslator $translator = null, ?Formatter $formatt $this->formatter = $formatter ?: new Formatter(); } - public function build(ICalculator $calculator, IRenderer $renderer, Customer $customer, Order $order, Company $company): string { + public function build(ICalculator $calculator, IRenderer $renderer, Customer $customer, Order $order, Company $company): string + { $this->renderer = $renderer; $this->customer = $customer; $this->order = $order; @@ -103,7 +104,8 @@ public function build(ICalculator $calculator, IRenderer $renderer, Customer $cu return $this->renderer->output(); } - protected function buildTotal(int $offset): void { + protected function buildTotal(int $offset): void + { $renderer = $this->renderer; $half = ($renderer->width() - 553) / 2; @@ -130,7 +132,8 @@ protected function buildTotal(int $offset): void { }); } - protected function buildItems(int $offset, array $items): int { + protected function buildItems(int $offset, array $items): int + { $renderer = $this->renderer; /** @@ -173,7 +176,8 @@ protected function buildItems(int $offset, array $items): int { return $offset; } - protected function buildMain(): void { + protected function buildMain(): void + { $renderer = $this->renderer; $renderer->rect(0, 307, $renderer->width(), 29, function (Settings $settings) { @@ -189,7 +193,6 @@ protected function buildMain(): void { }); $renderer->rect(0, 336, $renderer->width(), 1); - $renderer->cell(33, 307, 360, 29.0, Strings::upper($this->translator->translate('item')), function (Settings $settings) { $settings->fontColor = $this->primary; $settings->fontStyle = $settings::FONT_STYLE_BOLD; @@ -210,7 +213,8 @@ protected function buildMain(): void { }); } - protected function buildHeader(): void { + protected function buildHeader(): void + { $renderer = $this->renderer; $renderer->polygon([ @@ -308,7 +312,8 @@ protected function buildHeader(): void { }); } - protected function buildLeftPane(): void { + protected function buildLeftPane(): void + { $renderer = $this->renderer; $text = Strings::upper($this->translator->translate('subscriber')); @@ -321,8 +326,8 @@ protected function buildLeftPane(): void { $x = $renderer->textWidth($text) + 76; $x = max($x, $renderer->textWidth($this->customer->getName(), function (Settings $settings) { - $settings->fontSize = 7; - }) + 76); + $settings->fontSize = 7; + }) + 76); $renderer->polygon([ $x, 190, @@ -366,7 +371,8 @@ protected function buildLeftPane(): void { } } - protected function buildRightPane(): void { + protected function buildRightPane(): void + { $renderer = $this->renderer; $renderer->cell(450, 145, 1, null, Strings::upper($this->translator->translate('paymentData')), function (Settings $settings) { @@ -445,7 +451,8 @@ protected function buildRightPane(): void { $renderer->cell(465 + 100, 189 + ($multiplier * 15), 1, null, $this->formatter->formatMoney($this->order->getTotalPrice($this->calculator, $this->company->hasTax()), $this->order->getPayment()->getCurrency()), $textCb); } - protected function buildFooter(Paginator $paginator): void { + protected function buildFooter(Paginator $paginator): void + { $renderer = $this->renderer; $renderer->rect(0, $renderer->height() - 20, $renderer->width(), 20, function (Settings $settings) { diff --git a/src/Templates/ITemplate.php b/src/Templates/ITemplate.php index 43429d8..1c86e22 100644 --- a/src/Templates/ITemplate.php +++ b/src/Templates/ITemplate.php @@ -1,18 +1,17 @@ - [ 'subscriber' => 'Odběratel', @@ -37,7 +38,7 @@ class Translator implements ITranslator { 'constSymbol' => 'Konstant. symbol', 'tax' => 'DPH', 'subtotal' => 'Mezisoučet', - 'dueDate' => 'Datum splatnosti' + 'dueDate' => 'Datum splatnosti', ], 'en' => [ 'subscriber' => 'Subscriber', @@ -63,21 +64,23 @@ class Translator implements ITranslator { 'constSymbol' => 'Constant symbol', 'tax' => 'TAX', 'subtotal' => 'Subtotal', - 'dueDate' => 'Due date' - ] + 'dueDate' => 'Due date', + ], ]; /** @var string */ private $lang; - public function __construct(string $lang = self::ENGLISH) { + public function __construct(string $lang = self::ENGLISH) + { $this->lang = $lang; if (!isset(self::$translations[$this->lang])) { - throw new InvoiceException("Language $lang not exists."); + throw new InvoiceException(sprintf('Language %s not exists.', $lang)); } } - public function translate(string $message): string { + public function translate(string $message): string + { return self::$translations[$this->lang][$message]; } diff --git a/tests/_support/AcceptanceTester.php b/tests/_support/AcceptanceTester.php index 4c7dcbb..995d208 100644 --- a/tests/_support/AcceptanceTester.php +++ b/tests/_support/AcceptanceTester.php @@ -1,6 +1,5 @@ item = new \WebChemistry\Invoice\Data\Item('Foo', 15, 2, 0.10); - $this->calculator = new \WebChemistry\Invoice\Calculators\BcCalculator(2); + protected function _before() + { + $this->item = new \Contributte\Invoice\Data\Item('Foo', 15, 2, 0.10); + $this->calculator = new \Contributte\Invoice\Calculators\BcCalculator(2); } // tests - public function testTotalPriceWithoutTax() { + public function testTotalPriceWithoutTax() + { $this->assertSame('30.00', $this->item->getTotalPrice($this->calculator, false)); } - public function testTotalPriceSetFixed() { + public function testTotalPriceSetFixed() + { $this->item->setTotalPrice(40); $this->assertSame('40.00', $this->item->getTotalPrice($this->calculator, false)); } - public function testTotalPriceWithTax() { + public function testTotalPriceWithTax() + { $this->assertSame('33.00', $this->item->getTotalPrice($this->calculator, true)); } - public function testTotalPriceSetFixedWithUseTax() { + public function testTotalPriceSetFixedWithUseTax() + { $this->item->setTotalPrice(40.99); $this->assertSame('40.99', $this->item->getTotalPrice($this->calculator, true)); diff --git a/tests/unit/OrderTest.php b/tests/unit/OrderTest.php index c691e71..d0f2e57 100644 --- a/tests/unit/OrderTest.php +++ b/tests/unit/OrderTest.php @@ -1,29 +1,33 @@ order = new \WebChemistry\Invoice\Data\Order('0001', null, null, - new \WebChemistry\Invoice\Data\PaymentInformation('$')); + protected function _before() + { + $this->order = new \Contributte\Invoice\Data\Order('0001', null, null, + new \Contributte\Invoice\Data\PaymentInformation('$')); $this->order->addItem('Foo', 15, 2, 0.10); $this->order->addItem('Bar', 30, 3, 0.20); - $this->calculator = new \WebChemistry\Invoice\Calculators\BcCalculator(2); + $this->calculator = new \Contributte\Invoice\Calculators\BcCalculator(2); } // tests - public function testTotalPriceWithoutTax() { + public function testTotalPriceWithoutTax() + { $this->assertSame('120.00', $this->order->getTotalPrice($this->calculator)); } - public function testTotalPriceFixed() { + public function testTotalPriceFixed() + { $items = $this->order->getItems(); $items[0]->setTotalPrice(15); $items[1]->setTotalPrice(30); @@ -31,11 +35,13 @@ public function testTotalPriceFixed() { $this->assertSame('45.00', $this->order->getTotalPrice($this->calculator)); } - public function testTotalPriceWithTax() { + public function testTotalPriceWithTax() + { $this->assertSame('141.00', $this->order->getTotalPrice($this->calculator, true)); } - public function testTotalPriceFixedWithUseTax() { + public function testTotalPriceFixedWithUseTax() + { $items = $this->order->getItems(); $items[0]->setTotalPrice(15); $items[1]->setTotalPrice(30);