Merge pull request #15969 from eileenmcnaughton/utfmb8
[civicrm-core.git] / CRM / Utils / API / NullOutputCoder.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Work-around for CRM-13120 - The "create" action incorrectly returns string literal "null"
14 * when the actual value is NULL or "". Rewrite the output.
15 *
16 * @package CRM
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 */
19
20 require_once 'api/Wrapper.php';
21
22 /**
23 * Class CRM_Utils_API_NullOutputCoder
24 */
25 class CRM_Utils_API_NullOutputCoder extends CRM_Utils_API_AbstractFieldCoder {
26
27 /**
28 * @var CRM_Utils_API_NullOutputCoder
29 */
30 private static $_singleton = NULL;
31
32 /**
33 * @return CRM_Utils_API_NullOutputCoder
34 */
35 public static function singleton() {
36 if (self::$_singleton === NULL) {
37 self::$_singleton = new CRM_Utils_API_NullOutputCoder();
38 }
39 return self::$_singleton;
40 }
41
42 /**
43 * Going to filter the submitted values across XSS vulnerability.
44 *
45 * @param array|string $values
46 */
47 public function encodeInput(&$values) {
48 }
49
50 /**
51 * Decode output.
52 *
53 * @param array $values
54 * @param bool $castToString
55 */
56 public function decodeOutput(&$values, $castToString = FALSE) {
57 if (is_array($values)) {
58 foreach ($values as &$value) {
59 $this->decodeOutput($value, TRUE);
60 }
61 }
62 elseif ($castToString || is_string($values)) {
63 if ($values === 'null') {
64 $values = '';
65 }
66 }
67 }
68
69 /**
70 * To api output.
71 *
72 * @param array $apiRequest
73 * @param array $result
74 *
75 * @return array
76 */
77 public function toApiOutput($apiRequest, $result) {
78 $lowerAction = strtolower($apiRequest['action']);
79 if ($lowerAction === 'create') {
80 return parent::toApiOutput($apiRequest, $result);
81 }
82 else {
83 return $result;
84 }
85 }
86
87 }