Skip to content

Commit

Permalink
add calculators, refactoring [fixed #7][BC break]
Browse files Browse the repository at this point in the history
  • Loading branch information
MartkCz committed Feb 27, 2019
1 parent d852fba commit 0cd19ab
Show file tree
Hide file tree
Showing 39 changed files with 564 additions and 423 deletions.
33 changes: 8 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# PHP Invoice

Average output ~20ms

## Installation

php 7.1
Expand All @@ -15,31 +17,31 @@ composer require webchemistry/invoice
### Company

```php
$company = WebChemistry\Invoice\InvoiceFactory::createCompany('John Doe', 'Los Angeles', 'Cavetown', '720 55', 'USA', '0123456789', 'CZ0123456789');
$company = new WebChemistry\Invoice\Data\Company('John Doe', 'Los Angeles', 'Cavetown', '720 55', 'USA', '0123456789', 'CZ0123456789');
```

### Customer

```php
$customer = WebChemistry\Invoice\InvoiceFactory::createCustomer('John Doe', 'Los Angeles', 'Cavetown', '720 55', 'USA');
$customer = new WebChemistry\Invoice\Data\Customer('John Doe', 'Los Angeles', 'Cavetown', '720 55', 'USA');
```

### Account

```php
$account = WebChemistry\Invoice\InvoiceFactory::createAccount('1111', 'CZ4808000000002353462015', 'GIGACZPX');
$account = new WebChemistry\Invoice\Data\Account('1111', 'CZ4808000000002353462015', 'GIGACZPX');
```

### Payment info

```php
$payment = WebChemistry\Invoice\InvoiceFactory::createPaymentInformation('Kč', '0123456789', '1234', 0.21);
$payment = new WebChemistry\Invoice\Data\PaymentInformation('Kč', '0123456789', '1234', 0.21);
```

### Order

```php
$order = WebChemistry\Invoice\InvoiceFactory::createOrder('20160001', new \DateTime('+ 14 days'), $account, $payment);
$order = new WebChemistry\Invoice\Data\Order('20160001', new \DateTime('+ 14 days'), $account, $payment);
```

Adding items
Expand All @@ -53,32 +55,13 @@ $order->addItem('Logitech G700s Rechargeable Gaming Mouse', 4, 1790);
```php
class CustomFormatter implements IFormatter {

/**
* @param float $float
* @return string
*/
public function formatNumber($float) {}

/**
* @param float $float
* @param string $currency
* @return string
*/
public function formatMoney($float, $currency) {}

/**
* @param \DateTime $date
* @return string
*/
public function formatDate(\DateTime $date) {}

}
```

## Generating invoices

```php
$invoice = new \WebChemistry\Invoice\Invoice($company);
$invoice = new WebChemistry\Invoice\Invoice($company);

header('Content-Type: application/pdf');
echo $invoice->create($customer, $order);
Expand Down
10 changes: 10 additions & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
extensions:
enabled:
- Codeception\Extension\RunFailed
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
"invoice"
],
"require": {
"php": ">=7.1",
"nette/utils": "^2.3",
"php": ">=7.2",
"nette/utils": "^3.0",
"setasign/fpdf": "^1.8"
},
"require-dev": {
"nette/di": "^2.3",
"phpstan/phpstan": "dev-master"
"nette/di": "^3.0",
"codeception/codeception": "^2.4"
},
"autoload": {
"psr-4": {
"WebChemistry\\Invoice\\": "src/"
}
},
"minimum-stability": "dev",
"minimum-stability": "RC",
"prefer-stable": true
}
62 changes: 62 additions & 0 deletions src/Calculators/BcCalculator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php declare(strict_types = 1);

namespace WebChemistry\Invoice\Calculators;

class BcCalculator implements ICalculator {

/** @var int */
private $scale;

public function __construct(int $scale = 0) {
if (!function_exists('bcadd')) {
throw new \RuntimeException('BC math is not installed.');
}
$this->scale = $scale;
}

/**
* @param string|int|float $op1
* @param string|int|float $op2
* @return 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 {
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 {
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) {
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 {
return bccomp((string) $op1, (string) $op2, $this->scale);
}

}
52 changes: 52 additions & 0 deletions src/Calculators/FloatCalculator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types = 1);

namespace WebChemistry\Invoice\Calculators;

class FloatCalculator implements ICalculator {

/**
* @param string|int|float $op1
* @param string|int|float $op2
* @return float|int
*/
public function add($op1, $op2) {
return $op1 + $op2;
}

/**
* @param string|int|float $op1
* @param string|int|float $op2
* @return float|int
*/
public function mul($op1, $op2) {
return $op1 * $op2;
}

/**
* @param string|int|float $op1
* @param string|int|float $op2
* @return float|int
*/
public function div($op1, $op2) {
return $op1 / $op2;
}

/**
* @param string|int|float $op1
* @param string|int|float $op2
* @return float|int
*/
public function sub($op1, $op2) {
return $op1 - $op2;
}

/**
* @param string|int|float $op1
* @param string|int|float $op2
* @return int
*/
public function comp($op1, $op2): int {
return $op1 <=> $op2;
}

}
42 changes: 42 additions & 0 deletions src/Calculators/ICalculator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

namespace WebChemistry\Invoice\Calculators;

interface ICalculator {

/**
* @param string|int|float $op1
* @param string|int|float $op2
* @return mixed
*/
public function add($op1, $op2);

/**
* @param string|int|float $op1
* @param string|int|float $op2
* @return mixed
*/
public function mul($op1, $op2);

/**
* @param string|int|float $op1
* @param string|int|float $op2
* @return mixed
*/
public function div($op1, $op2);

/**
* @param string|int|float $op1
* @param string|int|float $op2
* @return mixed
*/
public function sub($op1, $op2);

/**
* @param string|int|float $op1
* @param string|int|float $op2
* @return int
*/
public function comp($op1, $op2): int;

}
21 changes: 4 additions & 17 deletions src/Components/Paginator.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types=1);
declare(strict_types = 1);

namespace WebChemistry\Invoice\Components;

Expand All @@ -25,17 +25,13 @@ class Paginator {

/**
* @param Item[] $items
* @param int $itemsPerPage
*/
public function __construct(array $items, int $itemsPerPage) {
$this->items = $items;
$this->totalPages = (int) ceil(count($this->items) / $itemsPerPage);
$this->totalPages = (int)ceil(count($this->items) / $itemsPerPage);
$this->itemsPerPage = $itemsPerPage;
}

/**
* @return int
*/
public function getTotalPages(): int {
return $this->totalPages;
}
Expand All @@ -53,30 +49,21 @@ public function isFirstPage(): bool {
return $this->currentPage === 1;
}

/**
* @return bool
*/
public function isLastPage(): bool {
return $this->currentPage >= $this->getTotalPages();
}

/**
* @return int
*/
public function getCurrentPage(): int {
return $this->currentPage;
}

/**
* @return bool
*/
public function nextPage(): bool {
if ($this->isLastPage()) {
return FALSE;
return false;
}
$this->currentPage++;

return TRUE;
return true;
}

}
13 changes: 2 additions & 11 deletions src/DI/InvoiceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
namespace WebChemistry\Invoice\DI;

use Nette\DI\CompilerExtension;
use WebChemistry\Invoice\Components\IPaginatorFactory;
use WebChemistry\Invoice\Components\PaginatorFactory;
use WebChemistry\Invoice\Data\Company;
use WebChemistry\Invoice\Data\Template;
use WebChemistry\Invoice\Formatter;
use WebChemistry\Invoice\IFormatter;
use WebChemistry\Invoice\Invoice;
use WebChemistry\Invoice\InvoiceFactory;
use WebChemistry\Invoice\ITranslator;
use WebChemistry\Invoice\Renderers\IRenderer;
use WebChemistry\Invoice\Renderers\PDFRenderer;
Expand All @@ -22,7 +18,7 @@

class InvoiceExtension extends CompilerExtension {

/** @var array */
/** @var array[] */
public $defaults = [
'company' => [
'name' => null,
Expand All @@ -35,10 +31,9 @@ class InvoiceExtension extends CompilerExtension {
'hasTax' => false,
],
'lang' => 'en',
'invoiceFactory' => false,
];

public function loadConfiguration() {
public function loadConfiguration(): void {
$builder = $this->getContainerBuilder();
$config = $this->validateConfig($this->defaults);

Expand All @@ -64,10 +59,6 @@ public function loadConfiguration() {
$builder->addDefinition($this->prefix('invoice'))
->setFactory(Invoice::class);

if ($config['invoiceFactory']) {
$builder->addDefinition($this->prefix('invoiceFactory'))
->setFactory(InvoiceFactory::class);
}
}

}
Loading

0 comments on commit 0cd19ab

Please sign in to comment.