Skip to content

Commit

Permalink
Cleaning code, independent on nette/utils
Browse files Browse the repository at this point in the history
  • Loading branch information
MartkCz committed Aug 26, 2016
1 parent fe19567 commit db06ce0
Show file tree
Hide file tree
Showing 22 changed files with 777 additions and 749 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"description": "Library for easily and quickly creating invoices for customers.",
"tags": ["webchemistry", "invoice"],
"require": {
"nette/utils": "^2.3",
"intervention/image": "^2.3"
},
"require-dev": {
Expand All @@ -13,4 +12,4 @@
"autoload": {
"classmap": ["src/"]
}
}
}
34 changes: 34 additions & 0 deletions src/Components/IPaginator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace WebChemistry\Invoice\Components;

use WebChemistry\Invoice\Data\Item;

interface IPaginator {

/**
* @return int
*/
public function getTotalPages();

/**
* @return Item[]
*/
public function getItems();

/**
* @return bool
*/
public function isLastPage();

/**
* @return int
*/
public function getCurrentPage();

/**
* @return bool
*/
public function nextPage();

}
15 changes: 15 additions & 0 deletions src/Components/IPaginatorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace WebChemistry\Invoice\Components;

use WebChemistry\Invoice\Data\Item;

interface IPaginatorFactory {

/**
* @param Item[] $items
* @return IPaginator
*/
public function createPaginator(array $items);

}
37 changes: 18 additions & 19 deletions src/Components/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@

use WebChemistry\Invoice\Data\Item;

class Paginator {
class Paginator implements IPaginator {

/** @var int */
public static $maxItems = 9;
const ITEMS_PER_PAGE = 9;

/** @var Item[] */
private $items;

/** @var int */
protected $currentPage;
protected $currentPage = 0;

/** @var int */
protected $totalPages;

/**
* @param array $items
* @return self
* @param Item[] $items
*/
public function setItems(array $items) {
public function __construct(array $items) {
$this->items = $items;

return $this;
$this->totalPages = (int) ceil(count($this->items) / self::ITEMS_PER_PAGE);
}

/**
* @return int
*/
public function getTotalPages() {
return (int) ceil(count($this->items) / 9);
return $this->totalPages;
}

/**
Expand All @@ -38,14 +38,14 @@ public function getTotalPages() {
public function getItems() {
$page = $this->currentPage - 1;

return array_slice($this->items, $page * 9, $page * 9 + 9);
return array_slice($this->items, $page * self::ITEMS_PER_PAGE, $page * self::ITEMS_PER_PAGE + self::ITEMS_PER_PAGE);
}

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

/**
Expand All @@ -55,14 +55,13 @@ public function getCurrentPage() {
return $this->currentPage;
}

/**
* @param int $currentPage
* @return Paginator
*/
public function setCurrentPage($currentPage) {
$this->currentPage = (int) $currentPage;
public function nextPage() {
if ($this->isLastPage()) {
return FALSE;
}
$this->currentPage++;

return $this;
return TRUE;
}

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

namespace WebChemistry\Invoice\Components;

use WebChemistry\Invoice\Data\Item;

class PaginatorFactory implements IPaginatorFactory {

/**
* @param Item[] $items
* @return Paginator
*/
public function createPaginator(array $items) {
return new Paginator($items);
}

}
38 changes: 24 additions & 14 deletions src/DI/InvoiceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
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\Invoice;
use WebChemistry\Invoice\ITranslator;
use WebChemistry\Invoice\Translator;

class InvoiceExtension extends CompilerExtension {

Expand All @@ -13,7 +20,9 @@ class InvoiceExtension extends CompilerExtension {
'town' => NULL,
'address' => NULL,
'zip' => NULL,
'country' => NULL
'country' => NULL,
'tin' => NULL,
'vaTin' => NULL,
],
'lang' => 'en'
];
Expand All @@ -22,24 +31,25 @@ public function loadConfiguration() {
$builder = $this->getContainerBuilder();
$config = $this->validateConfig($this->defaults);

$company = $config['company'];
$builder->addDefinition($this->prefix('company'))
->setClass(Company::class, array_values($config['company']));

$cmp = $builder->addDefinition($this->prefix('company'))
->setClass('WebChemistry\Invoice\Data\Company', [
$company['name'], $company['town'], $company['address'], $company['zip'], $company['country']
]);
unset($company['name'], $company['town'], $company['address'], $company['zip'], $company['country']);
$builder->addDefinition($this->prefix('template'))
->setClass(Template::class);

foreach ($company as $name => $value) {
$cmp->addSetup('set' . ucfirst($name), [$value]);
}
$builder->addDefinition($this->prefix('paginatorFactory'))
->setClass(IPaginatorFactory::class)
->setFactory(PaginatorFactory::class);

$builder->addDefinition($this->prefix('template'))
->setClass('WebChemistry\Invoice\Data\Template');
$builder->addDefinition($this->prefix('translation'))
->setClass(ITranslator::class)
->setFactory(Translator::class, [$config['lang']]);

$builder->addDefinition($this->prefix('invoice'))
->setClass('WebChemistry\Invoice\Invoice', [$this->prefix('@company'), $this->prefix('@template')])
->addSetup('?->getTranslator()->setLang(?)', ['@self', $config['lang']]);
->setClass(Invoice::class, [
$this->prefix('@company'), $this->prefix('@template'), $this->prefix('@translator'),
$this->prefix('@paginatorFactory'),
]);
}

}
169 changes: 0 additions & 169 deletions src/Data/AbstractData.php

This file was deleted.

Loading

0 comments on commit db06ce0

Please sign in to comment.