field::set_field_explain()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
ccs 5
cts 7
cp 0.7143
crap 3.2098
rs 10
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2016 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\model\entity;
11
12
use blitze\sitemaker\model\base_entity;
0 ignored issues
show
Bug introduced by
The type blitze\sitemaker\model\base_entity 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...
13
14
/**
15
 * @method integer get_field_id()
16
 * @method object set_content_id($content_id)
17
 * @method integer get_content_id()
18
 * @method object set_field_name($field_name)
19
 * @method integer get_field_name()
20
 * @method integer get_field_label()
21
 * @method object set_field_type($field_type)
22
 * @method string get_field_type()
23
 * @method object set_field_mod_only($mod_only)
24
 * @method boolean get_field_mod_only()
25
 * @method object set_field_required($required_field)
26
 * @method boolean get_field_required()
27
 * @method object set_field_summary_show($summary_show)
28
 * @method string get_field_summary_show()
29
 * @method object set_field_detail_show($detail_show)
30
 * @method string get_field_detail_show()
31
 * @method object set_field_summary_ldisp($summary_ldisp)
32
 * @method integer get_field_summary_ldisp()
33
 * @method object set_field_detail_ldisp($detail_ldisp)
34
 * @method integer get_field_detail_ldisp()
35
 * @method object set_exp_uid($uid)
36
 * @method string get_exp_uid()
37
 * @method object set_exp_bitfield($bitfield)
38
 * @method string get_exp_bitfield()
39
 * @method object set_exp_options($options)
40
 * @method integer get_exp_options()
41
 * @method object set_field_order($order)
42
 * @method string get_field_order()
43
 */
44
final class field extends base_entity
45
{
46
	/** @var integer */
47
	protected $field_id;
48
49
	/** @var integer */
50
	protected $content_id;
51
52
	/** @var string */
53
	protected $field_name;
54
55
	/** @var string */
56
	protected $field_label;
57
58
	/** @var string */
59
	protected $field_explain = '';
60
61
	/** @var string */
62
	protected $field_type;
63
64
	/** @var string */
65
	protected $field_props = '';
66
67
	/** @var boolean */
68
	protected $field_mod_only = false;
69
70
	/** @var boolean */
71
	protected $field_required = false;
72
73
	/** @var string */
74
	protected $field_summary_show = '';
75
76
	/** @var string */
77
	protected $field_detail_show = '';
78
79
	/** @var integer */
80
	protected $field_summary_ldisp = 1;
81
82
	/** @var integer */
83
	protected $field_detail_ldisp = 1;
84
85
	/** @var string */
86
	protected $field_exp_uid = '';
87
88
	/** @var string */
89
	protected $field_exp_bitfield = '';
90
91
	/** @var integer */
92
	protected $field_exp_options = 7;
93
94
	/** @var integer */
95
	protected $field_order = 0;
96
97
	/** @var array */
98
	protected $required_fields = array('content_id', 'field_name', 'field_label', 'field_type');
99
100
	/** @var array */
101
	protected $db_fields = array(
102
		'content_id',
103
		'field_id',
104
		'field_name',
105
		'field_label',
106
		'field_explain',
107
		'field_type',
108
		'field_props',
109
		'field_mod_only',
110
		'field_required',
111
		'field_summary_show',
112
		'field_detail_show',
113
		'field_summary_ldisp',
114
		'field_detail_ldisp',
115
		'field_exp_uid',
116
		'field_exp_bitfield',
117
		'field_exp_options',
118
		'field_order',
119
	);
120
121
	/**
122
	 * Set field ID
123
	 * @param int $field_id
124
	 * @return $this
125
	 */
126 28
	public function set_field_id($field_id)
127
	{
128 28
		if (!$this->field_id)
129 28
		{
130 28
			$this->field_id = (int) $field_id;
131 28
		}
132 28
		return $this;
133
	}
134
135
	/**
136
	 * Set field label
137
	 * @param string $label
138
	 * @return $this
139
	 */
140 28
	public function set_field_label($label)
141
	{
142 28
		$this->field_label = utf8_ucfirst(trim($label));
143 28
		return $this;
144
	}
145
146
	/**
147
	 * @param string $explain
148
	 * @param string $mode
149
	 * @return $this
150
	 */
151 27
	public function set_field_explain($explain, $mode = '')
152
	{
153 27
		$this->field_explain = $explain;
154
155 27
		if ($this->field_explain && $mode === 'storage')
156 27
		{
157
			generate_text_for_storage($this->field_explain, $this->field_exp_uid, $this->field_exp_bitfield, $this->field_exp_options, true, true, true);
158
		}
159 27
		return $this;
160
	}
161
162
	/**
163
	 * @param string $mode
164
	 * @return string|array
165
	 */
166 24
	public function get_field_explain($mode = 'display')
167
	{
168 24
		if ($mode === 'edit')
169 24
		{
170 1
			$data = generate_text_for_edit($this->field_explain, $this->field_exp_uid, $this->field_exp_options);
171 1
			return $data['text'];
172
		}
173
		else
174
		{
175 24
			$parse_flags = ($this->field_exp_bitfield ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
176 24
			return generate_text_for_display($this->field_explain, $this->field_exp_uid, $this->field_exp_bitfield, $parse_flags);
177
		}
178
	}
179
180
	/**
181
	 * Set field properties
182
	 * @param array|string $props
183
	 * @return $this
184
	 */
185 27
	public function set_field_props($props)
186
	{
187 27
		if (!is_array($props))
188 27
		{
189 26
			$this->field_props = $props;
190 26
		}
191 3
		else if (sizeof($props))
192 3
		{
193 3
			$this->field_props = json_encode($props);
194 3
		}
195 27
		return $this;
196
	}
197
198
	/**
199
	 * Get field settings
200
	 * @return array
201
	 */
202 24
	public function get_field_props()
203
	{
204 24
		$field_props = ($this->field_props) ? json_decode($this->field_props, true) : array();
205
206 24
		if (in_array($this->field_type, array('radio', 'checkbox', 'select')))
207 24
		{
208 19
			$options = (array) $field_props['options'];
209 19
			$field_props['options'] = array_filter(array_combine($options, $options), 'strlen');
0 ignored issues
show
Bug introduced by
It seems like array_combine($options, $options) 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

209
			$field_props['options'] = array_filter(/** @scrutinizer ignore-type */ array_combine($options, $options), 'strlen');
Loading history...
210
		}
211 24
212
		return $field_props;
213
	}
214
215
	/**
216
	* {@inheritdoc}
217
	*/
218
	public function to_db()
219
	{
220
		$db_data = parent::to_db();
221
222
		// we do this for postgresql since passing a field_id of null will cause a not-null constraint violation
223
		if (!$db_data['field_id'])
224
		{
225
			unset($db_data['field_id']);
226
		}
227
228
		return $db_data;
229
	}
230
}
231