-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathInvoiceExtension.php
More file actions
executable file
·93 lines (78 loc) · 2.14 KB
/
Copy pathInvoiceExtension.php
File metadata and controls
executable file
·93 lines (78 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php declare(strict_types = 1);
namespace Contributte\Invoice\Bridge\Nette\DI;
use Contributte\Invoice\Data\Account;
use Contributte\Invoice\Data\Company;
use Contributte\Invoice\Provider\InvoiceAccountsProvider;
use Contributte\Invoice\Provider\InvoiceCompanyProvider;
use Nette\DI\CompilerExtension;
use Nette\DI\Definitions\Statement;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use stdClass;
final class InvoiceExtension extends CompilerExtension
{
public function getConfigSchema(): Schema
{
return Expect::structure([
'company' => Expect::structure([
'name' => Expect::string()->required(),
'town' => Expect::string()->required(),
'address' => Expect::string()->required(),
'zip' => Expect::string()->required(),
'country' => Expect::string()->required(),
'vatNumber' => Expect::string(),
'id' => Expect::string(),
]),
'accounts' => Expect::arrayOf(Expect::structure([
'iban' => Expect::string(),
])),
]);
}
public function loadConfiguration(): void
{
$builder = $this->getContainerBuilder();
$company = $this->getCompany();
if ($company !== null) {
$builder->addDefinition($this->prefix('companyProvider'))
->setFactory(InvoiceCompanyProvider::class, [$company]);
}
$accounts = $this->getAccounts();
if ($accounts !== []) {
$builder->addDefinition($this->prefix('accountsProvider'))
->setFactory(InvoiceAccountsProvider::class, [$accounts]);
}
}
private function getCompany(): ?Statement
{
/** @var stdClass $config */
$config = $this->getConfig();
/** @var stdClass $company */
$company = $config->company;
if (!$company->name) {
return null;
}
return new Statement(Company::class, [
$company->name,
$company->town,
$company->address,
$company->zip,
$company->country,
$company->vatNumber,
$company->id,
]);
}
/**
* @return Statement[]
*/
private function getAccounts(): array
{
/** @var stdClass $config */
$config = $this->getConfig();
$accounts = [];
/** @var stdClass $account */
foreach ($config->accounts as $account) {
$accounts[] = new Statement(Account::class, [$account->iban]);
}
return $accounts;
}
}