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

Added support for Link and Array ContentTypes in src/DynamicEntry #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions contentful
Submodule contentful added at 74f647
32 changes: 32 additions & 0 deletions src/DynamicEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using the return $array[0] directly?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point - array_first seems to be a helper function from Laravel, rather than being in the PHP standard library.

return array_values($array)[0];

would usually be better.

}

$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;
}

}