6a69e29ac35ce1c4c5e1f20473c9e1f5d8c32d74
3 namespace Civi\Api4\Utils
;
5 use CRM_Utils_Array
as UtilsArray
;
7 require_once 'api/v3/utils.php';
12 * Massage values into the format the BAO expects for a write operation
17 * @throws \API_Exception
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') {
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;
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'.
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
45 elseif (array_key_exists($name, $params) && $params[$name] === NULL) {
46 $params[$name] = 'null';
52 * Transform raw api input to appropriate format for use in a SQL query.
54 * This is used by read AND write actions (Get, Create, Update, Replace)
58 * @param string $entity
59 * Ex: 'Contact', 'Domain'
60 * @throws \API_Exception
62 public static function formatValue(&$value, $fieldSpec, $entity) {
63 if (is_array($value)) {
64 foreach ($value as &$val) {
65 self
::formatValue($val, $fieldSpec, $entity);
69 $fk = UtilsArray
::value('fk_entity', $fieldSpec);
70 if ($fieldSpec['name'] == 'id') {
73 $dataType = UtilsArray
::value('data_type', $fieldSpec);
75 if ($fk === 'Domain' && $value === 'current_domain') {
76 $value = \CRM_Core_Config
::domainID();
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"]);
88 $value = date('Y-m-d H:i:s', strtotime($value));
92 $value = date('Ymd', strtotime($value));