Back to Select class

Method option

public static \stdClass
option
(mixed $value, mixed $text = '', mixed $optKey = 'value', mixed $optText = 'text', mixed $disable = false)
Create an object that represents an option in an option list.
Parameters
  • string $value The value of the option
  • string $text The text for the option
  • mixed $optKey If a string, the returned object property name for the value. If an array, options. Valid options are: attr: String|array. Additional attributes for this option. Defaults to none. disable: Boolean. If set, this option is disabled. label: String. The value for the option label. option.attr: The property in each option array to use for additional selection attributes. Defaults to none. option.disable: The property that will hold the disabled state. Defaults to "disable". option.key: The property that will hold the selection value. Defaults to "value". option.label: The property in each option array to use as the selection label attribute. If a "label" option is provided, defaults to "label", if no label is given, defaults to null (none). option.text: The property that will hold the the displayed text. Defaults to "text". If set to null, the option array is assumed to be a list of displayable scalars.
  • string $optText The property that will hold the the displayed text. This parameter is ignored if an options array is passed.
  • bool $disable Not used.
Returns
  • \stdClass
Since
  • 1.5
Class: Select
Project: Joomla

Method option - Source code

/**
 * Create an object that represents an option in an option list.
 *
 * @param   string   $value    The value of the option
 * @param   string   $text     The text for the option
 * @param   mixed    $optKey   If a string, the returned object property name for
 *                             the value. If an array, options. Valid options are:
 *                             attr: String|array. Additional attributes for this option.
 *                             Defaults to none.
 *                             disable: Boolean. If set, this option is disabled.
 *                             label: String. The value for the option label.
 *                             option.attr: The property in each option array to use for
 *                             additional selection attributes. Defaults to none.
 *                             option.disable: The property that will hold the disabled state.
 *                             Defaults to "disable".
 *                             option.key: The property that will hold the selection value.
 *                             Defaults to "value".
 *                             option.label: The property in each option array to use as the
 *                             selection label attribute. If a "label" option is provided, defaults to
 *                             "label", if no label is given, defaults to null (none).
 *                             option.text: The property that will hold the the displayed text.
 *                             Defaults to "text". If set to null, the option array is assumed to be a
 *                             list of displayable scalars.
 * @param   string   $optText  The property that will hold the the displayed text. This
 *                             parameter is ignored if an options array is passed.
 * @param   boolean  $disable  Not used.
 *
 * @return  \stdClass
 *
 * @since   1.5
 */
public static function option($value, $text = '', $optKey = 'value', $optText = 'text', $disable = false)
{
    $options = array('attr' => null, 'disable' => false, 'option.attr' => null, 'option.disable' => 'disable', 'option.key' => 'value', 'option.label' => null, 'option.text' => 'text');
    if (is_array($optKey)) {
        // Merge in caller's options
        $options = array_merge($options, $optKey);
    } else {
        // Get options from the parameters
        $options['option.key'] = $optKey;
        $options['option.text'] = $optText;
        $options['disable'] = $disable;
    }
    $obj = new \stdClass();
    $obj->{$options['option.key']} = $value;
    $obj->{$options['option.text']} = trim($text) ? $text : $value;
    /*
     * If a label is provided, save it. If no label is provided and there is
     * a label name, initialise to an empty string.
     */
    $hasProperty = $options['option.label'] !== null;
    if (isset($options['label'])) {
        $labelProperty = $hasProperty ? $options['option.label'] : 'label';
        $obj->{$labelProperty} = $options['label'];
    } elseif ($hasProperty) {
        $obj->{$options['option.label']} = '';
    }
    // Set attributes only if there is a property and a value
    if ($options['attr'] !== null) {
        $obj->{$options['option.attr']} = $options['attr'];
    }
    // Set disable only if it has a property and a value
    if ($options['disable'] !== null) {
        $obj->{$options['option.disable']} = $options['disable'];
    }
    return $obj;
}