<commit_before><?php

namespace Tequila\MongoDB\ODM\Metadata\Field;

use MongoDB\BSON\ObjectID;
use Tequila\MongoDB\ODM\Code\DocumentGenerator;
use Tequila\MongoDB\ODM\Proxy\Generator\AbstractGenerator;

class ObjectIdField extends AbstractFieldMetadata
{
    /**
     * @var bool
     */
    private $generateIfNotSet;

    /**
     * @param string $propertyName
     * @param string $dbFieldName
     * @param bool   $generateIfNotSet
     */
    public function __construct(string $propertyName, string $dbFieldName = null, bool $generateIfNotSet = false)
    {
        $this->generateIfNotSet = $generateIfNotSet;
        parent::__construct($propertyName, $dbFieldName);
    }

    public function generateDocument(DocumentGenerator $documentGenerator)
    {
        $documentGenerator->addUse(ObjectID::class);

        parent::generateDocument($documentGenerator);
    }

    public function generateProxy(AbstractGenerator $proxyGenerator)
    {
        $proxyGenerator->addUse(ObjectID::class);

        parent::generateProxy($proxyGenerator);
    }

    public function getType(): string
    {
        return ObjectID::class;
    }

    public function getSerializationCode(): string
    {
        if ($this->generateIfNotSet) {
            $code = <<<'EOT'
$objectData = $objectData ?? new ObjectID();
$dbData = $objectData;
EOT;
        } else {
            $code = parent::getSerializationCode();
        }

        return $code;
    }
}
<commit_msg>Converting ObjectID to string<commit_after><?php

namespace Tequila\MongoDB\ODM\Metadata\Field;

use MongoDB\BSON\ObjectID;
use Tequila\MongoDB\ODM\Code\DocumentGenerator;
use Tequila\MongoDB\ODM\Proxy\Generator\AbstractGenerator;

class ObjectIdField extends AbstractFieldMetadata
{
    public function generateDocument(DocumentGenerator $documentGenerator)
    {
        $documentGenerator->addUse(ObjectID::class);

        parent::generateDocument($documentGenerator);
    }

    /**
     * @param AbstractGenerator $proxyGenerator
     *
     * @throws \ReflectionException
     */
    public function generateProxy(AbstractGenerator $proxyGenerator)
    {
        $proxyGenerator->addUse(ObjectID::class);

        parent::generateProxy($proxyGenerator);
    }

    public function getType(): string
    {
        return 'string';
    }

    public function getSerializationCode(): string
    {
        return <<<'EOT'
if (null === $objectData) {
    $dbData = null;
} elseif (!$objectData instanceof ObjectID) {
    $dbData = new ObjectID((string)$objectData);
} else {
    $dbData = $objectData;
}
EOT;
    }

    public function getUnserializationCode(AbstractGenerator $proxyGenerator): string
    {
        return <<<'EOT'
$objectData = null === $dbData ? null : (string) $dbData;
EOT;
    }
}

{'fraction_non_alphanumeric': 0.11307803468208093, 'fraction_numerical': 0.0, 'mean_word_length': 3.2731481481481484, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 9, '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': '2475259', 'n_tokens_mistral': 802, 'n_tokens_neox': 794, 'n_words': 153}