<commit_before><?php

namespace Markup\Contentful;


class ResourceBuilder
{
    /**
     * @var callable
     */
    private $resolveLinkFunction;

    /**
     * @var bool
     */
    private $useDynamicEntries;

    /**
     * @var ResourceEnvelope
     */
    private $envelope;

    /**
     * @param ResourceEnvelope|null $envelope
     */
    public function __construct(ResourceEnvelope $envelope = null)
    {
        $this->envelope = $envelope ?: new ResourceEnvelope();
    }

    /**
     * @param array $data
     * @return mixed A Contentful resource.
     */
    public function buildFromData(array $data)
    {
        if ($this->isArrayResourceData($data)) {
            return array_map(function ($resourceData) {
                return $this->buildFromData($resourceData);
            }, $data);
        }
        $metadata = $this->buildMetadataFromSysData($data['sys']);
        if (!$metadata->getType()) {
            throw new \InvalidArgumentException('Resource data must always have a type in its system properties.');
        }
        switch ($metadata->getType()) {
            case 'Space':
                $locales = [];
                $defaultLocale = null;
                foreach ($data['locales'] as $locale) {
                    $localeObj = new Locale($locale['code'], $locale['name']);
                    if (isset($locale['default']) && $locale['default']) {
                        $defaultLocale = $localeObj;
                    }
                    $locales[] = $localeObj;
                }

                return new Space($data['name'], $metadata, $locales, $defaultLocale);
            case 'Entry':
                $fields = [];
                foreach ($data['fields'] as $name => $fieldData) {
                    if ($this->isResourceData($fieldData)) {
                        $fields[$name] = $this->buildFromData($fieldData);
                    } elseif ($this->isArrayResourceData($fieldData)) {
                        $fields[$name] = array_map(function ($itemData) {
                            return $this->buildFromData($itemData);
                        }, $fieldData);
                    } else {
                        $fields[$name] = $fieldData;
                    }
                }
                $entry = new Entry($fields, $metadata);
                if (null !== $this->resolveLinkFunction) {
                    $entry->setResolveLinkFunction($this->resolveLinkFunction);
                }
                if ($this->useDynamicEntries) {
                    $contentType = $metadata->getContentType();
                    if ($contentType instanceof Link) {
                        $contentType = call_user_func($this->resolveLinkFunction, $contentType);
                    }
                    $entry = new DynamicEntry($entry, $contentType);
                }
                $this->envelope->insertEntry($entry);

                return $entry;
            case 'Asset':
                $asset = new Asset(
                    $data['fields']['title'],
                    (isset($data['fields']['description'])) ? $data['fields']['description'] : '',<commit_msg>handle lack of fields data for an entry (could be case if preview API being accessed)<commit_after><?php

namespace Markup\Contentful;


class ResourceBuilder
{
    /**
     * @var callable
     */
    private $resolveLinkFunction;

    /**
     * @var bool
     */
    private $useDynamicEntries;

    /**
     * @var ResourceEnvelope
     */
    private $envelope;

    /**
     * @param ResourceEnvelope|null $envelope
     */
    public function __construct(ResourceEnvelope $envelope = null)
    {
        $this->envelope = $envelope ?: new ResourceEnvelope();
    }

    /**
     * @param array $data
     * @return mixed A Contentful resource.
     */
    public function buildFromData(array $data)
    {
        if ($this->isArrayResourceData($data)) {
            return array_map(function ($resourceData) {
                return $this->buildFromData($resourceData);
            }, $data);
        }
        $metadata = $this->buildMetadataFromSysData($data['sys']);
        if (!$metadata->getType()) {
            throw new \InvalidArgumentException('Resource data must always have a type in its system properties.');
        }
        switch ($metadata->getType()) {
            case 'Space':
                $locales = [];
                $defaultLocale = null;
                foreach ($data['locales'] as $locale) {
                    $localeObj = new Locale($locale['code'], $locale['name']);
                    if (isset($locale['default']) && $locale['default']) {
                        $defaultLocale = $localeObj;
                    }
                    $locales[] = $localeObj;
                }

                return new Space($data['name'], $metadata, $locales, $defaultLocale);
            case 'Entry':
                $fields = [];
                if (isset($data['fields'])) {
                    foreach ($data['fields'] as $name => $fieldData) {
                        if ($this->isResourceData($fieldData)) {
                            $fields[$name] = $this->buildFromData($fieldData);
                        } elseif ($this->isArrayResourceData($fieldData)) {
                            $fields[$name] = array_map(function ($itemData) {
                                return $this->buildFromData($itemData);
                            }, $fieldData);
                        } else {
                            $fields[$name] = $fieldData;
                        }
                    }
                }
                $entry = new Entry($fields, $metadata);
                if (null !== $this->resolveLinkFunction) {
                    $entry->setResolveLinkFunction($this->resolveLinkFunction);
                }
                if ($this->useDynamicEntries) {
                    $contentType = $metadata->getContentType();
                    if ($contentType instanceof Link) {
                        $contentType = call_user_func($this->resolveLinkFunction, $contentType);
                    }
                    $entry = new DynamicEntry($entry, $contentType);
                }
                $this->envelope->insertEntry($entry);

                return $entry;
            case 'Asset':
                $asset = new Asset(
                    $data['fields']['title'],
                    (isset($data['fields']['description'])) ? $data['fields']['description'] : '',
{'fraction_non_alphanumeric': 0.13213074313906875, 'fraction_numerical': 0.0, 'mean_word_length': 1.5123934934159566, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4003183', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1524, 'n_words': 319}