Skip to content

Commit

Permalink
Double fix for a bug whereby transforming an attribute that has an ar…
Browse files Browse the repository at this point in the history
…ray cast, which happens to be null, gets turned into an empty string.
  • Loading branch information
specialtactics committed Jul 15, 2019
1 parent 029f14e commit 5697865
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Transformers/RestfulTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,13 @@ protected function transformKeysCase(array $transformed)
* However, if the level is 1, we also want to transform the first level of keys in a json field which has been cast to array
*/
if ($levels == 1 && ! is_null($this->model)) {
// Go through casted atrributes, figuring out what their new key name is to access them
foreach ($this->model->getCasts() as $fieldName => $castType) {
if ($castType == 'array') {
$fieldNameFormatted = $this->formatKeyCase($fieldName);
if (array_key_exists($fieldNameFormatted, $transformed)) {

// Transform the keys of the array attribute
if (array_key_exists($fieldNameFormatted, $transformed) && !empty($transformed[$fieldNameFormatted])) {
$transformed[$fieldNameFormatted] = $this->formatKeyCase($transformed[$fieldNameFormatted]);
}
}
Expand All @@ -146,6 +149,11 @@ protected function transformKeysCase(array $transformed)
*/
protected function formatKeyCase($input, $levels = null)
{
// Fail early in the event of special cases (such as a null which could be an array), to prevent unwanted casting
if (empty($input)) {
return $input;
}

$caseFormat = APIBoilerplate::getResponseCaseType();

if ($caseFormat == APIBoilerplate::CAMEL_CASE) {
Expand Down

0 comments on commit 5697865

Please sign in to comment.