<commit_before><?php

namespace Doctrine\ODM\MongoDB\Persisters;

use Doctrine\ODM\MongoDB\DocumentManager,
    Doctrine\ODM\MongoDB\UnitOfWork,
    Doctrine\ODM\MongoDB\Mapping\ClassMetadata,
    Doctrine\ODM\MongoDB\Mapping\Types\Type;

class DataPreparer
{
    /**
     * The DocumentManager instance.
     *
     * @var Doctrine\ODM\MongoDB\DocumentManager
     */
    private $dm;

    /**
     * The UnitOfWork instance.
     *
     * @var Doctrine\ODM\MongoDB\UnitOfWork
     */
    private $uow;

    /**
     * Initializes a new DataPreparer instance.
     *
     * @param Doctrine\ODM\MongoDB\DocumentManager $dm
     * @param Doctrine\ODM\MongoDB\UnitOfWork $uow
     */
    public function __construct(DocumentManager $dm, UnitOfWork $uow, $cmd)
    {
        $this->dm = $dm;
        $this->uow = $uow;
        $this->cmd = $cmd;
    }

    /**
     * Prepares insert data for document
     *
     * @param mixed $document
     * @return array
     */
    public function prepareInsertData($document)
    {
        $oid = spl_object_hash($document);
        $class = $this->dm->getClassMetadata(get_class($document));
        $changeset = $this->uow->getDocumentChangeSet($document);
        $insertData = array();
        foreach ($class->fieldMappings as $mapping) {
            if ($mapping['type'] === 'many') {
                continue;
            }
            if (isset($mapping['notSaved']) && $mapping['notSaved'] === true) {
                continue;
            }
            $new = isset($changeset[$mapping['fieldName']][1]) ? $changeset[$mapping['fieldName']][1] : null;
            if ($class->isIdentifier($mapping['fieldName'])) {
                if ($new === null) {
                    $new = new \MongoId();
                }
                $insertData['_id'] = $this->prepareValue($mapping, $new);
                continue;
            }
            if ($new === null && $mapping['nullable'] === false) {
                continue;
            }
            $value = $this->prepareValue($mapping, $new);
            if ($value === null && $mapping['nullable'] === false) {
                continue;
            }

            $insertData[$mapping['name']] = $value;
            if (isset($mapping['reference'])) {
                $scheduleForUpdate = false;
                if ($mapping['type'] === 'one') {
                    if ( ! isset($insertData[$mapping['name']][$this->cmd . 'id'])) {
                        $scheduleForUpdate = true;
                    }
                }
                if ($scheduleForUpdate) {
                    unset($insertData[$mapping['name']]);
                    $this->uow->scheduleExtraUpdate($document, array(
                        $mapping['fieldName'] => array(null, $new)
                    ));
                }
            }
        }
        // add discriminator if the class has one
        if ($class->hasDiscriminator()) {
            $insertData[$class->discriminatorField['name']] = $class->discriminatorValue;
        }
        return $insertData;
    }

    /**
     * Prepares update array for document, using atomic operators
     *
     * @param mixed $document
     * @return array
     */
    public function prepareUpdateData($document)
    {
        $oid = spl_object_hash($document);
        $class = $this->dm->getClassMetadata(get_class($document));
        $changeset = $this->uow->getDocumentChangeSet($document);
        $result = array();
        foreach ($changeset as $fieldName => $change) {
            $mapping = $class->fieldMappings[$fieldName];
            if (isset($mapping['reference']) && $mapping['type'] === 'many') {
                continue;
            }
            if (isset($mapping['notSaved']) && $mapping['notSaved'] === true) {
                continue;
            }
            list($old, $new) = $change;

            if (isset($mapping['embedded']) && $mapping['type'] === 'one') {
                // If we have a new embedded document then lets set it
                if ($new && $this->uow->isScheduledForInsert($new)) {
                    $result[$this->cmd . 'set'] = array($mapping['name'] => $this->prepareEmbeddedDocValue($mapping, $new));
                // If we don't have a new value then lets unset the embedded document
                } else if ( ! $new) {
                    $result[$this->cmd . 'unset'] = array($mapping['name'] => true);
                // Update existing embedded document
                } else {
                    $update = $this->prepareUpdateData($new);
                    foreach ($update as $cmd => $values) {
                        foreach ($values as $key => $value) {
                            $result[$cmd][$mapping['name'] . '.' . $key] = $value;
                        }
                    }
                }
            } else if (isset($mapping['embedded']) && $mapping['type'] === 'many') {
                foreach ($new as $key => $embeddedDoc) {
                    if (!$this->uow->isScheduledForInsert($embeddedDoc)) {
                        $update = $this->prepareUpdateData($embeddedDoc);
                        foreach ($update as $cmd => $values) {<commit_msg>DataPreparer::prepareUpdateData should not ovewrite  when preparing embedded data<commit_after><?php

namespace Doctrine\ODM\MongoDB\Persisters;

use Doctrine\ODM\MongoDB\DocumentManager,
    Doctrine\ODM\MongoDB\UnitOfWork,
    Doctrine\ODM\MongoDB\Mapping\ClassMetadata,
    Doctrine\ODM\MongoDB\Mapping\Types\Type;

class DataPreparer
{
    /**
     * The DocumentManager instance.
     *
     * @var Doctrine\ODM\MongoDB\DocumentManager
     */
    private $dm;

    /**
     * The UnitOfWork instance.
     *
     * @var Doctrine\ODM\MongoDB\UnitOfWork
     */
    private $uow;

    /**
     * Initializes a new DataPreparer instance.
     *
     * @param Doctrine\ODM\MongoDB\DocumentManager $dm
     * @param Doctrine\ODM\MongoDB\UnitOfWork $uow
     */
    public function __construct(DocumentManager $dm, UnitOfWork $uow, $cmd)
    {
        $this->dm = $dm;
        $this->uow = $uow;
        $this->cmd = $cmd;
    }

    /**
     * Prepares insert data for document
     *
     * @param mixed $document
     * @return array
     */
    public function prepareInsertData($document)
    {
        $oid = spl_object_hash($document);
        $class = $this->dm->getClassMetadata(get_class($document));
        $changeset = $this->uow->getDocumentChangeSet($document);
        $insertData = array();
        foreach ($class->fieldMappings as $mapping) {
            if ($mapping['type'] === 'many') {
                continue;
            }
            if (isset($mapping['notSaved']) && $mapping['notSaved'] === true) {
                continue;
            }
            $new = isset($changeset[$mapping['fieldName']][1]) ? $changeset[$mapping['fieldName']][1] : null;
            if ($class->isIdentifier($mapping['fieldName'])) {
                if ($new === null) {
                    $new = new \MongoId();
                }
                $insertData['_id'] = $this->prepareValue($mapping, $new);
                continue;
            }
            if ($new === null && $mapping['nullable'] === false) {
                continue;
            }
            $value = $this->prepareValue($mapping, $new);
            if ($value === null && $mapping['nullable'] === false) {
                continue;
            }

            $insertData[$mapping['name']] = $value;
            if (isset($mapping['reference'])) {
                $scheduleForUpdate = false;
                if ($mapping['type'] === 'one') {
                    if ( ! isset($insertData[$mapping['name']][$this->cmd . 'id'])) {
                        $scheduleForUpdate = true;
                    }
                }
                if ($scheduleForUpdate) {
                    unset($insertData[$mapping['name']]);
                    $this->uow->scheduleExtraUpdate($document, array(
                        $mapping['fieldName'] => array(null, $new)
                    ));
                }
            }
        }
        // add discriminator if the class has one
        if ($class->hasDiscriminator()) {
            $insertData[$class->discriminatorField['name']] = $class->discriminatorValue;
        }
        return $insertData;
    }

    /**
     * Prepares update array for document, using atomic operators
     *
     * @param mixed $document
     * @return array
     */
    public function prepareUpdateData($document)
    {
        $oid = spl_object_hash($document);
        $class = $this->dm->getClassMetadata(get_class($document));
        $changeset = $this->uow->getDocumentChangeSet($document);
        $result = array();
        foreach ($changeset as $fieldName => $change) {
            $mapping = $class->fieldMappings[$fieldName];
            if (isset($mapping['reference']) && $mapping['type'] === 'many') {
                continue;
            }
            if (isset($mapping['notSaved']) && $mapping['notSaved'] === true) {
                continue;
            }
            list($old, $new) = $change;

            if (isset($mapping['embedded']) && $mapping['type'] === 'one') {
                // If we have a new embedded document then lets set it
                if ($new && $this->uow->isScheduledForInsert($new)) {
                    $result[$this->cmd . 'set'][$mapping['name']] = $this->prepareEmbeddedDocValue($mapping, $new);
                // If we don't have a new value then lets unset the embedded document
                } else if ( ! $new) {
                    $result[$this->cmd . 'unset'][$mapping['name']] = true;
                // Update existing embedded document
                } else {
                    $update = $this->prepareUpdateData($new);
                    foreach ($update as $cmd => $values) {
                        foreach ($values as $key => $value) {
                            $result[$cmd][$mapping['name'] . '.' . $key] = $value;
                        }
                    }
                }
            } else if (isset($mapping['embedded']) && $mapping['type'] === 'many') {
                foreach ($new as $key => $embeddedDoc) {
                    if (!$this->uow->isScheduledForInsert($embeddedDoc)) {
                        $update = $this->prepareUpdateData($embeddedDoc);
                        foreach ($update as $cmd => $values) {
{'fraction_non_alphanumeric': 0.1509143407122233, 'fraction_numerical': 0.00038498556304138594, 'mean_word_length': 1.7024707412223667, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 79, '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': '6032458', 'n_tokens_mistral': 2872, 'n_tokens_neox': 2771, 'n_words': 553}