Passed
Push — release-3.0.0 ( 4d9055...2010c1 )
by Daniel
07:48
created

base::get_field_template()   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 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 * @package sitemaker
6
 * @copyright (c) 2013 Daniel A. (blitze)
7
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 *
9
 */
10
11
namespace blitze\content\services\form\field;
12
13
abstract class base implements field_interface
14
{
15
	/** @var \phpbb\language\language */
16
	protected $language;
17
18
	/** @var \phpbb\request\request_interface */
19
	protected $request;
20
21
	/** @var \phpbb\template\template */
22
	protected $template;
23
24
	/**
25
	 * Constructor
26
	 *
27
	 * @param \phpbb\language\language                  $language       Language object
28
	 * @param \phpbb\request\request_interface			$request		Request object
29
	 * @param \phpbb\template\template					$template		Template object
30
	 */
31
	public function __construct(\phpbb\language\language $language, \phpbb\request\request_interface $request, \phpbb\template\template $template)
32
	{
33
		$this->language = $language;
34
		$this->request = $request;
35
		$this->template = $template;
36
	}
37
38
	/**
39
	 * @inheritdoc
40
	 */
41
	public function get_default_props()
42
	{
43
		return array();
44
	}
45
46
	/**
47
	 * @inheritdoc
48
	 */
49
	public function get_field_value(array $data)
50
	{
51
		return $data['field_value'] ?: '';
52
	}
53
54
	/**
55
	 * @inheritdoc
56
	 */
57
	public function display_field(array $data, array $topic_data, $display_mode, $view_mode)
58
	{
59
		return $data['field_value'];
60
	}
61
62
	/**
63
	 * @inheritdoc
64
	 * @return string
65
	 */
66
	public function get_submitted_value(array $data)
67
	{
68
		return $this->request->variable($data['field_name'], (string) $data['field_value'], true);
69
	}
70
71
	/**
72
	 * @inheritdoc
73
	 */
74
	public function get_field_template()
75
	{
76
		return '@blitze_content/fields/' . $this->get_name() . '.html';
77
	}
78
79
	/**
80
	 * @inheritdoc
81
	 */
82
	public function show_form_field(array &$data)
83
	{
84
		return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the return type mandated by blitze\content\services\...face::show_form_field() of string.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
85
	}
86
87
	/**
88
	 * @inheritdoc
89
	 */
90
	public function save_field(array $field_data, array $topic_data)
91
	{
92
		// we do nothing here as field data is stored in phpbb post
93
		// for fields that store their own data, this would be used to persist data to a database
94
	}
95
96
	/**
97
	 * @inheritdoc
98
	 */
99
	public function get_langname()
100
	{
101
		return strtoupper('FORM_FIELD_' . $this->get_name());
102
	}
103
104
	/**
105
	 * @inheritdoc
106
	 */
107
	public function validate_field(array $data)
108
	{
109
		$rules = $this->get_validation_rules($data);
110
111
		if (isset($rules['sanitize']))
112
		{
113
			$data['field_value'] = filter_var($data['field_value'], (int) $rules['sanitize']);
114
		}
115
116
		$message = '';
117
		if ($rules['filter'] && filter_var($data['field_value'], (int) $rules['filter'], $rules['options']) === false)
118
		{
119
			$message = $this->get_error_message($data);
120
		}
121
		return $message;
122
	}
123
124
	/**
125
	 * @inheritdoc
126
	 */
127
	public function get_validation_rules(array $data)
128
	{
129
		return array(
130
			'filter'	=> '',
131
			'options'	=> array(),
132
		);
133
	}
134
135
	/**
136
	 * @inheritdoc
137
	 */
138
	public function get_error_message(array $data)
139
	{
140
		return $this->language->lang('FIELD_INVALID', $data['field_label']);
141
	}
142
143
	/**
144
	 * @param mixed $value
145
	 * @return array
146
	 */
147
	protected function ensure_is_array($value)
148
	{
149
		if (!is_array($value))
150
		{
151
			return array_filter((array) preg_split("/(\n|<br>)/", $value));
152
		}
153
154
		return $value;
155
	}
156
}
157