Issues (565)

form/input/AdminPageFramework_Input_Base.php (1 issue)

1
<?php
2
/**
3
 * Admin Page Framework
4
 * 
5
 * http://admin-page-framework.michaeluno.jp/
6
 * Copyright (c) 2013-2022, Michael Uno; Licensed MIT
7
 * 
8
 */
9
10
/**
11
 * The base class of form input classes that return outputs of input form elements.
12
 * 
13
 * @package     AdminPageFramework/Common/Form/Input
14
 * @since       3.4.0
15
 * @extends     AdminPageFramework_FrameworkUtility
16
 * @internal
17
 */
18
abstract class AdminPageFramework_Input_Base extends AdminPageFramework_FrameworkUtility {
19
    
20
    /**
21
     * Stores the field definition array.
22
     * 
23
     * @since       3.4.0
24
     * @deprecated  3.5.3
25
     */
26
    public $aField = array();
27
28
    /**
29
     * Stores the attribute array.
30
     * 
31
     * @since       3.5.3
32
     */
33
    public $aAttributes = array();
34
    
35
    /**
36
     * Stores the options of how the input elements should be constructed.
37
     * 
38
     * @since       3.4.0
39
     */
40
    public $aOptions = array();
41
    
42
    /**
43
     * Represents the structure of the options array.
44
     * 
45
     * @since       3.4.0
46
     */
47
    public $aStructureOptions = array(
48
        'input_container_tag'          => 'span',
49
        'input_container_attributes'    => array(
50
            'class' => 'admin-page-framework-input-container',
51
        ),
52
        'label_container_tag'          => 'span',
53
        'label_container_attributes'    => array(
54
            'class' => 'admin-page-framework-input-label-string',
55
        ),         
56
    );
57
    
58
    /**
59
     * Sets up properties.
60
     * 
61
     * @since       3.4.0
62
     * @since       3.5.3       
63
     * @param       array       $aAttributes    The attribute array. A field definition array is deprecated.
64
     * @param       array       $aOptions       options that allows the user to set custom container tags and class selectors.
65
     */
66
    public function __construct( array $aAttributes, array $aOptions=array() ) {
67
68
        $this->aAttributes  = $this->getElementAsArray( 
69
            $aAttributes, 
70
            'attributes', 
71
            $aAttributes    // if the above key is not set, this will be set
72
        );
73
74
        $this->aOptions     = $aOptions + $this->aStructureOptions;
75
        
76
        // @deprecated 3.5.3+ use $aAttributes.
77
        $this->aField       = $aAttributes;  
0 ignored issues
show
Deprecated Code introduced by
The property AdminPageFramework_Input_Base::$aField has been deprecated: 3.5.3 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

77
        /** @scrutinizer ignore-deprecated */ $this->aField       = $aAttributes;  

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
78
        
79
        // User Constructor
80
        $this->construct();
81
        
82
    }
83
    
84
    /**
85
     * A user construct.
86
     * 
87
     * Rather than messing with __construct() simply use this method to implement additional tasks.
88
     * 
89
     * @since       3.5.3
90
     * @return      void
91
     */
92
    protected function construct() {}
93
        
94
    /**
95
     * Returns the output of the input element.
96
     * 
97
     * @remark       This method should be overridden in each extended class.
98
     * @since        3.4.0     
99
     */
100
    public function get() {}
101
 
102
    /**
103
     * Returns the set attribute by name. 
104
     * 
105
     * If a parameter is not passed, it returns the entire attribute array.
106
     * 
107
     * @since       3.5.3
108
     * @return      string|array|null        The specified attribute value or the entire attribute array if not specified. 
109
     * If not set, null will be returned as the `getAttributes()` method will not list an attribute with the null value.
110
     * @param       string      $sName      The attribute name.
111
     * @param       string      $sDefault   The defaqult value if the value is not set.
112
     */
113
    public function getAttribute( /* $sName=null, $sDefault=null */ ) {
114
        $_aParams = func_get_args() + array(
115
            0 => null,
116
            1 => null,
117
        );
118
        return isset( $_aParams[ 0 ] )
119
            ? $this->getElement( $this->aAttributes, $_aParams[ 0 ], $_aParams[ 1 ] )
120
            : $this->aAttributes;
121
    }
122
 
123
    /**
124
     * Adds class selector to the input class attribute.
125
     * 
126
     * @since       3.5.3
127
     * @return      string      The set class selector(s).
128
     */
129
    public function addClass( /* $asSelectors1, $asSelectors2 */ ) {
130
        foreach( func_get_args() as $_asSelectors ) {            
131
            $this->aAttributes['class'] = $this->getClassAttribute( 
132
                $this->aAttributes['class'],
133
                $_asSelectors
134
            );
135
        }
136
        return $this->aAttributes['class'];
137
    }
138
    
139
    /**
140
     * Sets an attribute to the attribute property.
141
     * 
142
     * <h4>Example</h4>
143
     * <code>
144
     * $oInput->setAttribute( 'data-default', '2' );
145
     *      // will be same as $this->aAttributes['data-default'] = 2;
146
     * $oInput->setAttribute( array( 'select', 'multiple' ), 'multiple' );
147
     *      // will be same as $this->aAttributes['select']['multiple'] = 'multiple';
148
     * </code>
149
     * @since       3.5.3
150
     * @return      void
151
     */
152
    public function setAttribute( /* $asAttributeName, $mValue */ ) {
153
        $_aParams = func_get_args() + array(
154
            0 => null,
155
            1 => null,
156
        );
157
        
158
        // $this->aAttributes[ $_aParams[ 0 ] ] = $_aParams[ 1 ];
159
        $this->setMultiDimensionalArray(
160
            $this->aAttributes, 
161
            $this->getElementAsArray( $_aParams, 0 ),   // $asAttributeName
162
            $_aParams[ 1 ]  // $mValue
163
        );
164
    }
165
    
166
    /**
167
     * Updates the attributes by the given array key.
168
     * 
169
     * Use this method to generate an attribute array for multiple input items.
170
     * 
171
     * @since       3.5.3
172
     * @param       strign $sKey
173
     * @return      void
174
     */
175
    public function setAttributesByKey( $sKey ) {
176
        $this->aAttributes = call_user_func_array( array( $this, 'getAttributesByKey' ), array( $sKey ) );
177
    }
178
    
179
    /**
180
     * Generates an attribute array from the given key based on the attributes set in the constructor.
181
     * 
182
     * @return      array       The updated attribute array. 
183
     * @since       3.5.3
184
     */
185
    public function getAttributesByKey( /* $sKey */ ) {
186
        return array();
187
    }
188
        
189
        /**
190
         * Calculates and returns the attributes as an array.
191
         * 
192
         * @since           3.4.0
193
         * @deprecated      3.5.3       Use `getAttributesByKey()`
194
         */
195
        public function getAttributeArray( /* $sKey */ ) {
196
            $_aParams = func_get_args();
197
            return call_user_func_array( array( $this, 'getAttributesByKey' ), $_aParams );
198
        }    
199
200
}
201