Completed
Branch dev (16db98)
by
unknown
05:13
created

AdminPageFramework_Input_checkbox   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
B get() 0 26 1
A _getInputElements() 0 15 2
B getAttributesByKey() 0 27 2
A _getCheckedAttributeValue() 0 10 3
1
<?php
2
/**
3
 * Admin Page Framework
4
 * 
5
 * http://en.michaeluno.jp/admin-page-framework/
6
 * Copyright (c) 2013-2016 Michael Uno; Licensed MIT
7
 * 
8
 */
9
10
/**
11
 * Provides methods to output form input element of check boxes.
12
 * 
13
 * @package         AdminPageFramework
14
 * @subpackage      Common/Form/Input
15
 * @since           3.4.0       
16
 * @internal
17
 */
18
class AdminPageFramework_Input_checkbox extends AdminPageFramework_Input_Base {
19
    
20
    /**
21
     * @since       3.8.8
22
     */
23
    public $aOptions = array(
24
        'save_unchecked'    => true,
25
    );
26
    
27
    /**
28
     * Returns the output of the input element.
29
     * 
30
     * @since       3.4.0    
31
     * @param       string      $sLabel         The label text.
0 ignored issues
show
Bug introduced by
There is no parameter named $sLabel. 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...
32
     * @param       array       $aAttributes    (optional) The attribute array. If set, it will be merged with the attribute set in the constructor.
0 ignored issues
show
Bug introduced by
There is no parameter named $aAttributes. 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...
33
     */    
34
    public function get( /* $sLabel, $aAttributes=array() */ ) {
35
        
36
        // Parameters
37
        $_aParams       = func_get_args() + array( 
38
            0 => '',            // 1st parameter
39
            1 => array()        // 2nd parameter
40
        );
41
        $_sLabel        = $_aParams[ 0 ];       // first parameter
42
43
        // Attributes
44
        $_aAttributes   = $this->uniteArrays(   // second parameter
45
            $this->getElementAsArray( $_aParams, 1, array() ),
46
            $this->aAttributes
47
        );
48
49
        // Output
50
        return 
51
           "<{$this->aOptions[ 'input_container_tag' ]} " . $this->getAttributes( $this->aOptions[ 'input_container_attributes' ] ) . ">"
52
                . $this->_getInputElements( $_aAttributes, $this->aOptions )
53
            . "</{$this->aOptions[ 'input_container_tag' ]}>"
54
            . "<{$this->aOptions[ 'label_container_tag' ]} " . $this->getAttributes( $this->aOptions[ 'label_container_attributes' ] ) . ">"
55
                . $_sLabel
56
            . "</{$this->aOptions[ 'label_container_tag' ]}>"
57
        ;
58
                        
59
    }        
60
        /**
61
         * @since       3.8.8
62
         * @internal   
63
         * @return      string
64
         */
65
        private function _getInputElements( $aAttributes, $aOptions ) {
0 ignored issues
show
Unused Code introduced by
The parameter $aOptions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
            $_sOutput = $this->aOptions[ 'save_unchecked' ]
67
                // the unchecked value must be set prior to the checkbox input field.
68
                ? "<input " . $this->getAttributes( 
69
                    array(
70
                        'type'      => 'hidden',
71
                        'class'     => $aAttributes[ 'class' ],
72
                        'name'      => $aAttributes[ 'name' ],
73
                        'value'     => '0',
74
                    ) 
75
                ) . " />"
76
                : '';
77
            $_sOutput .= "<input " . $this->getAttributes( $aAttributes ) . " />";
78
            return $_sOutput;
79
        }
80
        
81
    /**
82
     * Generates an attribute array from the given key based on the attributes set in the constructor.
83
     * 
84
     * @return      array       The updated attribute array. 
85
     * @since       3.5.3
86
     */
87
    public function getAttributesByKey( /* $sKey */ ) {
88
        
89
        // Parameters
90
        $_aParams       = func_get_args() + array( 0 => '', );
91
        $_sKey          = $_aParams[ 0 ];        
92
        $_bIsMultiple   = '' !== $_sKey;
93
        
94
        // Result
95
        return 
96
            // Allows the user set attributes to override the system set attributes.
97
            $this->getElement( $this->aAttributes, $_sKey, array() )
98
            
99
            // The type needs to be specified since the postytpe field type extends this class. If not set, the 'posttype' will be passed to the type attribute.
100
            + array(
101
                'type'      => 'checkbox', 
102
                'id'        => $this->aAttributes[ 'id' ] . '_' . $_sKey,
103
                'checked'   => $this->_getCheckedAttributeValue( $_sKey ),
104
                'value'     => 1,   // this must be always 1 because the key value can be zero. In that case, the value always will be false and unchecked.
105
                'name'      => $_bIsMultiple 
106
                    ? "{$this->aAttributes[ 'name' ]}[{$_sKey}]" 
107
                    : $this->aAttributes[ 'name' ],
108
                'data-id'   => $this->aAttributes[ 'id' ],       // referenced by the JavaScript scripts such as the revealer script.
109
            )
110
            + $this->aAttributes
111
            ;
112
            
113
    }
114
        /**
115
         * @since       3.7.0
116
         * @return      string|null        If checked, `checked`; otherwize, `null`
117
         */
118
        private function _getCheckedAttributeValue( $_sKey ) {
119
            
120
            $_aValueDimension = '' === $_sKey
121
                ? array( 'value' )
122
                : array( 'value', $_sKey );
123
            return $this->getElement( $this->aAttributes, $_aValueDimension )
124
                ? 'checked' 
125
                : null;    // to not to set, pass null. An empty value '' will still set the attribute.            
126
        
127
        }
128
        
129
}
130