Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_Javascript | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
56 | |
0.00% |
0 / 1 |
| buildObject | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * DEPRECATED, use json_encode instead. Preferably not into the template but in the controller. |
| 27 | * |
| 28 | * A helper to fascilitate the exchange of data between php and javascript |
| 29 | * |
| 30 | * @deprecated since version 2.6 |
| 31 | * @access public |
| 32 | * @author Joel Bout, <joel@taotesting.com> |
| 33 | * @package tao |
| 34 | |
| 35 | */ |
| 36 | class tao_helpers_Javascript |
| 37 | { |
| 38 | /** |
| 39 | * converts a php array or string into a javascript format |
| 40 | * |
| 41 | * @access public |
| 42 | * @author Joel Bout, <joel@taotesting.com> |
| 43 | * @param mixed $var |
| 44 | * @param boolean format |
| 45 | * @return string |
| 46 | */ |
| 47 | public static function buildObject($var, $format = false) |
| 48 | { |
| 49 | if (is_array($var)) { |
| 50 | $returnValue = '{'; |
| 51 | $i = 1; |
| 52 | foreach ($var as $k => $v) { |
| 53 | $k = is_int($k) ? '"' . json_encode($k) . '"' : json_encode($k); |
| 54 | $returnValue .= $k . ':' . self::buildObject($v, $format); |
| 55 | $returnValue .= ($i < count($var)) ? ',' : ''; |
| 56 | $returnValue .= $format ? PHP_EOL : ''; |
| 57 | $i++; |
| 58 | } |
| 59 | $returnValue .= '}'; |
| 60 | } else { |
| 61 | // Some versions of PHP simply fail |
| 62 | // when encoding non UTF-8 data. Some other |
| 63 | // versions return null... If it fails, simply |
| 64 | // reproduce a single failure scenario. |
| 65 | $returnValue = @json_encode($var); |
| 66 | |
| 67 | if ($returnValue === false) { |
| 68 | $returnValue = json_encode(null); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return $returnValue; |
| 73 | } |
| 74 | } |