06.05 diff saved
Bic2k
Tags add more
javascript, json, object and strict  
Note
Bic2k did not leave a note
  1. Index: libs/view/helpers/javascript.php
  2.  
  3. ===================================================================
  4.  
  5. --- libs/view/helpers/javascript.php    (revision 7111)
  6.  
  7. +++ libs/view/helpers/javascript.php    (working copy)
  8.  
  9. @@ -470,7 +470,7 @@
  10.  
  11.   * from an array
  12.   *
  13.   * @param array $data Data to be converted
  14. - * @param array $options Set of options: block, prefix, postfix, stringKeys, quoteKeys, q
  15. + * @param array $options Set of options: block, prefix, postfix, stringKeys, quoteKeys, q, strict
  16.   * @param string $prefix DEPRECATED, use $options['prefix'] instead. Prepends the string to the returned data
  17.   * @param string $postfix DEPRECATED, use $options['postfix'] instead. Appends the string to the returned data
  18.   * @param array $stringKeys DEPRECATED, use $options['stringKeys'] instead. A list of array keys to be treated as a string
  19. @@ -485,7 +485,7 @@
  20.  
  21.           $options = array();
  22.        }
  23.  
  24. -      $defaultOptions = array('block' => false, 'prefix' => '', 'postfix' => '', 'stringKeys' => array(), 'quoteKeys' => true, 'q' => '"');
  25. +      $defaultOptions = array('block' => false, 'prefix' => '', 'postfix' => '', 'stringKeys' => array(), 'quoteKeys' => true, 'q' => '"', 'strict' => true );
  26.        $options = array_merge($defaultOptions, $options, array_filter(compact(array_keys($defaultOptions))));
  27.  
  28.        if (is_object($data)) {
  29. @@ -507,13 +507,14 @@
  30.  
  31.           }
  32.  
  33.           foreach ($data as $key => $val) {
  34. +                //if ( $key === 'StockHistory-open_price' ) { debug($val);debug(is_numeric($val) == true ? 'true' : 'false');debug(is_float($val) == true ? 'true' : 'false');die(); }
  35.              if (is_array($val) || is_object($val)) {
  36.                 $val = $this->object($val, am($options, array('block' => false)));
  37.              } else {
  38. -               $val = $this->value($val, (!count($options['stringKeys']) || ($options['quoteKeys'] && in_array($key, $options['stringKeys'], true)) || (!$options['quoteKeys'] && !in_array($key, $options['stringKeys'], true))));
  39. +               $val = $this->value($val, (!count($options['stringKeys']) || ($options['quoteKeys'] && in_array($key, $options['stringKeys'], true)) || (!$options['quoteKeys'] && !in_array($key, $options['stringKeys'], true))),$options['strict']);
  40.              }
  41.              if (!$numeric) {
  42. -               $val = $options['q'] . $this->value($key, false) . $options['q'] . ':' . $val;
  43. +               $val = $options['q'] . $this->value($key, false,$options['strict']) . $options['q'] . ':' . $val;
  44.              }
  45.              $out[] = $val;
  46.           }
  47. @@ -537,33 +538,63 @@
  48.  
  49.   *
  50.   * @param mixed $val A PHP variable to be converted to JSON
  51.   * @param boolean $quoteStrings If false, leaves string values unquoted
  52. + * @param boolean $strict If true, uses strict typing on values for quoting/formatting
  53.   * @return string a JavaScript-safe/JSON representation of $val
  54.   */
  55. -   function value($val, $quoteStrings = true) {
  56. -      switch (true) {
  57. -         case (is_array($val) || is_object($val)):
  58. -            $val = $this->object($val);
  59. -         break;
  60. -         case ($val === null):
  61. -            $val = 'null';
  62. -         break;
  63. -         case (is_bool($val)):
  64. -            $val = ife($val, 'true', 'false');
  65. -         break;
  66. -         case (is_int($val)):
  67. -            $val = $val;
  68. -         break;
  69. -         case (is_float($val)):
  70. -            $val = sprintf("%.11f", $val);
  71. -         break;
  72. -         default:
  73. -            $val = $this->escapeString($val);
  74. -            if ($quoteStrings) {
  75. -               $val = '"' . $val . '"';
  76. -            }
  77. -         break;
  78. -      }
  79. -      return $val;
  80. +   function value($val, $quoteStrings = true, $strict = true) {
  81. +        if ($strict) {
  82. +            switch (true) {
  83. +                case (is_array($val) || is_object($val)):
  84. +                   $val = $this->object($val);
  85. +                   break;
  86. +                case ($val === null):
  87. +                    $val = 'null';
  88. +                    break;
  89. +                case (is_bool($val)):
  90. +                    $val = ife($val, 'true', 'false');
  91. +                    break;
  92. +            //case (is_numeric($val) && (integer)($val) == $val):
  93. +                case (is_int($val)):               
  94. +                    $val = $val;
  95. +                    break;
  96. +            //case (is_numeric($val) && (float)$val == $val ):
  97. +                case (is_float($val)):
  98. +                    $val = sprintf("%.11f", $val);
  99. +                    break;
  100. +                default:
  101. +                    $val = $this->escapeString($val);
  102. +                    if ($quoteStrings) {
  103. +                        $val = '"' . $val . '"';
  104. +                    }
  105. +                    break;
  106. +            }
  107. +            return $val;
  108. +        } else {
  109. +            switch (true) {
  110. +                case (is_array($val) || is_object($val)):
  111. +                   $val = $this->object($val);
  112. +                   break;
  113. +                case ($val === null):
  114. +                    $val = 'null';
  115. +                    break;
  116. +                case (is_bool($val)):
  117. +                    $val = ife($val, 'true', 'false');
  118. +                    break;
  119. +                case (is_numeric($val) && (integer)($val) == $val):
  120. +                    $val = $val;
  121. +                    break;
  122. +                case (is_numeric($val) && (float)$val == $val ):
  123. +                    $val = sprintf("%.11f", $val);
  124. +                    break;
  125. +                default:
  126. +                    $val = $this->escapeString($val);
  127. +                    if ($quoteStrings) {
  128. +                        $val = '"' . $val . '"';
  129. +                    }
  130. +                    break;
  131. +            }
  132. +            return $val;
  133. +        }
  134.     }
  135.  /**
  136.   * AfterRender callback.  Writes any cached events to the view, or to a temp file.
  137. Index: tests/cases/libs/view/helpers/javascript.test.php
  138.  
  139. ===================================================================
  140.  
  141. --- tests/cases/libs/view/helpers/javascript.test.php   (revision 7111)
  142.  
  143. +++ tests/cases/libs/view/helpers/javascript.test.php   (working copy)
  144.  
  145. @@ -332,6 +332,11 @@
  146.  
  147.           $result = $this->Javascript->object(array('Object' => array(true => true, false, -3.141592653589, -10)));
  148.           $expected = '{"Object":{"1":true,"2":false,"3":' . (-1 * $number) . ',"4":-10}}';
  149.           $this->assertEqual($result, $expected);
  150. +           
  151. +            $result = $this->Javascript->object(array('Object' => array(1,-1,"1","-1",$number,"$number")),array('strict' => false));
  152. +           
  153. +            $expected = '{"Object":[1,-1,1,-1,3.14159265359,3.14159265359]}';
  154. +            $this->assertEqual($result, $expected);
  155.        }
  156.  
  157.        $result = $this->Javascript->object(new TestJavascriptObject());
  158.  
Parsed in 0.050 seconds, using GeSHi 1.0.7.14

Modify this Paste