<commit_before><?php namespace Tobscure\JsonApi;

class Document
{
    protected $links;

    protected $included = [];

    protected $meta;

    protected $data;

    public function addIncluded($link)
    {
        $resources = $link->getLinkage()->getResources();

        foreach ($resources as $k => $resource) {
            // If the resource doesn't have any attributes, then we don't need to
            // put it into the included part of the document.
            if (! $resource->getAttributes()) {
                unset($resources[$k]);
            }
        }

        // Filter out any resources that we have already added to the document.
        $resources = $this->uniqueResources($resources);

        foreach ($resources as $resource) {
            foreach ($resource->getIncluded() as $link) {
                $this->addIncluded($link);
            }
        }

        if ($resources) {
            $this->included = array_merge($this->included, $resources);
        }

        return $this;
    }

    protected function uniqueResources($resources)
    {
        $ids = [];

        foreach ($this->included as $resource) {
            $included[] = [$resource->getType(), $resource->getId()];
        }

        foreach ($this->data->getResources() as $resource) {
            $included[] = [$resource->getType(), $resource->getId()];
        }

        $resources = array_filter($resources, function ($resource) use ($included) {
            return ! in_array([$resource->getType(), $resource->getId()], $included);
        });

        return $resources;
    }


    public function setData($element)
    {
        $this->data = $element;

        if ($element) {
            foreach ($element->getResources() as $resource) {
                foreach ($resource->getIncluded() as $link) {
                    $this->addIncluded($link);
                }
            }
        }

        return $this;
    }

    public function addLink($key, $value)
    {
        $this->links[$key] = $value;

        return $this;
    }

    public function addMeta($key, $value)
    {
        $this->meta[$key] = $value;

        return $this;
    }

    public function setMeta($meta)
    {
        $this->meta = $meta;

        return $this;
    }

    public function toArray()
    {
        $document = [];

        if (! empty($this->links)) {
            ksort($this->links);
            $document['links'] = $this->links;
        }

        if (! empty($this->data)) {
            $document['data'] = $this->data->toArray();
        }

        if (! empty($this->included)) {
            $document['included'] = [];
            foreach ($this->included as $resource) {
                $document['included'][] = $resource->toArray();
            }
        }

        if (! empty($this->meta)) {
            $document['meta'] = $this->meta;
        }

        return $document;
    }
}<commit_msg>Allow Document to be converted to string<commit_after><?php namespace Tobscure\JsonApi;

class Document
{
    protected $links;

    protected $included = [];

    protected $meta;

    protected $data;

    public function addIncluded($link)
    {
        $resources = $link->getLinkage()->getResources();

        foreach ($resources as $k => $resource) {
            // If the resource doesn't have any attributes, then we don't need to
            // put it into the included part of the document.
            if (! $resource->getAttributes()) {
                unset($resources[$k]);
            }
        }

        // Filter out any resources that we have already added to the document.
        $resources = $this->uniqueResources($resources);

        foreach ($resources as $resource) {
            foreach ($resource->getIncluded() as $link) {
                $this->addIncluded($link);
            }
        }

        if ($resources) {
            $this->included = array_merge($this->included, $resources);
        }

        return $this;
    }

    protected function uniqueResources($resources)
    {
        $ids = [];

        foreach ($this->included as $resource) {
            $included[] = [$resource->getType(), $resource->getId()];
        }

        foreach ($this->data->getResources() as $resource) {
            $included[] = [$resource->getType(), $resource->getId()];
        }

        $resources = array_filter($resources, function ($resource) use ($included) {
            return ! in_array([$resource->getType(), $resource->getId()], $included);
        });

        return $resources;
    }


    public function setData($element)
    {
        $this->data = $element;

        if ($element) {
            foreach ($element->getResources() as $resource) {
                foreach ($resource->getIncluded() as $link) {
                    $this->addIncluded($link);
                }
            }
        }

        return $this;
    }

    public function addLink($key, $value)
    {
        $this->links[$key] = $value;

        return $this;
    }

    public function addMeta($key, $value)
    {
        $this->meta[$key] = $value;

        return $this;
    }

    public function setMeta($meta)
    {
        $this->meta = $meta;

        return $this;
    }

    public function toArray()
    {
        $document = [];

        if (! empty($this->links)) {
            ksort($this->links);
            $document['links'] = $this->links;
        }

        if (! empty($this->data)) {
            $document['data'] = $this->data->toArray();
        }

        if (! empty($this->included)) {
            $document['included'] = [];
            foreach ($this->included as $resource) {
                $document['included'][] = $resource->toArray();
            }
        }

        if (! empty($this->meta)) {
            $document['meta'] = $this->meta;
        }

        return $document;
    }

    public function __toString()
    {
        return json_encode($this->toArray());
    }
}
{'fraction_non_alphanumeric': 0.15105130361648444, 'fraction_numerical': 0.0, 'mean_word_length': 1.7527777777777778, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 76, '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': '1254622', 'n_tokens_mistral': 1653, 'n_tokens_neox': 1562, 'n_words': 363}