1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @package sitemaker |
5
|
|
|
* @copyright (c) 2013 Daniel A. (blitze) |
6
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace blitze\content\services\form\field; |
11
|
|
|
|
12
|
|
|
abstract class base implements field_interface |
13
|
|
|
{ |
14
|
|
|
/** @var \phpbb\language\language */ |
15
|
|
|
protected $language; |
16
|
|
|
|
17
|
|
|
/** @var \phpbb\request\request_interface */ |
18
|
|
|
protected $request; |
19
|
|
|
|
20
|
|
|
/** @var \blitze\sitemaker\services\template */ |
21
|
|
|
protected $ptemplate; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor |
25
|
|
|
* |
26
|
|
|
* @param \phpbb\language\language $language Language object |
27
|
|
|
* @param \phpbb\request\request_interface $request Request object |
28
|
|
|
* @param \blitze\sitemaker\services\template $ptemplate Sitemaker template object |
29
|
|
|
*/ |
30
|
64 |
|
public function __construct(\phpbb\language\language $language, \phpbb\request\request_interface $request, \blitze\sitemaker\services\template $ptemplate) |
31
|
|
|
{ |
32
|
64 |
|
$this->language = $language; |
33
|
64 |
|
$this->request = $request; |
34
|
64 |
|
$this->ptemplate = $ptemplate; |
35
|
64 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritdoc |
39
|
|
|
*/ |
40
|
4 |
|
public function get_default_props() |
41
|
|
|
{ |
42
|
4 |
|
return array(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
8 |
|
public function display_field(array $data) |
49
|
|
|
{ |
50
|
8 |
|
return $data['field_value']; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
11 |
|
public function get_field_value($name, $value, $display = 'form') |
57
|
|
|
{ |
58
|
11 |
|
return $this->request->variable($name, $value, true); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
17 |
|
public function show_form_field($name, array &$data) |
65
|
|
|
{ |
66
|
17 |
|
$data['field_name'] = $name; |
67
|
17 |
|
$data['field_value'] = $this->get_field_value($name, $data['field_value']); |
68
|
|
|
|
69
|
17 |
|
$this->ptemplate->assign_vars($data); |
70
|
|
|
|
71
|
17 |
|
$field = $this->get_name(); |
72
|
17 |
|
return $this->ptemplate->render_view('blitze/content', "fields/$field.twig", $field . '_field'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
|
|
public function save_field($field, $value) |
79
|
|
|
{ |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
*/ |
86
|
9 |
|
public function get_langname() |
87
|
|
|
{ |
88
|
9 |
|
return strtoupper('FORM_FIELD_' . $this->get_name()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
|
|
public function validate_field(array $data) |
95
|
|
|
{ |
96
|
|
|
$options = $this->get_filter_options($data); |
97
|
|
|
|
98
|
|
|
$message = ''; |
99
|
|
|
if (isset($data['validation_filter']) && !filter_var($data['field_value'], $data['validation_filter'], $options)) |
100
|
|
|
{ |
101
|
|
|
$message = $this->get_error_message($data); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $message; |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param array $data |
109
|
|
|
* @return array|false |
110
|
|
|
*/ |
111
|
|
|
protected function get_filter_options(array &$data) |
112
|
|
|
{ |
113
|
|
|
if (isset($data['field_minlength'])) |
114
|
|
|
{ |
115
|
|
|
$data['validation_options'] += array('min_range' => $data['field_minlength']); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (isset($data['field_maxlength'])) |
119
|
|
|
{ |
120
|
|
|
$data['validation_options'] += array('max_range' => $data['field_maxlength']); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return (isset($data['validation_options'])) ? array('options' => $data['validation_options']) : false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param array $data |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
protected function get_error_message(array $data) |
131
|
|
|
{ |
132
|
|
|
$length = utf8_strlen($data['field_value']); |
133
|
|
|
|
134
|
|
|
if ($this->is_too_short($data, $length)) |
135
|
|
|
{ |
136
|
|
|
return $this->language->lang('FIELD_TOO_SHORT', $data['field_label'], $data['field_minlength']); |
137
|
|
|
} |
138
|
|
|
else if ($this->is_too_long($data, $length)) |
139
|
|
|
{ |
140
|
|
|
return $this->language->lang('FIELD_TOO_LONG', $data['field_label'], $data['field_maxlength']); |
141
|
|
|
} |
142
|
|
|
else |
143
|
|
|
{ |
144
|
|
|
return $this->language->lang('FIELD_INVALID', $data['field_label']); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param array $data |
150
|
|
|
* @param $length |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
protected function is_too_short(array $data, $length) |
154
|
|
|
{ |
155
|
|
|
return (isset($data['field_minlength']) && $length < $data['field_minlength']) ? true : false; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param array $data |
160
|
|
|
* @param $length |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
|
|
protected function is_too_long(array $data, $length) |
164
|
|
|
{ |
165
|
|
|
return (isset($data['field_maxlength']) && $length > $data['field_maxlength']) ? true : false; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.