diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..16c2c0a --- /dev/null +++ b/.travis.yml @@ -0,0 +1,22 @@ +anguage: php + +os: + - linux + +php: + - 7.0 + - 7.1 + +matrix: + include: + - php: 7.0 + env: PHPSTAN=1 + - php: 7.1 + env: PHPSTAN=1 + +install: + - composer self-update + - composer install + +script: + - if ["$PHPSTAN" = "1"]; then vendor/bin/phpstan analyse src --level=7 --ansi --no-progress; fi diff --git a/README.md b/README.md index ba0fb86..41af695 100755 --- a/README.md +++ b/README.md @@ -1,11 +1,21 @@ +[![Build Status](https://travis-ci.org/WebChemistry/invoice.svg?branch=master)](https://travis-ci.org/WebChemistry/invoice) + # PHP Invoice ## Installation +php 7.0 + ``` composer require webchemistry/invoice ``` +php 5.5 + +``` +composer require webchemistry/invoice:^1.0 +``` + ## Usage ### Company diff --git a/composer.json b/composer.json index 2a50ce6..ad4de83 100755 --- a/composer.json +++ b/composer.json @@ -7,12 +7,13 @@ "invoice" ], "require": { - "php": ">=5.5", + "php": ">=7.0", "intervention/image": "^2.3", "nette/utils": "^2.3" }, "require-dev": { - "nette/di": "^2.3" + "nette/di": "^2.3", + "phpstan/phpstan": "^0.7.0" }, "autoload": { "psr-4": { diff --git a/src/Components/IPaginator.php b/src/Components/IPaginator.php index e55a216..3a3d65e 100755 --- a/src/Components/IPaginator.php +++ b/src/Components/IPaginator.php @@ -1,5 +1,7 @@ totalPages; } /** * @return Item[] */ - public function getItems() { + public function getItems(): array { $page = $this->currentPage - 1; return array_slice($this->items, $page * self::ITEMS_PER_PAGE, $page * self::ITEMS_PER_PAGE + self::ITEMS_PER_PAGE); @@ -44,21 +46,21 @@ public function getItems() { /** * @return bool */ - public function isLastPage() { + public function isLastPage(): bool { return $this->currentPage >= $this->getTotalPages(); } /** * @return int */ - public function getCurrentPage() { + public function getCurrentPage(): int { return $this->currentPage; } /** * @return bool */ - public function hasNextPage() { + public function hasNextPage(): bool { if ($this->isLastPage()) { return FALSE; } diff --git a/src/Components/PaginatorFactory.php b/src/Components/PaginatorFactory.php index 31c22ca..c5a92ab 100755 --- a/src/Components/PaginatorFactory.php +++ b/src/Components/PaginatorFactory.php @@ -1,5 +1,7 @@ accountNumber = $accountNumber; $this->iBan = $iBan; $this->swift = $swift; - - $this->validate(); - } - - /** - * Validates properties - * - * @throws InvoiceException - */ - private function validate() { - if (!$this->accountNumber || !is_string($this->accountNumber)) { - throw InvoiceException::wrongType('non-empty string', $this->accountNumber); - } - if ($this->iBan !== NULL && !$this->iBan || !is_string($this->iBan)) { - throw InvoiceException::wrongType('non-empty string or null', $this->iBan); - } - if ($this->swift !== NULL && !$this->swift || !is_string($this->swift)) { - throw InvoiceException::wrongType('non-empty string or null', $this->iBan); - } } ///////////////////////////////////////////////////////////////// @@ -50,21 +31,21 @@ private function validate() { /** * @return string */ - public function getAccountNumber() { + public function getAccountNumber(): string { return $this->accountNumber; } /** * @return null|string */ - public function getIBan() { + public function getIBan(): ?string { return $this->iBan; } /** * @return null|string */ - public function getSwift() { + public function getSwift(): ?string { return $this->swift; } diff --git a/src/Data/Company.php b/src/Data/Company.php index a9d9140..f688004 100755 --- a/src/Data/Company.php +++ b/src/Data/Company.php @@ -1,5 +1,7 @@ hasTax = (bool) $hasTax; + $this->hasTax = $hasTax; } /** * @return bool */ - public function hasTax() { + public function hasTax(): bool { return $this->hasTax; } diff --git a/src/Data/Customer.php b/src/Data/Customer.php index 995561f..fdfe50a 100755 --- a/src/Data/Customer.php +++ b/src/Data/Customer.php @@ -1,5 +1,7 @@ name = $name; $this->count = $count; $this->price = $price; @@ -35,9 +37,6 @@ public function __construct($name, $count, $price) { * @throws InvoiceException */ private function validate() { - if (!$this->name || !is_string($this->name)) { - throw InvoiceException::wrongType('non-empty string', $this->name); - } if (!is_numeric($this->count)) { throw InvoiceException::wrongType('numeric', $this->count); } @@ -51,19 +50,19 @@ private function validate() { /** * @return string */ - public function getName() { + public function getName(): string { return $this->name; } /** - * @return int + * @return int|float */ public function getCount() { return $this->count; } /** - * @return int + * @return int|float */ public function getPrice() { return $this->price; diff --git a/src/Data/Order.php b/src/Data/Order.php index 3c20be8..523c505 100755 --- a/src/Data/Order.php +++ b/src/Data/Order.php @@ -1,5 +1,7 @@ items[] = new Item($name, $price, $count); } @@ -75,35 +77,35 @@ public function getNumber() { /** * @return \DateTime */ - public function getDueDate() { + public function getDueDate(): \DateTime { return $this->dueDate; } /** * @return Account */ - public function getAccount() { + public function getAccount(): Account { return $this->account; } /** * @return PaymentInformation */ - public function getPayment() { + public function getPayment(): PaymentInformation { return $this->payment; } /** * @return \DateTime */ - public function getCreated() { + public function getCreated(): \DateTime { return $this->created; } /** * @return Item[] */ - public function getItems() { + public function getItems(): array { return $this->items; } diff --git a/src/Data/PaymentInformation.php b/src/Data/PaymentInformation.php index 3c74b48..beb6c38 100755 --- a/src/Data/PaymentInformation.php +++ b/src/Data/PaymentInformation.php @@ -1,18 +1,18 @@ currency = $currency; $this->variableSymbol = $variableSymbol; $this->constantSymbol = $constantSymbol; $this->tax = $tax; - - $this->validate(); - } - - /** - * @throws InvoiceException - */ - private function validate() { - if (!$this->currency || !is_string($this->currency)) { - throw InvoiceException::wrongType('non-empty string', $this->currency); - } - if ($this->tax && !is_float($this->tax)) { - throw InvoiceException::wrongType('float', $this->tax); - } } - ///////////////////////////////////////////////////////////////// - /** * @return string */ - public function getCurrency() { + public function getCurrency(): string { return $this->currency; } /** - * @return string + * @return string|null */ - public function getVariableSymbol() { + public function getVariableSymbol(): ?string { return $this->variableSymbol; } /** - * @return string + * @return string|null */ - public function getConstantSymbol() { + public function getConstantSymbol(): ?string { return $this->constantSymbol; } /** * @return float|null */ - public function getTax() { + public function getTax(): ?float { return $this->tax; } diff --git a/src/Data/Subject.php b/src/Data/Subject.php index 141f30d..501cfda 100755 --- a/src/Data/Subject.php +++ b/src/Data/Subject.php @@ -1,8 +1,8 @@ name = $name; $this->town = $town; $this->address = $address; @@ -45,71 +44,54 @@ public function __construct($name, $town, $address, $zip, $country, $tin = NULL, $this->country = $country; $this->tin = $tin; $this->vaTin = $vaTin; - - $this->validate(); - } - - /** - * Validates properties - * - * @throws InvoiceException - */ - private function validate() { - foreach (['name', 'town', 'address', 'zip', 'country'] as $val) { - if (!$this->$val || !is_string($this->$val)) { - throw InvoiceException::wrongType('non-empty string', $this->$val); - } - } } - ///////////////////////////////////////////////////////////////// - /** * @return string */ - public function getName() { + public function getName(): string { return $this->name; } /** * @return string */ - public function getTown() { + public function getTown(): string { return $this->town; } /** * @return string */ - public function getAddress() { + public function getAddress(): string { return $this->address; } /** * @return string */ - public function getZip() { + public function getZip(): string { return $this->zip; } /** * @return string */ - public function getCountry() { + public function getCountry(): string { return $this->country; } /** - * @return string + * @return string|null */ - public function getTin() { + public function getTin(): ?string { return $this->tin; } /** - * @return string + * @return string|null */ - public function getVaTin() { + public function getVaTin(): ?string { return $this->vaTin; } diff --git a/src/Data/Template.php b/src/Data/Template.php index 6690f7e..d6b3822 100755 --- a/src/Data/Template.php +++ b/src/Data/Template.php @@ -1,5 +1,7 @@ font; } @@ -54,7 +56,7 @@ public function getFont() { * @throws InvoiceException * @return self */ - public function setFont($font) { + public function setFont(string $font): self { if (!file_exists($font) || !is_file($font)) { throw new InvoiceException("File '$font' not exists."); } @@ -66,7 +68,7 @@ public function setFont($font) { /** * @return string */ - public function getFontBold() { + public function getFontBold(): string { return $this->fontBold; } @@ -75,7 +77,7 @@ public function getFontBold() { * @throws InvoiceException * @return self */ - public function setFontBold($fontBold) { + public function setFontBold(string $fontBold): self { if (!file_exists($fontBold) || !is_file($fontBold)) { throw new InvoiceException("File '$fontBold' not exists."); } @@ -87,7 +89,7 @@ public function setFontBold($fontBold) { /** * @return string */ - public function getIconFont() { + public function getIconFont(): string { return $this->iconFont; } @@ -96,7 +98,7 @@ public function getIconFont() { * @throws InvoiceException * @return self */ - public function setIconFont($iconFont) { + public function setIconFont(string $iconFont): self { if (!file_exists($iconFont) || !is_file($iconFont)) { throw new InvoiceException("File '$iconFont' not exists."); } @@ -108,16 +110,16 @@ public function setIconFont($iconFont) { /** * @return array */ - public function getBaseColor() { + public function getBaseColor(): array { return $this->baseColor; } /** * @param array $baseColor - * @throws + * @throws InvoiceException * @return self */ - public function setBaseColor(array $baseColor) { + public function setBaseColor(array $baseColor): self { if (count($baseColor) !== 3) { throw new InvoiceException('Color must have 3 items.'); } @@ -129,7 +131,7 @@ public function setBaseColor(array $baseColor) { /** * @return array */ - public function getPrimaryColor() { + public function getPrimaryColor(): array { return $this->primaryColor; } @@ -138,7 +140,7 @@ public function getPrimaryColor() { * @throws InvoiceException * @return self */ - public function setPrimaryColor($primaryColor) { + public function setPrimaryColor(array $primaryColor): self { if (count($primaryColor) !== 3) { throw new InvoiceException('Color must have 3 items.'); } @@ -150,7 +152,7 @@ public function setPrimaryColor($primaryColor) { /** * @return array */ - public function getFontColor() { + public function getFontColor(): array { return $this->fontColor; } @@ -159,7 +161,7 @@ public function getFontColor() { * @throws InvoiceException * @return self */ - public function setFontColor($fontColor) { + public function setFontColor(array $fontColor): self { if (count($fontColor) !== 3) { throw new InvoiceException('Color must have 3 items.'); } @@ -171,7 +173,7 @@ public function setFontColor($fontColor) { /** * @return array */ - public function getColorOdd() { + public function getColorOdd(): array { return $this->colorOdd; } @@ -180,7 +182,7 @@ public function getColorOdd() { * @throws InvoiceException * @return self */ - public function setColorOdd($colorOdd) { + public function setColorOdd(array $colorOdd): self { if (count($colorOdd) !== 3) { throw new InvoiceException('Color must have 3 items.'); } @@ -192,7 +194,7 @@ public function setColorOdd($colorOdd) { /** * @return array */ - public function getColorEven() { + public function getColorEven(): array { return $this->colorEven; } @@ -201,7 +203,7 @@ public function getColorEven() { * @throws InvoiceException * @return self */ - public function setColorEven($colorEven) { + public function setColorEven(array $colorEven): self { if (count($colorEven) !== 3) { throw new InvoiceException('Color must have 3 items.'); } @@ -211,30 +213,30 @@ public function setColorEven($colorEven) { } /** - * @param string $footer + * @param string|null $footer */ - public function setFooter($footer) { + public function setFooter(?string $footer = NULL) { $this->footer = $footer; } /** - * @return string + * @return string|null */ - public function getFooter() { + public function getFooter(): ?string { return $this->footer; } /** - * @param string $logo + * @param string|null $logo */ - public function setLogo($logo) { + public function setLogo(?string $logo = NULL) { $this->logo = $logo; } /** * @return string */ - public function getLogo() { + public function getLogo(): ?string { return $this->logo; } diff --git a/src/Formatter.php b/src/Formatter.php index 9e63d8c..f8c53d5 100644 --- a/src/Formatter.php +++ b/src/Formatter.php @@ -1,5 +1,7 @@ lang = $lang; } - public function formatNumber($float) { + public function formatNumber($float): string { return number_format($float, 2, self::$options[$this->lang]['number']['dec'], self::$options[$this->lang]['number']['sep']); } - public function formatMoney($float, $currency) { + public function formatMoney($float, string $currency): string { return strtr(self::$options[$this->lang]['money'], ['%money' => $this->formatNumber($float), '%currency' => $currency]); } - public function formatDate(\DateTime $date) { + 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 031dc4b..5874736 100644 --- a/src/IFormatter.php +++ b/src/IFormatter.php @@ -10,19 +10,19 @@ interface IFormatter { * @param float $float * @return string */ - public function formatNumber($float); + public function formatNumber($float): string; /** * @param float $float * @param string $currency * @return string */ - public function formatMoney($float, $currency); + public function formatMoney($float, string $currency): string; /** * @param \DateTime $date * @return string */ - public function formatDate(\DateTime $date); + public function formatDate(\DateTime $date): string; } diff --git a/src/ITranslator.php b/src/ITranslator.php index 4993fe0..31dfd7a 100755 --- a/src/ITranslator.php +++ b/src/ITranslator.php @@ -1,5 +1,7 @@ translator->translate($message); } @@ -71,7 +73,7 @@ protected function translate($message) { /** * @return string Encoded invoice */ - public function generatePreview() { + public function generatePreview(): string { $factory = new InvoiceFactory(); $tax = $this->company->hasTax() ? 0.21 : NULL; @@ -89,7 +91,7 @@ public function generatePreview() { $images = $this->create($customer, $order); - return $images[0]->encode(); + return (string) $images[0]->encode(); } /** @@ -98,7 +100,7 @@ public function generatePreview() { * @throws InvoiceException * @return Image[] */ - public function create(Customer $customer, Order $order) { + public function create(Customer $customer, Order $order): array { $this->customer = $customer; $this->order = $order; $paginator = $this->paginatorFactory->createPaginator($order->getItems()); @@ -133,7 +135,6 @@ public function create(Customer $customer, Order $order) { $pages[] = $this->image; } - $this->image = NULL; return $pages; } @@ -543,7 +544,7 @@ protected function item($multiplier, Item $item) { $font->color($this->template->getFontColor()); }); - $this->image->text($this->formatter->formatNumber($item->getCount(), 0), 1255, 1350 + $plus, function (Font $font) { + $this->image->text($this->formatter->formatNumber($item->getCount()), 1255, 1350 + $plus, function (Font $font) { $font->size(27); $font->file($this->template->getFont()); $font->valign('center'); diff --git a/src/InvoiceException.php b/src/InvoiceException.php index 43642e5..adec629 100755 --- a/src/InvoiceException.php +++ b/src/InvoiceException.php @@ -4,7 +4,7 @@ class InvoiceException extends \Exception { - public static function wrongType($need, $given) { + public static function wrongType($need, $given): self { $given = is_object($given) ? get_class($given) : gettype($given); return new self(sprintf('%s expected, %s given.', $need, $given)); diff --git a/src/InvoiceFactory.php b/src/InvoiceFactory.php index 9d2ffde..703405f 100755 --- a/src/InvoiceFactory.php +++ b/src/InvoiceFactory.php @@ -1,5 +1,7 @@ lang = $lang; if (!isset(self::$translations[$this->lang])) { throw new InvoiceException("Language $lang not exists."); @@ -81,7 +81,7 @@ public function __construct($lang = self::ENGLISH) { * @param string $message * @return string */ - public function translate($message) { + public function translate(string $message): string { return self::$translations[$this->lang][$message]; }