You are here

public function OSMPlayer::osm_json_encode in MediaFront 6.2

Same name and namespace in other branches
  1. 6 players/osmplayer/player/OSMPlayer.php \OSMPlayer::osm_json_encode()
  2. 7 players/osmplayer/player/OSMPlayer.php \OSMPlayer::osm_json_encode()

Converts a PHP variable into its Javascript equivalent.

1 call to OSMPlayer::osm_json_encode()
OSMPlayer::getParams in players/osmplayer/player/OSMPlayer.php
Get the player parameters. This will only return the parameters that are included in the playerParams array.

File

players/osmplayer/player/OSMPlayer.php, line 390

Class

OSMPlayer
PHP wrapper class for the Open Standard Media (OSM) player.

Code

public function osm_json_encode($var) {
  switch (gettype($var)) {
    case 'boolean':
      return $var ? 'true' : 'false';

    // Lowercase necessary!
    case 'integer':
    case 'double':
      return $var;
    case 'resource':
    case 'string':
      return '"' . str_replace(array(
        "\r",
        "\n",
        "<",
        ">",
        "&",
        "\\'",
      ), array(
        '\\r',
        '\\n',
        '\\x3c',
        '\\x3e',
        '\\x26',
        "'",
      ), addslashes($var)) . '"';
    case 'array':

      // Arrays in JSON can't be associative. If the array is empty or if it
      // has sequential whole number keys starting with 0, it's not associative
      // so we can go ahead and convert it as an array.
      if (empty($var) || array_keys($var) === range(0, sizeof($var) - 1)) {
        $output = array();
        foreach ($var as $v) {
          $output[] = $this
            ->osm_json_encode($v);
        }
        return '[' . implode(',', $output) . ']';
      }

    // Otherwise, fall through to convert the array as an object.
    case 'object':
      $output = array();
      foreach ($var as $k => $v) {
        $output[] = $this
          ->osm_json_encode(strval($k)) . ': ' . $this
          ->osm_json_encode($v);
      }
      return '{' . implode(',', $output) . '}';
    default:
      return 'null';
  }
}