Passed
Push — develop ( 77e6e2...97cb86 )
by Daniel
03:55
created

base::get_validation_rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
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 */
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 145
	public function __construct(\phpbb\language\language $language, \phpbb\request\request_interface $request, \blitze\sitemaker\services\template $ptemplate)
31
	{
32 145
		$this->language = $language;
33 145
		$this->request = $request;
34 145
		$this->ptemplate = $ptemplate;
35 145
	}
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, array $topic_data, $view_mode)
49
	{
50 8
		return $data['field_value'];
51
	}
52
53
	/**
54
	 * @inheritdoc
55
	 */
56 19
	public function get_field_value(array $data)
57
	{
58 19
		return $this->request->variable($data['field_name'], $data['field_value'], true);
59
	}
60
61
	/**
62
	 * @inheritdoc
63
	 */
64 27
	public function show_form_field($name, array &$data)
65
	{
66 27
		$data['field_name'] = $name;
67 27
		$data['field_value'] = $this->get_field_value($data);
68
69 27
		$this->ptemplate->assign_vars($data);
70
71 27
		$field = $this->get_name();
72 27
		return $this->ptemplate->render_view('blitze/content', "fields/$field.html", $field . '_field');
73
	}
74
75
	/**
76
	 * @inheritdoc
77
	 */
78
	public function save_field($value, array $field_data, array $topic_data)
79
	{
80
		// we do nothing here as field data is stored in phpbb post
81
		// for fields that store their own data, this would be used to persist data to a database
82
	}
83
84
	/**
85
	 * @inheritdoc
86 16
	 */
87
	public function get_langname()
88 16
	{
89
		return strtoupper('FORM_FIELD_' . $this->get_name());
90
	}
91
92
	/**
93
	 * @inheritdoc
94
	 */
95
	public function validate_field(array $data)
96
	{
97
		$rules = $this->get_validation_rules($data);
98
99
		$message = '';
100
		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

100
		if ($rules['filter'] && filter_var($data['field_value'], /** @scrutinizer ignore-type */ $rules['filter'], $rules['options']) === false)
Loading history...
101
		{
102
			$message = $this->get_error_message($data);
103
		}
104
		return $message;
105
	}
106
107
	/**
108
	 * @inheritdoc
109
	 */
110
	public function get_validation_rules(array $data)
111
	{
112
		return array(
113
			'filter'	=> '',
114
			'options'	=> array(),
115
		);
116
	}
117
118
	/**
119
	 * @inheritdoc
120
	 */
121
	public function get_error_message(array $data)
122
	{
123
		return $this->language->lang('FIELD_INVALID', $data['field_label']);
124
	}
125
}
126