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

choice::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 choice extends base
14
{
15
	/** @var string */
16
	protected $template_name;
17
18
	/**
19
	 * @inheritdoc
20
	 */
21
	public function get_default_props()
22
	{
23
		return array(
24
			'defaults'		=> array(),
25
			'options'		=> array(),
26
			'multi_select'	=> false,
27
		);
28
	}
29
30
	/**
31
	 * @inheritdoc
32
	 */
33
	public function display_field(array $data, array $topic_data, $display_mode, $view_mode)
34
	{
35
		$value = $this->ensure_is_array($data['field_value']);
36
		return sizeof($value) ? join($this->language->lang('COMMA_SEPARATOR'), $value) : '';
37
	}
38
39
	/**
40
	 * @inheritdoc
41
	 * @return array
42
	 */
43
	public function get_submitted_value(array $data, $form_is_submitted = false)
44
	{
45
		$default = $this->get_default_value($data);
46
47
		if ($form_is_submitted)
48
		{
49
			return $this->request->variable($data['field_name'], $default, true);
50
		}
51
52
		return $default;
53
	}
54
55
	/**
56
	 * @inheritdoc
57
	 */
58
	public function show_form_field(array &$data)
59
	{
60
		$this->set_field_options($data);
61
		$this->template_name = ($data['field_type'] === 'select') ? 'select' : 'pickem';
62
		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...
63
	}
64
65
	/**
66
	 * @inheritdoc
67
	 */
68
	public function get_field_template()
69
	{
70
		return '@blitze_content/fields/' . $this->template_name . '.html';
71
	}
72
73
	/**
74
	 * @param array $data
75
	 * @return mixed
76
	 */
77
	protected function get_default_value(array $data)
78
	{
79
		$value = $this->ensure_is_array($data['field_value']);
80
		$default = $value ?: $data['field_props']['defaults'] ?? array(0 => '');
81
		return ($data['field_props']['multi_select']) ? $default : array_shift($default);
82
	}
83
84
	/**
85
	 * @param array $data
86
	 * @return void
87
	 */
88
	protected function set_field_options(array &$data)
89
	{
90
		if ($data['field_type'] === 'radio' || $data['field_type'] === 'checkbox')
91
		{
92
			$data['field_id'] .= '-0';
93
		}
94
95
		$count = 0;
96
		$options = array();
97
		if (is_array($data['field_props']['options']))
98
		{
99
			$choices = (array) $data['field_props']['options'];
100
			foreach ($choices as $value => $option)
101
			{
102
				$options[] = array(
103
					'id'		=> 'smc-' . $data['field_name'] . '-' . $count,
104
					'label'		=> $this->language->lang($option),
105
					'selected'	=> (int) (in_array($value, (array) $data['field_value'])),
106
					'value'		=> $value,
107
				);
108
				$count++;
109
			}
110
		}
111
		$data['field_props']['options'] = $options;
112
	}
113
}
114