protected function transcodeDataToDocument($document, $identifier)
    {
        if (!$document instanceof Document) {

            if (is_object($document)) {
                if (get_class($document) == 'stdClass') {
                    $document = (array) $document;
                } else {
                    if ($document instanceof \JsonSerializable) {
                        $document = json_encode($document);
                    } elseif (method_exists($document, 'toArray')) {
                        $document = $document->toArray();
                    } else {
                        throw new \LogicException(
                            'The given object representing a document value eihter have to implement the JsonSerializable'.
                            'interface or a toArray() method in order to be stored it in elasticsearch.',
                            AdaptorException::DATA_UNSERIALIZABLE
                        );
                    }
                }
            }

            $document = $this->decorator->normalizeValue($document);

            Assertion::notEmpty($document, 'The document data may not be empty.');

            $document = new Document($identifier, $document);
        }

        return $document;
    }