Completed
Push — master ( e43ea6...048027 )
by Adam
03:04
created

TForm::beforeRender()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
rs 8.439
cc 5
eloc 13
nc 7
nop 0
1
<?php
2
/**
3
 * TForm.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     Forms
10
 * @since          1.0.0
11
 *
12
 * @date           26.05.13
13
 */
14
15
namespace IPub\Forms\Forms;
16
17
use Nette;
18
use Nette\Utils;
19
20
use IPub\Forms;
21
22
/**
23
 * Form trait for decorating form
24
 *
25
 * @package        iPublikuj:Forms!
26
 * @subpackage     Forms
27
 *
28
 * @author         Adam Kadlec <[email protected]>
29
 */
30
trait TForm
31
{
32
	/**
33
	 * @var mixed
34
	 */
35
	protected $id;
36
37
	/**
38
	 * @var string|NULL
39
	 */
40
	protected $errorClass = NULL;
41
42
	/**
43
	 * @param array $defaults
44
	 */
45
	public function restore(array $defaults = [])
46
	{
47
		$this->setDefaults($defaults, TRUE);
0 ignored issues
show
Bug introduced by
It seems like setDefaults() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
48
		$this->setValues($defaults, TRUE);
0 ignored issues
show
Bug introduced by
It seems like setValues() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
49
	}
50
51
	/**
52
	 * @return mixed
53
	 */
54
	public function getId()
55
	{
56
		return $this->id;
57
	}
58
59
	/**
60
	 * @param mixed $id
61
	 */
62
	public function setId($id)
63
	{
64
		$this->id = $id;
65
	}
66
67
	/**
68
	 * @param string $errorClass
69
	 *
70
	 * @return void
71
	 */
72
	public function setErrorClass(string $errorClass)
73
	{
74
		$this->errorClass = $errorClass;
75
	}
76
77
	/**
78
	 * @return void
79
	 */
80
	protected function beforeRender()
81
	{
82
		parent::beforeRender();
83
84
		/** @var Nette\Forms\Controls\BaseControl $control */
85
		foreach ($this->getControls() as $control) {
0 ignored issues
show
Bug introduced by
It seems like getControls() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
86
			$inputClass = [];
87
88
			if ($control->isRequired()) {
89
				$control->getLabelPrototype()->appendAttribute('required', 'required');
90
				$control->getLabelPrototype()->appendAttribute('class', 'ipub-field-required');
91
92
				$inputClass[] = 'ipub-field-required';
93
			}
94
95
			if ($control->hasErrors()) {
96
				$inputClass[] = 'ipub-field-error';
97
98
				if ($this->errorClass) {
99
					$inputClass[] = $this->errorClass;
100
				}
101
			}
102
103
			$control->getControlPrototype()->addAttributes(['class' => $inputClass]);
104
		}
105
	}
106
107
	/**
108
	 * @param string $name
109
	 * @param string $class
110
	 *
111
	 * @return void
112
	 */
113
	protected function addExtension(string $name, string $class)
114
	{
115
		Nette\Forms\Container::extensionMethod($name, function (Nette\Forms\Container $container, $name, $label = NULL) use ($class) {
116
			return $container[$name] = new $class($label);
117
		});
118
	}
119
}
120