6a69e29ac35ce1c4c5e1f20473c9e1f5d8c32d74
[civicrm-core.git] / Civi / Api4 / Utils / FormattingUtil.php
1 <?php
2
3 namespace Civi\Api4\Utils;
4
5 use CRM_Utils_Array as UtilsArray;
6
7 require_once 'api/v3/utils.php';
8
9 class FormattingUtil {
10
11 /**
12 * Massage values into the format the BAO expects for a write operation
13 *
14 * @param $params
15 * @param $entity
16 * @param $fields
17 * @throws \API_Exception
18 */
19 public static function formatWriteParams(&$params, $entity, $fields) {
20 foreach ($fields as $name => $field) {
21 if (!empty($params[$name])) {
22 $value =& $params[$name];
23 // Hack for null values -- see comment below
24 if ($value === 'null') {
25 $value = 'Null';
26 }
27 FormattingUtil::formatValue($value, $field, $entity);
28 // Ensure we have an array for serialized fields
29 if (!empty($field['serialize'] && !is_array($value))) {
30 $value = (array) $value;
31 }
32 }
33 /*
34 * Because of the wacky way that database values are saved we need to format
35 * some of the values here. In this strange world the string 'null' is used to
36 * unset values. Hence if we encounter true null we change it to string 'null'.
37 *
38 * If we encounter the string 'null' then we assume the user actually wants to
39 * set the value to string null. However since the string null is reserved for
40 * unsetting values we must change it. Another quirk of the DB_DataObject is
41 * that it allows 'Null' to be set, but any other variation of string 'null'
42 * will be converted to true null, e.g. 'nuLL', 'NUlL' etc. so we change it to
43 * 'Null'.
44 */
45 elseif (array_key_exists($name, $params) && $params[$name] === NULL) {
46 $params[$name] = 'null';
47 }
48 }
49 }
50
51 /**
52 * Transform raw api input to appropriate format for use in a SQL query.
53 *
54 * This is used by read AND write actions (Get, Create, Update, Replace)
55 *
56 * @param $value
57 * @param $fieldSpec
58 * @param string $entity
59 * Ex: 'Contact', 'Domain'
60 * @throws \API_Exception
61 */
62 public static function formatValue(&$value, $fieldSpec, $entity) {
63 if (is_array($value)) {
64 foreach ($value as &$val) {
65 self::formatValue($val, $fieldSpec, $entity);
66 }
67 return;
68 }
69 $fk = UtilsArray::value('fk_entity', $fieldSpec);
70 if ($fieldSpec['name'] == 'id') {
71 $fk = $entity;
72 }
73 $dataType = UtilsArray::value('data_type', $fieldSpec);
74
75 if ($fk === 'Domain' && $value === 'current_domain') {
76 $value = \CRM_Core_Config::domainID();
77 }
78
79 if ($fk === 'Contact' && !is_numeric($value)) {
80 $value = \_civicrm_api3_resolve_contactID($value);
81 if ('unknown-user' === $value) {
82 throw new \API_Exception("\"{$fieldSpec['name']}\" \"{$value}\" cannot be resolved to a contact ID", 2002, ['error_field' => $fieldSpec['name'], "type" => "integer"]);
83 }
84 }
85
86 switch ($dataType) {
87 case 'Timestamp':
88 $value = date('Y-m-d H:i:s', strtotime($value));
89 break;
90
91 case 'Date':
92 $value = date('Ymd', strtotime($value));
93 break;
94 }
95 }
96
97 }