<commit_before><?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\ODM\PHPCR;

use Doctrine\ODM\PHPCR\Mapping\ClassMetadata;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ODM\PHPCR\Event\LifecycleEventArgs;
use Doctrine\ODM\PHPCR\Event\OnFlushEventArgs;
use PHPCR\PropertyType;

/**
 * Unit of work class
 *
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link        www.doctrine-project.com
 * @since       1.0
 * @author      <NAME> <<EMAIL>>
 * @author      <NAME> <<EMAIL>>
 */
class UnitOfWork
{
    const STATE_NEW = 1;
    const STATE_MANAGED = 2;
    const STATE_REMOVED = 3;
    const STATE_DETACHED = 4;

    /**
     * @var DocumentManager
     */
    private $dm = null;

    /**
     * @var array
     */
    private $identityMap = array();

    /**
     * @var array
     */
    private $nodesMap = array();

    /**
     * @var array
     */
    private $documentIds = array();

    /**
     * @var array
     */
    private $documentRevisions = array();

    /**
     * @var array
     */
    private $documentState = array();

    /**
     * PHPCR always returns and updates the whole data of a document. If on update data is "missing"
     * this means the data is deleted. This also applies to attachments. This is why we need to ensure
     * that data that is not mapped is not lost. This map here saves all the "left-over" data and keeps
     * track of it if necessary.
     *
     * @var array
     */
    private $nonMappedData = array();

    /**
     * @var array
     */
    private $originalData = array();

    /**
     * @var array
     */
    private $documentChangesets = array();

    /**
     * @var array
     */
    private $scheduledUpdates = array();

    /**
     * @var array
     */
    private $scheduledInserts = array();

    /**
     * @var array
     */
    private $scheduledRemovals = array();

    /**
     * @var array
     */
    private $visitedCollections = array();

    /**
     * @var array
     */
    private $idGenerators = array();

    /**
     * @var EventManager
     */
    private $evm;

    /**
     * @var DocumentNameMapperInterface
     */
    private $documentNameMapper;

    /**
     * @var boolean
     */
    private $validateDocumentName;

    /**
     * @var boolean
     */
    private $writeMetadata;

    /**
     * @param DocumentManager $dm
     */
    public function __construct(DocumentManager $dm, DocumentNameMapperInterface $documentNameMapper = null)
    {
        $this->dm = $dm;
        $this->evm = $dm->getEventManager();
        $this->documentNameMapper = $documentNameMapper;
        $this->validateDocumentName = $this->dm->getConfiguration()->getValidateDoctrineMetadata();
        $this->writeMetadata = $this->dm->getConfiguration()->getWriteDoctrineMetadata();
    }

    /**
     * Create a document given class, data and the doc-id and revision
     *
     * @param string $documentName
     * @param \PHPCR\NodeInterface $node
     * @param array $hints
     * @return object
     */
    public function createDocument($documentName, $node, array &$hints = array())
    {
        // get uuid rather than dereference reference properties
        $properties = $node->getPropertiesValues(null, false);

        if ($this->documentNameMapper) {
            $type = $this->documentNameMapper->getDocumentName($this->dm, $documentName, $node, $this->writeMetadata);
            $class = $this->dm->getClassMetadata($type);
        } else {
            if (isset($documentName)) {
                $class = $this->dm->getClassMetadata($documentName);
            } else if (isset($properties['phpcr:class'])) {
                $class = $this->dm->getClassMetadata($properties['phpcr:class']);
            } else if (isset($properties['phpcr:alias'])) {
                $class = $this->dm->getMetadataFactory()->getMetadataForAlias($properties['phpcr:alias']);
            }

            if ($this->writeMetadata && empty($properties['phpcr:class'])) {
                $node->setProperty('phpcr:class', $class->name, PropertyType::STRING);
            }
        }

        if (empty($class)) {
            throw new \InvalidArgumentException("Could not determine Doctrine metadata for node");
        }

        $documentState = array();
        $nonMappedData = array();
        $id = $node->getPath();

        foreach ($class->fieldMappings as $fieldName => $mapping) {
            if (isset($properties[$mapping['name']])) {
                if ($mapping['multivalue']) {
                    $documentState[$fieldName] = new ArrayCollection($properties[$mapping['name']]);
                } else {
                    $documentState[$fieldName] = $properties[$mapping['name']];
                }
            }
        }

        if ($class->node) {
            $documentState[$class->node] = $node;<commit_msg>minor fix for previous commit<commit_after><?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\ODM\PHPCR;

use Doctrine\ODM\PHPCR\Mapping\ClassMetadata;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ODM\PHPCR\Event\LifecycleEventArgs;
use Doctrine\ODM\PHPCR\Event\OnFlushEventArgs;
use PHPCR\PropertyType;

/**
 * Unit of work class
 *
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link        www.doctrine-project.com
 * @since       1.0
 * @author      <NAME> <<EMAIL>>
 * @author      <NAME> <<EMAIL>>
 */
class UnitOfWork
{
    const STATE_NEW = 1;
    const STATE_MANAGED = 2;
    const STATE_REMOVED = 3;
    const STATE_DETACHED = 4;

    /**
     * @var DocumentManager
     */
    private $dm = null;

    /**
     * @var array
     */
    private $identityMap = array();

    /**
     * @var array
     */
    private $nodesMap = array();

    /**
     * @var array
     */
    private $documentIds = array();

    /**
     * @var array
     */
    private $documentRevisions = array();

    /**
     * @var array
     */
    private $documentState = array();

    /**
     * PHPCR always returns and updates the whole data of a document. If on update data is "missing"
     * this means the data is deleted. This also applies to attachments. This is why we need to ensure
     * that data that is not mapped is not lost. This map here saves all the "left-over" data and keeps
     * track of it if necessary.
     *
     * @var array
     */
    private $nonMappedData = array();

    /**
     * @var array
     */
    private $originalData = array();

    /**
     * @var array
     */
    private $documentChangesets = array();

    /**
     * @var array
     */
    private $scheduledUpdates = array();

    /**
     * @var array
     */
    private $scheduledInserts = array();

    /**
     * @var array
     */
    private $scheduledRemovals = array();

    /**
     * @var array
     */
    private $visitedCollections = array();

    /**
     * @var array
     */
    private $idGenerators = array();

    /**
     * @var EventManager
     */
    private $evm;

    /**
     * @var DocumentNameMapperInterface
     */
    private $documentNameMapper;

    /**
     * @var boolean
     */
    private $validateDocumentName;

    /**
     * @var boolean
     */
    private $writeMetadata;

    /**
     * @param DocumentManager $dm
     */
    public function __construct(DocumentManager $dm, DocumentNameMapperInterface $documentNameMapper = null)
    {
        $this->dm = $dm;
        $this->evm = $dm->getEventManager();
        $this->documentNameMapper = $documentNameMapper;
        $this->validateDocumentName = $this->dm->getConfiguration()->getValidateDoctrineMetadata();
        $this->writeMetadata = $this->dm->getConfiguration()->getWriteDoctrineMetadata();
    }

    /**
     * Create a document given class, data and the doc-id and revision
     *
     * @param string $documentName
     * @param \PHPCR\NodeInterface $node
     * @param array $hints
     * @return object
     */
    public function createDocument($documentName, $node, array &$hints = array())
    {
        // get uuid rather than dereference reference properties
        $properties = $node->getPropertiesValues(null, false);

        if ($this->documentNameMapper) {
            $type = $this->documentNameMapper->getDocumentName($this->dm, $documentName, $node, $this->writeMetadata);
            $class = $this->dm->getClassMetadata($type);
        } else {
            if (isset($documentName)) {
                $class = $this->dm->getClassMetadata($documentName);
            } else if (isset($properties['phpcr:class'])) {
                $class = $this->dm->getClassMetadata($properties['phpcr:class']);
            } else if (isset($properties['phpcr:alias'])) {
                $class = $this->dm->getMetadataFactory()->getMetadataForAlias($properties['phpcr:alias']);
            }

            if ($this->writeMetadata && empty($properties['phpcr:class']) && isset($class)) {
                $node->setProperty('phpcr:class', $class->name, PropertyType::STRING);
            }
        }

        if (empty($class)) {
            throw new \InvalidArgumentException("Could not determine Doctrine metadata for node");
        }

        $documentState = array();
        $nonMappedData = array();
        $id = $node->getPath();

        foreach ($class->fieldMappings as $fieldName => $mapping) {
            if (isset($properties[$mapping['name']])) {
                if ($mapping['multivalue']) {
                    $documentState[$fieldName] = new ArrayCollection($properties[$mapping['name']]);
                } else {
                    $documentState[$fieldName] = $properties[$mapping['name']];
                }
            }
        }

        if ($class->node) {
            $documentState[$class->node] = $node;
{'fraction_non_alphanumeric': 0.12590571988747762, 'fraction_numerical': 0.001022930696445316, 'mean_word_length': 2.4334211296458883, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '2122147', 'n_tokens_mistral': 3357, 'n_tokens_neox': 3165, 'n_words': 910}