Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api4 - Convert field values to correct data type #16274

Merged
merged 1 commit into from
Jan 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions Civi/Api4/Event/Subscriber/PostSelectQuerySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ protected function postRun(array $results, Api4SelectQuery $query) {
return $results;
}

$fieldSpec = $query->getApiFieldSpec();
$this->unserializeFields($results, $query->getEntity(), $fieldSpec);
$this->formatFieldValues($results, $query->getApiFieldSpec());

// Group the selects to avoid queries for each field
$groupedSelects = $this->getNtoManyJoinSelects($query);
Expand All @@ -75,7 +74,7 @@ protected function postRun(array $results, Api4SelectQuery $query) {
foreach ($results as &$primaryResult) {
$baseId = $primaryResult['id'];
$filtered = array_filter($joinResults, function ($res) use ($baseId) {
return ($res['_base_id'] === $baseId);
return ($res['_base_id'] == $baseId);
});
$filtered = array_values($filtered);
ArrayInsertionUtil::insert($primaryResult, $joinPath, $filtered);
Expand All @@ -98,26 +97,55 @@ private function formatJoinResults(&$joinResults, $query, $alias) {
$fields[array_pop($name)] = $field->toArray();
}
if ($fields) {
$this->unserializeFields($joinResults, NULL, $fields);
$this->formatFieldValues($joinResults, $fields);
}
}

/**
* Unserialize values
* Unserialize values and convert to correct type
*
* @param array $results
* @param string $entity
* @param array $fields
*/
protected function unserializeFields(&$results, $entity, $fields = []) {
protected function formatFieldValues(&$results, $fields = []) {
foreach ($results as &$result) {
foreach ($result as $field => &$value) {
if (!empty($fields[$field]['serialize']) && is_string($value)) {
$serializationType = $fields[$field]['serialize'];
$value = \CRM_Core_DAO::unSerializeField($value, $serializationType);
foreach ($result as $field => $value) {
$dataType = $fields[$field]['data_type'] ?? NULL;
if (!empty($fields[$field]['serialize'])) {
if (is_string($value)) {
$result[$field] = $value = \CRM_Core_DAO::unSerializeField($value, $fields[$field]['serialize']);
foreach ($value as $key => $val) {
$result[$field][$key] = $this->convertDataType($val, $dataType);
}
}
}
else {
$result[$field] = $this->convertDataType($value, $dataType);
}
}
}
}

/**
* @param mixed $value
* @param string $dataType
* @return mixed
*/
protected function convertDataType($value, $dataType) {
if (isset($value)) {
switch ($dataType) {
case 'Boolean':
return (bool) $value;

case 'Integer':
return (int) $value;

case 'Money':
case 'Float':
return (float) $value;
}
}
return $value;
}

/**
Expand Down