Completed
Branch FET-8385-datetime-ticket-selec... (a857e7)
by
unknown
21:36 queued 11:32
created

EE_Select_Display_Strategy::display()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 38
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 28
nc 9
nop 0
dl 0
loc 38
rs 5.3846
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * Class EE_Select_Display_Strategy
5
 *
6
 * displays either simple arrays as selected, or if a 2d array is provided, separates them into optgroups
7
 *
8
 * @package 			Event Espresso
9
 * @subpackage 	core
10
 * @author 				Mike Nelson
11
 * @since 				$VID:$
12
 *
13
 */
14
class EE_Select_Display_Strategy extends EE_Display_Strategy_Base{
15
16
	/**
17
	 *
18
	 * @throws EE_Error
19
	 * @return string of html to display the field
20
	 */
21
	function display(){
22
		if( ! $this->_input instanceof EE_Form_Input_With_Options_Base){
23
			throw new EE_Error( sprintf( __( 'Cannot use Select Display Strategy with an input that doesn\'t have options', 'event_espresso' )));
24
		}
25
26
		$html = EEH_HTML::nl( 0, 'select' );
27
		$html .= '<select';
28
		$html .= ' id="' . $this->_input->html_id() . '"';
29
		$html .= ' name="' . $this->_input->html_name() . '"';
30
		$class = $this->_input->required() ? $this->_input->required_css_class() . ' ' . $this->_input->html_class() : $this->_input->html_class();
31
		$html .= ' class="' . $class . '"';
32
		// add html5 required
33
		$html .= $this->_input->required() ? ' required' : '';
34
		$html .= ' style="' . $this->_input->html_style() . '"';
35
		$html .= ' ' . $this->_input->other_html_attributes();
36
		$html .= '>';
37
38
		if ( EEH_Array::is_multi_dimensional_array( $this->_input->options() )) {
39
			EEH_HTML::indent( 1, 'optgroup' );
40
			foreach( $this->_input->options() as $opt_group_label => $opt_group ){
41
			    if ( ! empty($opt_group_label)) {
42
                    $html .= EEH_HTML::nl(0, 'optgroup') . '<optgroup label="' . esc_attr($opt_group_label) . '">';
43
                }
44
				EEH_HTML::indent( 1, 'option' );
45
				$html .= $this->_display_options( $opt_group );
46
				EEH_HTML::indent( -1, 'option' );
47
                if ( ! empty($opt_group_label)) {
48
                    $html .= EEH_HTML::nl( 0, 'optgroup' ) . '</optgroup>';
49
			    }
50
			}
51
			EEH_HTML::indent( -1, 'optgroup' );
52
		} else {
53
			$html.=$this->_display_options( $this->_input->options() );
54
		}
55
56
		$html.= EEH_HTML::nl( 0, 'select' ) . '</select>';
57
		return $html;
58
	}
59
60
61
62
	/**
63
	 * Displays a flat list of options as option tags
64
	 * @param array $options
65
	 * @return string
66
	 */
67
	protected function _display_options($options){
68
		$html = '';
69
		EEH_HTML::indent( 1, 'option' );
70
		foreach( $options as $value => $display_text ){
71
			$unnormalized_value = $this->_input->get_normalization_strategy()->unnormalize_one( $value );
72
			$selected = $this->_check_if_option_selected( $unnormalized_value ) ? ' selected="selected"' : '';
0 ignored issues
show
Bug introduced by
It seems like $unnormalized_value defined by $this->_input->get_norma...unnormalize_one($value) on line 71 can also be of type array; however, EE_Select_Display_Strate...ck_if_option_selected() does only seem to accept string|integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
73
			$html.= EEH_HTML::nl( 0, 'option' ) . '<option value="' . esc_attr( $unnormalized_value ) . '"' . $selected . '>' . $display_text . '</option>';
74
		}
75
		EEH_HTML::indent( -1, 'option' );
76
		return $html;
77
	}
78
79
80
81
	/**
82
	 * Checks if that value is the one selected
83
	 * @param string|int $value unnormalized value option (string)
84
	 * @return string
85
	 */
86
	protected function _check_if_option_selected( $value ){
87
		return $this->_input->raw_value() == $value ? TRUE : FALSE;
88
	}
89
90
91
92
}