Prado\Web\Javascripts\TJavaScript::encode PHP Метод

encode() публичный статический Метод

Example: $options['onLoading'] = "doit"; $options['onComplete'] = "more"; echo TJavaScript::encode($options); expects the following javascript code {'onLoading':'doit','onComplete':'more'} For higher complexity data structures use {@link jsonEncode} and {@link jsonDecode} to serialize and unserialize.
С версии: 3.1.5
public static encode ( $value, $toMap = true, $encodeEmptyStrings = false ) : string
Результат string the encoded string
    public static function encode($value, $toMap = true, $encodeEmptyStrings = false)
    {
        if (is_string($value)) {
            return self::quoteString($value);
        } else {
            if (is_bool($value)) {
                return $value ? 'true' : 'false';
            } else {
                if (is_array($value)) {
                    $results = '';
                    if (($n = count($value)) > 0 && array_keys($value) !== range(0, $n - 1)) {
                        foreach ($value as $k => $v) {
                            if ($v !== '' || $encodeEmptyStrings) {
                                if ($results !== '') {
                                    $results .= ',';
                                }
                                $results .= "'{$k}':" . self::encode($v, $toMap, $encodeEmptyStrings);
                            }
                        }
                        return '{' . $results . '}';
                    } else {
                        foreach ($value as $v) {
                            if ($v !== '' || $encodeEmptyStrings) {
                                if ($results !== '') {
                                    $results .= ',';
                                }
                                $results .= self::encode($v, $toMap, $encodeEmptyStrings);
                            }
                        }
                        return '[' . $results . ']';
                    }
                } else {
                    if (is_integer($value)) {
                        return "{$value}";
                    } else {
                        if (is_float($value)) {
                            switch ($value) {
                                case -INF:
                                    return 'Number.NEGATIVE_INFINITY';
                                    break;
                                case INF:
                                    return 'Number.POSITIVE_INFINITY';
                                    break;
                                default:
                                    $locale = localeConv();
                                    if ($locale['decimal_point'] == '.') {
                                        return "{$value}";
                                    } else {
                                        return str_replace($locale['decimal_point'], '.', "{$value}");
                                    }
                                    break;
                            }
                        } else {
                            if (is_object($value)) {
                                if ($value instanceof TJavaScriptLiteral) {
                                    return $value->toJavaScriptLiteral();
                                } else {
                                    return self::encode(get_object_vars($value), $toMap);
                                }
                            } else {
                                if ($value === null) {
                                    return 'null';
                                } else {
                                    return '';
                                }
                            }
                        }
                    }
                }
            }
        }
    }

Usage Example

Пример #1
0
 /**
  * Register a default button to panel. When the $panel is in focus and
  * the 'enter' key is pressed, the $button will be clicked.
  * @param TControl|string panel (or its unique ID) to register the default button action
  * @param TControl|string button (or its unique ID) to trigger a postback
  */
 public function registerDefaultButton($panel, $button)
 {
     $panelID = is_string($panel) ? $panel : $panel->getUniqueID();
     if (is_string($button)) {
         $buttonID = $button;
     } else {
         $button->setIsDefaultButton(true);
         $buttonID = $button->getUniqueID();
     }
     $options = TJavaScript::encode($this->getDefaultButtonOptions($panelID, $buttonID));
     $code = "new Prado.WebUI.DefaultButton({$options});";
     $this->_endScripts['prado:' . $panelID] = $code;
     $this->registerPradoScriptInternal('prado');
     $params = array($panelID, $buttonID);
     $this->_page->registerCachingAction('Page.ClientScript', 'registerDefaultButton', $params);
 }
All Usage Examples Of Prado\Web\Javascripts\TJavaScript::encode