Serializer/Serializer.php
private function doSerialize($data, $include = false)
    {
        // TODO Handle cycles and allow subtrees to be embedded instead of just being referenced
        $metadata = $this->hydraApi->getMetadataFor(get_class($data));

        if (null === $metadata) {
            // TODO Improve this error message
            throw new \Exception(sprintf('"%s" cannot be serialized as it is not documented.', get_class($data)));
        }

        $result = array();
        if ($include) {
            $result['@context'] = $this->router->generate('hydra_context', array('type' => $metadata->getExposeAs()));
        }

        if (null !== ($route = $metadata->getRoute())) {
            if (null !== ($url = $this->generateUrl($route, $data))) {
                $result['@id'] = $url;
            }
        }

        if ($include) {
            $result['@type'] = $metadata->getExposeAs();
        } else {
            $result['@type'] = ($metadata->isExternalReference())
                ? $metadata->getIri()
                : 'vocab:' . $metadata->getIri();
        }

        if (false === $include) {
            return $result;
        }


        foreach ($metadata->getProperties() as $property) {
            if ($property->isWriteOnly()) {
                continue;
            }

            $value = $property->getValue($data);

            if (null !== ($route = $property->getRoute())) {
                if (false === $value) {
                    continue;
                }

                $url = $this->generateUrl($route, $data, $value);

                if (null !== $url) {
                    $result[$property->getExposeAs()] = $url;
                }

                continue;
            }

            // TODO Recurse
            if (is_object($value) && $this->hydraApi->hasNormalizer(get_class($value))) {
                $normalizer = $this->hydraApi->getNormalizer(get_class($value));
                $result[$property->getExposeAs()] = $normalizer->normalize($value);
            } elseif (is_array($value) || ($value instanceof \ArrayAccess) || ($value instanceof \Travesable)) {
                $result[$property->getExposeAs()] = array();
                foreach ($value as $val) {
                    $result[$property->getExposeAs()][] = $this->doSerialize($val);
                }
            } else {
                $result[$property->getExposeAs()] = $value;
            }
        }

        return $result;
    }
private function doSerialize($data, $include = false)
    {
        // TODO Handle cycles and allow subtrees to be embedded instead of just being referenced
        $metadata = $this->hydraApi->getMetadataFor(get_class($data));

        if (null === $metadata) {
            // TODO Improve this error message
            throw new \Exception(sprintf('"%s" cannot be serialized as it is not documented.', get_class($data)));
        }

        $result = array();
        if ($include) {
            $result['@context'] = $this->router->generate('hydra_context', array('type' => $metadata->getExposeAs()));
        }

        if (null !== ($route = $metadata->getRoute())) {
            if (null !== ($url = $this->generateUrl($route, $data))) {
                $result['@id'] = $url;
            }
        }

        if ($include) {
            $result['@type'] = $metadata->getExposeAs();
        } else {
            $result['@type'] = ($metadata->isExternalReference())
                ? $metadata->getIri()
                : 'vocab:' . $metadata->getIri();
        }

        if (false === $include) {
            return $result;
        }


        foreach ($metadata->getProperties() as $property) {
            if ($property->isWriteOnly()) {
                continue;
            }

            $value = $property->getValue($data);

            if (null !== ($route = $property->getRoute())) {
                if (false === $value) {
                    continue;
                }

                $url = $this->generateUrl($route, $data, $value);

                if (null !== $url) {
                    $result[$property->getExposeAs()] = $url;
                }

                continue;
            }

            // TODO Recurse
            if (is_object($value) && $this->hydraApi->hasNormalizer(get_class($value))) {
                $normalizer = $this->hydraApi->getNormalizer(get_class($value));
                $result[$property->getExposeAs()] = $normalizer->normalize($value);
            } elseif (is_array($value) || ($value instanceof \ArrayAccess) || ($value instanceof \Travesable)) {
                $result[$property->getExposeAs()] = array();
                foreach ($value as $val) {
                    $result[$property->getExposeAs()][] = $this->doSerialize($val);
                }
            } else {
                $result[$property->getExposeAs()] = $value;
            }
        }

        return $result;
    }
Serializes data

@param mixed    $data    The data to serialize.
@param boolealn $include Include (embed) the complete data instead of
of just adding a reference?

@return mixed The serialized data.