PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/pkp/classes/core/JSON.inc.php

https://github.com/lib-uoguelph-ca/ocs
PHP | 188 lines | 96 code | 20 blank | 72 comment | 10 complexity | 7a68891647636ad316cd11c4c54b0ce5 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file classes/core/JSON.inc.php
  4. *
  5. * Copyright (c) 2000-2012 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class JSON
  9. * @ingroup core
  10. *
  11. * @brief Class to build and manipulate JSON (Javascript Object Notation) objects.
  12. *
  13. */
  14. // $Id$
  15. class JSON {
  16. /** @var $status string The status of an event (e.g. false if form validation fails) */
  17. var $status;
  18. /** @var $content string The message to be delivered back to the calling script */
  19. var $content;
  20. /** @var $isScript string Whether the content is javascript that should be executed */
  21. var $isScript;
  22. /** @var $elementId string ID for DOM element that will be replaced */
  23. var $elementId;
  24. /** @var $additionalAttributes array Set of additional attributes for special cases*/
  25. var $additionalAttributes;
  26. /**
  27. * Constructor.
  28. * @param $status string The status of an event (e.g. false if form validation fails)
  29. * @param $content string The message to be delivered back to the calling script
  30. */
  31. function JSON($status = 'true', $content = '', $isScript = 'false', $elementId = '0', $additionalAttributes = null) {
  32. $this->status = $status;
  33. $this->content = $this->json_encode($content);
  34. $this->isScript = $isScript;
  35. $this->elementId = $this->json_encode($elementId);
  36. if (isset($additionalAttributes)) {
  37. $this->additionalAttributes = $additionalAttributes;
  38. }
  39. }
  40. /**
  41. * Get the status string
  42. * @return string
  43. */
  44. function getStatus () {
  45. return $this->status;
  46. }
  47. /**
  48. * Set the status string
  49. * @param $status string
  50. */
  51. function setStatus($status) {
  52. $this->status = $status;
  53. }
  54. /**
  55. * Construct the content string
  56. * @return string
  57. */
  58. function getContent() {
  59. return $this->content;
  60. }
  61. /**
  62. * Set the content string
  63. * @param $content string
  64. */
  65. function setContent($content) {
  66. $this->content = $this->json_encode($content);
  67. }
  68. /**
  69. * Get the isScript string
  70. * @return string
  71. */
  72. function getIsScript () {
  73. return $this->isScript;
  74. }
  75. /**
  76. * Set the isScript string
  77. * @param $isScript string
  78. */
  79. function setIsScript($isScript) {
  80. $this->isScript = $isScript;
  81. }
  82. /**
  83. * Get the elementId string
  84. * @return string
  85. */
  86. function getElementId () {
  87. return $this->elementId;
  88. }
  89. /**
  90. * Set the elementId string
  91. * @param $elementId string
  92. */
  93. function setElementId($elementId) {
  94. $this->elementId = $this->json_encode($elementId);
  95. }
  96. /**
  97. * Get the additionalAttributes array
  98. * @return array
  99. */
  100. function getAdditionalAttributes () {
  101. return $this->additionalAttributes;
  102. }
  103. /**
  104. * Set the additionalAttributes array
  105. * @param $additionalAttributes array
  106. */
  107. function setAdditionalAttributes($additionalAttributes) {
  108. $this->additionalAttributes = $additionalAttributes;
  109. }
  110. /**
  111. * Construct a JSON string to use for AJAX communication
  112. * @return string
  113. */
  114. function getString() {
  115. $jsonString = "{\"status\": $this->status, \"content\": $this->content, \"isScript\": $this->isScript, \"elementId\": $this->elementId";
  116. if(isset($this->additionalAttributes)) {
  117. foreach($this->additionalAttributes as $key => $value) {
  118. $jsonString .= ", \"$key\": " . $this->json_encode($value);
  119. }
  120. }
  121. $jsonString .= "}";
  122. return $jsonString;
  123. }
  124. /**
  125. * encode a string for use with JSON
  126. * Thanks to: http://usphp.com/manual/en/function.json-encode.php#82904
  127. */
  128. function json_encode($a = false) {
  129. if (function_exists('json_encode')) {
  130. return json_encode($a);
  131. } else {
  132. if (is_null($a)) return 'null';
  133. if ($a === false) return 'false';
  134. if ($a === true) return 'true';
  135. if (is_scalar($a)){
  136. if (is_float($a)) {
  137. // Always use "." for floats.
  138. return floatval(str_replace(",", ".", strval($a)));
  139. }
  140. if (is_string($a)) {
  141. static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
  142. return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
  143. }
  144. else {
  145. return $a;
  146. }
  147. }
  148. $isList = true;
  149. for ($i = 0, reset($a); $i < count($a); $i++, next($a)) {
  150. if (key($a) !== $i) {
  151. $isList = false;
  152. break;
  153. }
  154. }
  155. $result = array();
  156. if ($isList) {
  157. foreach ($a as $v) $result[] = $this->json_encode($v);
  158. return '[' . join(',', $result) . ']';
  159. }
  160. else {
  161. foreach ($a as $k => $v) $result[] = $this->json_encode($k).':'.$this->json_encode($v);
  162. return '{' . join(',', $result) . '}';
  163. }
  164. }
  165. }
  166. }
  167. ?>