Completed
Push — develop ( 6a0b8a...755a17 )
by Daniel
07:17
created

range::show_form_field()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 15
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 2
crap 2
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
class range extends base
13
{
14
	/** @var \blitze\sitemaker\services\util */
15
	protected $util;
16
17
	/**
18
	 * Constructor
19
	 *
20
	 * @param \phpbb\language\language                  $language       Language object
21
	 * @param \phpbb\request\request_interface			$request		Request object
22
	 * @param \blitze\sitemaker\services\template		$ptemplate		Sitemaker template object
23
	 * @param \blitze\sitemaker\services\util			$util       	Sitemaker utility object
24
	 */
25
	public function __construct(\phpbb\language\language $language, \phpbb\request\request_interface $request, \blitze\sitemaker\services\template $ptemplate, \blitze\sitemaker\services\util $util)
26
	{
27
		parent::__construct($language, $request, $ptemplate);
28
29
		$this->util = $util;
30
	}
31
32
	/**
33
	 * @inheritdoc
34
	 */
35
	public function get_name()
36
	{
37
		return 'range';
38
	}
39
40
	/**
41
	 * @inheritdoc
42
	 */
43
	public function get_default_props()
44
	{
45
		return array(
46
			'type'		=> 'double',
47
			'theme'		=> 'skinFlat',
48
			'size'		=> 100,
49
			'values'	=> '',
50
			'prefix'	=> '',
51
			'postfix'	=> '',
52
			'min'		=> '',
53
			'max'		=> '',
54
			'step'		=> 1,
55
			'grid'		=> false,
56
		);
57
	}
58
59
	/**
60
	 * @inheritdoc
61
	 */
62
	public function show_form_field($name, array &$data)
63
	{
64
		$this->util->add_assets(array(
65
			'js'   => array(
66
				'@blitze_content/vendor/ion.rangeSlider/js/ion.rangeSlider.min.js',
67
				'@blitze_content/assets/form/fields.min.js',
68
			),
69
			'css'   => array(
70
				'@blitze_content/vendor/ion.rangeSlider/css/ion.rangeSlider.min.css',
71
				'@blitze_content/vendor/ion.rangeSlider/css/ion.rangeSlider.' . $data['field_props']['theme'] . '.min.css',
72
			)
73
		));
74
75
		$this->set_range($data);
76
77
		return parent::show_form_field($name, $data);
78
	}
79
80
	/**
81
	 * @inheritdoc
82
	 */
83
	public function display_field(array $data)
84
	{
85
		$range = $this->get_range($data['field_value']);
86
87
		if (sizeof($range))
88
		{
89
			array_walk($range, array($this, 'set_prepost'), $data['field_props']);
90
		}
91
92
		return join($range, ' - ');
93
	}
94
95
	/**
96
	 * @param string $values
0 ignored issues
show
Documentation introduced by
There is no parameter named $values. Did you maybe mean $value?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
97
	 * @return array
98
	 */
99
	protected function get_range($value)
100
	{
101
		return explode(';', preg_replace('/;\s+/', ';', $value));
102
	}
103
104
	/**
105
	 * @param array $data
106
	 * @return void
107
	 */
108
	protected function set_range(array &$data)
109
	{
110
		list($from, $to) = $this->get_range($data['field_value']);
111
112
		if ($data['field_props']['values'])
113
		{
114
			$values = explode(',', preg_replace('/,\s+/', ',', $data['field_props']['values']));
115
116
			$from = array_search($from, $values);
117
			$to = array_search($to, $values);
118
		}
119
120
		$data['field_props']['from'] = $from;
121
		$data['field_props']['to'] = $to;
122
	}
123
124
	/**
125
	 * @param array $data
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
126
	 * @return array
127
	 */
128
	protected function set_prepost(&$item, $key, array $props)
129
	{
130
		$item = $props['prefix'] . $item . $props['postfix'];
131
	}
132
}
133