Skip to content

Commit

Permalink
Merge pull request #599 from dantleech/map_depth
Browse files Browse the repository at this point in the history
Added depth mapping
  • Loading branch information
dbu committed Mar 2, 2015
2 parents a593b1f + 2b22df9 commit 8210438
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 1 deletion.
6 changes: 6 additions & 0 deletions doctrine-phpcr-odm-mapping.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
<xs:element name="version-created" type="phpcr:version-created" minOccurs="0" maxOccurs="1"/>
<xs:element name="parent-document" type="phpcr:parent-document" minOccurs="0" maxOccurs="1"/>
<xs:element name="child" type="phpcr:child" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="depth" type="phpcr:depth" minOccurs="0" maxOccurs="1"/>
<xs:element name="children" type="phpcr:children" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="reference-one" type="phpcr:reference-one" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="reference-many" type="phpcr:reference-many" minOccurs="0" maxOccurs="unbounded" />
Expand Down Expand Up @@ -173,6 +174,11 @@
<xs:anyAttribute namespace="##other"/>
</xs:complexType>

<xs:complexType name="depth">
<xs:attribute name="name" type="xs:NMTOKEN" use="required" />
<xs:anyAttribute namespace="##other"/>
</xs:complexType>

<xs:simpleType name="generator-strategy">
<xs:restriction base="xs:token">
<xs:enumeration value="REPOSITORY"/>
Expand Down
Binary file added lib/Doctrine/ODM/PHPCR/.UnitOfWork.php.swl
Binary file not shown.
30 changes: 30 additions & 0 deletions lib/Doctrine/ODM/PHPCR/Mapping/Annotations/Depth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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 MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ODM\PHPCR\Mapping\Annotations;

use Doctrine\Common\Annotations\Annotation;

/**
* @Annotation
* @Target("PROPERTY")
*/
final class Depth
{
}
22 changes: 22 additions & 0 deletions lib/Doctrine/ODM/PHPCR/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ class ClassMetadata implements ClassMetadataInterface
*/
public $localeMapping;

/**
* READ-ONLY: Name of the depth property
*
* @var string
*/
public $depthMapping;

/**
* READ-ONLY: Name of the version name property of this document
*
Expand Down Expand Up @@ -767,6 +774,13 @@ public function mapLocale(array $mapping, ClassMetadata $inherited = null)
$this->localeMapping = $mapping['fieldName'];
}

public function mapDepth(array $mapping, ClassMetadata $inherited = null)
{
$mapping['type'] = 'depth';
$mapping = $this->validateAndCompleteFieldMapping($mapping, $inherited, false, false);
$this->depthMapping = $mapping['fieldName'];
}

public function mapVersionName(array $mapping, ClassMetadata $inherited = null)
{
$mapping['type'] = 'versionname';
Expand Down Expand Up @@ -1118,6 +1132,7 @@ public function hasField($fieldName)
|| isset($this->inheritedFields[$fieldName])
|| $this->identifier === $fieldName
|| $this->localeMapping === $fieldName
|| $this->depthMapping === $fieldName
|| $this->node === $fieldName
|| $this->nodename === $fieldName
|| $this->versionNameField === $fieldName
Expand Down Expand Up @@ -1196,6 +1211,9 @@ public function getFieldNames()
if ($this->localeMapping) {
$fields[] = $this->localeMapping;
}
if ($this->depthMapping) {
$fields[] = $this->depthMapping;
}
if ($this->node) {
$fields[] = $this->node;
}
Expand Down Expand Up @@ -1448,6 +1466,10 @@ public function __sleep()
$serialized[] = 'localeMapping';
}

if ($this->depthMapping) {
$serialized[] = 'depthMapping';
}

if ($this->translator) {
$serialized[] = 'translator';
}
Expand Down
3 changes: 3 additions & 0 deletions lib/Doctrine/ODM/PHPCR/Mapping/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
} elseif ($fieldAnnot instanceof ODM\Locale) {
$mapping = array_merge($mapping, (array) $fieldAnnot);
$metadata->mapLocale($mapping);
} elseif ($fieldAnnot instanceof ODM\Depth) {
$mapping = array_merge($mapping, (array) $fieldAnnot);
$metadata->mapDepth($mapping);
} elseif ($fieldAnnot instanceof ODM\VersionName) {
$mapping = array_merge($mapping, (array) $fieldAnnot);
$metadata->mapVersionName($mapping);
Expand Down
4 changes: 4 additions & 0 deletions lib/Doctrine/ODM/PHPCR/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ public function loadMetadataForClass($className, ClassMetadata $class)
$class->mapLocale(array('fieldName' => (string) $xmlRoot->locale->attributes()->name));
}

if (isset($xmlRoot->depth)) {
$class->mapDepth(array('fieldName' => (string) $xmlRoot->depth->attributes()->name));
}

if (isset($xmlRoot->{'mixed-referrers'})) {
foreach ($xmlRoot->{'mixed-referrers'} as $mixedReferrers) {
$attributes = $mixedReferrers->attributes();
Expand Down
3 changes: 3 additions & 0 deletions lib/Doctrine/ODM/PHPCR/Mapping/Driver/YamlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ public function loadMetadataForClass($className, ClassMetadata $class)
$class->mapLocale(array('fieldName' => $element['locale']));
}

if (isset($element['depth'])) {
$class->mapDepth(array('fieldName' => $element['depth']));
}

if (isset($element['mixedReferrers'])) {
foreach ($element['mixedReferrers'] as $name => $attributes) {
Expand Down
10 changes: 9 additions & 1 deletion lib/Doctrine/ODM/PHPCR/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,10 @@ public function getOrCreateDocuments($className, $nodes, array &$hints = array()
$documentState[$class->parentMapping] = $this->getOrCreateProxyFromNode($node->getParent(), $locale);
}

if ($class->depthMapping) {
$documentState[$class->depthMapping] = $node->getDepth();
}

foreach ($class->childMappings as $fieldName) {
$mapping = $class->mappings[$fieldName];
$documentState[$fieldName] = $node->hasNode($mapping['nodeName'])
Expand Down Expand Up @@ -2712,7 +2716,7 @@ private function executeMoves($documents)

$this->session->move($sourcePath, $targetPath);

// update fields nodename and parentMapping if they exist in this type
// update fields nodename, parentMapping and depth if they exist in this type
$node = $this->session->getNode($targetPath); // get node from session, document class might not map it
if ($class->nodename) {
$class->setFieldValue($document, $class->nodename, $node->getName());
Expand All @@ -2722,6 +2726,10 @@ private function executeMoves($documents)
$class->setFieldValue($document, $class->parentMapping, $this->getOrCreateProxyFromNode($node->getParent(), $this->getCurrentLocale($document, $class)));
}

if ($class->depthMapping) {
$class->setFieldValue($document, $class->depthMapping, $node->getDepth());
}

// update all cached children of the document to reflect the move (path id changes)
foreach ($this->documentIds as $childOid => $id) {
if (0 !== strpos($id, $sourcePath)) {
Expand Down
30 changes: 30 additions & 0 deletions tests/Doctrine/Tests/ODM/PHPCR/Functional/BasicCrudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPCR\Util\UUIDHelper;

use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
use PHPCR\Util\NodeHelper;

/**
* @group functional
Expand Down Expand Up @@ -584,6 +585,23 @@ public function testVersionedDocument()
$this->assertEquals($user->numbers, $userNew->numbers);
}

public function testDepth()
{
$object = new DepthMappingObject();
$object->id = '/functional/test';
$this->dm->persist($object);
$this->dm->flush();
$this->dm->clear();

$object = $this->dm->find(null, '/functional/test');
$this->assertEquals(2, $object->depth);

NodeHelper::createPath($this->dm->getPhpcrSession(), '/functional/newtest/foobar');
$this->dm->move($object, '/functional/newtest/foobar/test');
$this->dm->flush();
$this->assertEquals(4, $object->depth);
}

/**
* Create a node with a bad name and explicitly persist it without
* adding it to any parent's children collection.
Expand Down Expand Up @@ -762,3 +780,15 @@ class UserWithUuid extends User
/** @PHPCRODM\Uuid */
public $uuid;
}

/**
* @PHPCRODM\Document
*/
class DepthMappingObject
{
/** @PHPCRODM\Id */
public $id;

/** @PHPCRODM\Depth */
public $depth;
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ class Testclass
public $node;
/** @PHPCRODM\String */
public $text;
/** @PHPCRODM\Depth */
public $depth;
public $callback_run = 0;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,22 @@ public function testParentDocumentMapping($class)
$this->assertEquals('parent', $class->parentMapping);
}

public function testLoadDepthMapping()
{
$className = 'Doctrine\Tests\ODM\PHPCR\Mapping\Model\DepthMappingObject';
return $this->loadMetadataForClassname($className);
}

/**
* @depends testLoadDepthMapping
* @param ClassMetadata $class
*/
public function testDepthMapping($class)
{
$this->assertTrue(isset($class->depthMapping));
$this->assertEquals('depth', $class->depthMapping);
}

public function testParentWithPrivatePropertyMapping()
{
$className = 'Doctrine\Tests\ODM\PHPCR\Mapping\Model\ParentWithPrivatePropertyObject';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Doctrine\Tests\ODM\PHPCR\Mapping\Model;

use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;

/**
* A class that contains a mapped parent document via properties
*
* @PHPCRODM\Document
*/
class DepthMappingObject
{
/** @PHPCRODM\Id */
public $id;

/** @PHPCRODM\Depth */
public $depth;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/phpcr-odm/phpcr-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/phpcr-odm/phpcr-mapping
https://github.com/doctrine/phpcr-odm/raw/master/doctrine-phpcr-odm-mapping.xsd">
<document name="Doctrine\Tests\ODM\PHPCR\Mapping\Model\DepthMappingObject">
<id name="id" />
<depth name="depth" />
</document>
</doctrine-mapping>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Doctrine\Tests\ODM\PHPCR\Mapping\Model\DepthMappingObject:
id: id
depth: depth

0 comments on commit 8210438

Please sign in to comment.