diff --git a/contentful b/contentful new file mode 160000 index 0000000..74f647c --- /dev/null +++ b/contentful @@ -0,0 +1 @@ +Subproject commit 74f647c9d75a823ab2332117b8b7f65f5264ea33 diff --git a/src/DynamicEntry.php b/src/DynamicEntry.php index bdec8fa..552d69c 100644 --- a/src/DynamicEntry.php +++ b/src/DynamicEntry.php @@ -174,9 +174,11 @@ private function getCoercedField($key) { $contentTypeField = $this->contentType->getField($key); $raw = $this->entry->getField($key); + if (!$contentTypeField) { return $raw; } + switch ($contentTypeField->getType()) { case 'Symbol': case 'Text': @@ -193,8 +195,38 @@ private function getCoercedField($key) case 'Location': list($lat, $lon) = explode(',', $raw); return new Location(floatval($lat), floatval($lon)); + case 'Array': + return $this->formatArray($raw); + case 'Link': + return $raw->getFields(); default: return $raw; } } + + /** + * Formats arrays with single value | Array with DynamicEntries + * + * @param $array + * @return string|array + */ + private function formatArray($array) + { + if (count($array) == 1) { + return array_first($array); + } + + $returnArray = []; + foreach ($array as $id => $dynamicEntry) { + // If its not an instance, but array with multiple strings + if (is_string($dynamicEntry)) { + return $array; + } + + $returnArray[$id] = $dynamicEntry->getFields(); + } + + return $returnArray; + } + }