/**
  * Encode a value and return it's javascript representation.
  *
  * @param Chain    $chain The chain.
  * @param mixed    $value The generated javascript.
  * @param int|null $flags Force custom json encode flags.
  *
  * @return string
  */
 public static function routeEncodeValue(Chain $chain, $value, $flags = null)
 {
     if (static::isScalar($value)) {
         // If we got a scalar value, just encode it.
         return $chain->first('encodeScalar', [$value, $flags]);
     } elseif (is_array($value)) {
         return $chain->first('encodeArray', [$value, $flags]);
     }
     return $chain->first('encodeObject', [$value, $flags]);
 }