Passed
Push — release-3.0.0 ( e8971e...d75c10 )
by Daniel
04:23
created

base::get_submitted_value()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 */
0 ignored issues
show
Bug introduced by
The type blitze\sitemaker\services\template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
	public function __construct(\phpbb\language\language $language, \phpbb\request\request_interface $request, \blitze\sitemaker\services\template $ptemplate)
31
	{
32
		$this->language = $language;
33
		$this->request = $request;
34
		$this->ptemplate = $ptemplate;
35
	}
36
37
	/**
38
	 * @inheritdoc
39
	 */
40
	public function get_default_props()
41
	{
42
		return array();
43
	}
44
45
	/**
46
	 * @inheritdoc
47
	 */
48
	public function get_field_value(array $data)
49
	{
50
		return $data['field_value'] ?: '';
51
	}
52
53
	/**
54
	 * @inheritdoc
55
	 */
56
	public function display_field(array $data, array $topic_data, $view_mode)
57
	{
58
		return $data['field_value'];
59
	}
60
61
	/**
62
	 * @inheritdoc
63
	 * @return string
64
	 */
65
	public function get_submitted_value(array $data)
66
	{
67
		return $this->request->variable($data['field_name'], (string) $data['field_value'], true);
68
	}
69
70
	/**
71
	 * @inheritdoc
72
	 */
73
	public function show_form_field(array &$data)
74
	{
75
		$this->ptemplate->assign_vars($data);
76
77
		$field = $this->get_name();
78
		return $this->ptemplate->render_view('blitze/content', "fields/$field.html", $field . '_field');
79
	}
80
81
	/**
82
	 * @inheritdoc
83
	 */
84
	public function save_field(array $field_data, array $topic_data)
85
	{
86
		// we do nothing here as field data is stored in phpbb post
87
		// for fields that store their own data, this would be used to persist data to a database
88
	}
89
90
	/**
91
	 * @inheritdoc
92
	 */
93
	public function get_langname()
94
	{
95
		return strtoupper('FORM_FIELD_' . $this->get_name());
96
	}
97
98
	/**
99
	 * @inheritdoc
100
	 */
101
	public function validate_field(array $data)
102
	{
103
		$rules = $this->get_validation_rules($data);
104
105
		if (isset($rules['sanitize']))
106
		{
107
			$data['field_value'] = filter_var($data['field_value'], $rules['sanitize']);
0 ignored issues
show
Bug introduced by
$rules['sanitize'] of type array|string is incompatible with the type integer expected by parameter $filter of filter_var(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
			$data['field_value'] = filter_var($data['field_value'], /** @scrutinizer ignore-type */ $rules['sanitize']);
Loading history...
108
		}
109
110
		$message = '';
111
		if ($rules['filter'] && filter_var($data['field_value'], $rules['filter'], $rules['options']) === false)
0 ignored issues
show
Bug introduced by
$rules['filter'] of type string is incompatible with the type integer expected by parameter $filter of filter_var(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

111
		if ($rules['filter'] && filter_var($data['field_value'], /** @scrutinizer ignore-type */ $rules['filter'], $rules['options']) === false)
Loading history...
112
		{
113
			$message = $this->get_error_message($data);
114
		}
115
		return $message;
116
	}
117
118
	/**
119
	 * @inheritdoc
120
	 */
121
	public function get_validation_rules(array $data)
122
	{
123
		return array(
124
			'filter'	=> '',
125
			'options'	=> array(),
126
		);
127
	}
128
129
	/**
130
	 * @inheritdoc
131
	 */
132
	public function get_error_message(array $data)
133
	{
134
		return $this->language->lang('FIELD_INVALID', $data['field_label']);
135
	}
136
137
	/**
138
	 * @param mixed $value
139
	 * @return array
140
	 */
141
	protected function ensure_is_array($value)
142
	{
143
		if (!is_array($value))
144
		{
145
			return array_filter(preg_split("/(\n|<br>)/", $value));
0 ignored issues
show
Bug introduced by
It seems like preg_split('/( |<br>)/', $value) can also be of type false; however, parameter $input of array_filter() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

145
			return array_filter(/** @scrutinizer ignore-type */ preg_split("/(\n|<br>)/", $value));
Loading history...
146
		}
147
148
		return $value;
149
	}
150
}
151