Completed
Push — master ( 48290c...e43ea6 )
by Adam
07:23
created

FormFactory::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
/**
3
 * FormFactory.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:Forms!
9
 * @subpackage     common
10
 * @since          5.0
11
 *
12
 * @date           10.06.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Forms;
18
19
use Nette;
20
use Nette\Application;
21
use Nette\DI;
22
use Nette\Localization;
23
24
/**
25
 * Form factory
26
 *
27
 * @package        iPublikuj:Forms!
28
 * @subpackage     common
29
 */
30
final class FormFactory implements IFormFactory
31
{
32
	/**
33
	 * Define class name
34
	 */
35
	const CLASS_NAME = __CLASS__;
36
37
	/**
38
	 * @var Localization\ITranslator
39
	 */
40
	private $translator;
41
42
	/**
43
	 * @var DI\Container
44
	 */
45
	private $container;
46
47
	/**
48
	 * @param Localization\ITranslator|NULL $translator
49
	 * @param DI\Container $container
50
	 */
51
	public function __construct(
52
		Localization\ITranslator $translator = NULL,
53
		DI\Container $container
54
	) {
55
		$this->translator = $translator;
56
		$this->container = $container;
57
	}
58
59
	/**
60
	 * {@inheritdoc}
61
	 */
62
	public function create(string $formClassName, ...$args)
63
	{
64
		if (!class_exists($formClassName)) {
65
			throw new Exceptions\InvalidArgumentException('Factory form class isn\'t defined.');
66
		}
67
68
		/** @var Application\UI\Form $form */
69
		$form = $this->container->createInstance($formClassName, $args);
70
71
		if ($this->translator instanceof Localization\ITranslator) {
72
			$form->setTranslator($this->translator);
73
		}
74
75
		return $form;
76
	}
77
}
78