Completed
Push — development ( e37bfd...5ad0f2 )
by Alexander
03:46
created

AbstractForm   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 155
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
__construct() 0 1 ?
save() 0 1 ?
A addElement() 0 4 1
A init() 0 13 2
A valid() 0 11 3
A submitted() 0 14 3
A cleanup() 0 5 1
B __toString() 0 25 2
A getSid() 0 4 1
A saveSid() 0 4 1
A getData() 0 4 2
1
<?php
2
/**
3
 * Base class for forms
4
 *
5
 * @file      AbstractForm.php
6
 *
7
 * PHP version 5.6+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2016 Alexander Yancharuk <alex at itvault at info>
11
 * @date      Вск Авг 12 10:30:52 2012
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\Form;
17
18
use Veles\Cache\Cache;
19
use Veles\Form\Elements\ButtonElement;
20
use Veles\Form\Elements\HiddenElement;
21
use Veles\Form\Elements\ElementInterface;
22
use Veles\Form\Elements\SubmitElement;
23
use Veles\Validators\RegExValidator;
24
25
/**
26
 * Class AbstractForm
27
 * @author  Alexander Yancharuk <alex at itvault dot info>
28
 */
29
abstract class AbstractForm implements FormInterface
30
{
31
	protected $method   = 'post';
32
	protected $template = null;
33
	protected $data     = null;
34
	protected $sid      = null;
35
	protected $name     = null;
36
	protected $action   = null;
37
	protected $elements = [];
38
39
	/**
40
	 * Constructor
41
	 * @param mixed $data Optional data for form generation
42
	 */
43
	abstract public function __construct($data = false);
44
45
	/**
46
	 * Form save
47
	 */
48
	abstract public function save();
49
50
	/**
51
	 * Default values initialization
52
	 */
53
	protected function init()
54
	{
55
		$input      = ('get' === $this->method) ? INPUT_GET : INPUT_POST;
56
		$this->data = filter_input_array($input, FILTER_UNSAFE_RAW);
57
		$this->sid  = md5(uniqid('', true));
58
59
		$params = [
60
			'validator'  => new RegExValidator('/^[a-f\d]{32}$/'),
61
			'required'   => true,
62
			'attributes' => ['name' => 'sid', 'value' => $this->sid]
63
		];
64
		$this->addElement(new HiddenElement($params));
65
	}
66
67
	/**
68
	 * Add form element
69
	 * @param ElementInterface $element Form element
70
	 * @return void
71
	 */
72
	public function addElement(ElementInterface $element)
73
	{
74
		$this->elements[] = $element;
75
	}
76
77
	/**
78
	 * Form validation
79
	 * @return bool
80
	 */
81
	public function valid()
82
	{
83
		/** @var ElementInterface $element*/
84
		foreach ($this->elements as $element) {
85
			if (!$element->validate($this)) {
86
				return false;
87
			}
88
		}
89
90
		return true;
91
	}
92
93
	/**
94
	 * Check is form submitted by security key presence
95
	 * @return bool
96
	 */
97
	public function submitted()
98
	{
99
		if (!isset($this->data['sid'])) {
100
			return false;
101
		}
102
103
		$key = $this->name . $this->data['sid'];
104
105
		if (!Cache::get($key)) {
106
			return false;
107
		}
108
109
		return true;
110
	}
111
112
	/**
113
	 * Security key cleanup
114
	 */
115
	public function cleanup()
116
	{
117
		$key = $this->name . $this->data['sid'];
118
		Cache::del($key);
119
	}
120
121
	/**
122
	 * Form output
123
	 * @return string
124
	 */
125
	public function __toString()
126
	{
127
		$elements = $tpl = [];
128
		$output   = file_get_contents($this->template);
129
130
		/** @var ElementInterface $element */
131
		foreach ($this->elements as $number => $element) {
132
			$elements[] = $element->render();
133
			$tpl[]      = "#$number#";
134
		}
135
136
		$tpl      = array_merge($tpl, ["#method#", "#action#", "#name#"]);
137
		$elements = array_merge(
138
			$elements,
139
			[
140
				$this->method,
141
				$this->action,
142
				$this->name
143
			]
144
		);
145
146
		$this->saveSid();
147
148
		return str_replace($tpl, $elements, $output);
149
	}
150
151
	/**
152
	 * Return form security id
153
	 *
154
	 * Can be used for refresh sid after ajax-request
155
	 *
156
	 * @return string
157
	 */
158
	public function getSid()
159
	{
160
		return $this->sid;
161
	}
162
163
	/**
164
	 * Save form security id to cache
165
	 * @return bool
166
	 */
167
	public function saveSid()
168
	{
169
		return Cache::set($this->name . $this->sid, true, 7200);
170
	}
171
172
	/**
173
	 * Get data by element name
174
	 *
175
	 * @param string $name Element name
176
	 *
177
	 * @return null|string
178
	 */
179
	public function getData($name)
180
	{
181
		return (isset($this->data[$name])) ? $this->data[$name] : null;
182
	}
183
}
184