Skip to content

Commit 8210438

Browse files
committed
Merge pull request #599 from dantleech/map_depth
Added depth mapping
2 parents a593b1f + 2b22df9 commit 8210438

14 files changed

+157
-1
lines changed

doctrine-phpcr-odm-mapping.xsd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
<xs:element name="version-created" type="phpcr:version-created" minOccurs="0" maxOccurs="1"/>
147147
<xs:element name="parent-document" type="phpcr:parent-document" minOccurs="0" maxOccurs="1"/>
148148
<xs:element name="child" type="phpcr:child" minOccurs="0" maxOccurs="unbounded"/>
149+
<xs:element name="depth" type="phpcr:depth" minOccurs="0" maxOccurs="1"/>
149150
<xs:element name="children" type="phpcr:children" minOccurs="0" maxOccurs="unbounded"/>
150151
<xs:element name="reference-one" type="phpcr:reference-one" minOccurs="0" maxOccurs="unbounded"/>
151152
<xs:element name="reference-many" type="phpcr:reference-many" minOccurs="0" maxOccurs="unbounded" />
@@ -173,6 +174,11 @@
173174
<xs:anyAttribute namespace="##other"/>
174175
</xs:complexType>
175176

177+
<xs:complexType name="depth">
178+
<xs:attribute name="name" type="xs:NMTOKEN" use="required" />
179+
<xs:anyAttribute namespace="##other"/>
180+
</xs:complexType>
181+
176182
<xs:simpleType name="generator-strategy">
177183
<xs:restriction base="xs:token">
178184
<xs:enumeration value="REPOSITORY"/>
188 KB
Binary file not shown.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/*
3+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14+
*
15+
* This software consists of voluntary contributions made by many individuals
16+
* and is licensed under the MIT license. For more information, see
17+
* <http://www.doctrine-project.org>.
18+
*/
19+
20+
namespace Doctrine\ODM\PHPCR\Mapping\Annotations;
21+
22+
use Doctrine\Common\Annotations\Annotation;
23+
24+
/**
25+
* @Annotation
26+
* @Target("PROPERTY")
27+
*/
28+
final class Depth
29+
{
30+
}

lib/Doctrine/ODM/PHPCR/Mapping/ClassMetadata.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ class ClassMetadata implements ClassMetadataInterface
257257
*/
258258
public $localeMapping;
259259

260+
/**
261+
* READ-ONLY: Name of the depth property
262+
*
263+
* @var string
264+
*/
265+
public $depthMapping;
266+
260267
/**
261268
* READ-ONLY: Name of the version name property of this document
262269
*
@@ -767,6 +774,13 @@ public function mapLocale(array $mapping, ClassMetadata $inherited = null)
767774
$this->localeMapping = $mapping['fieldName'];
768775
}
769776

777+
public function mapDepth(array $mapping, ClassMetadata $inherited = null)
778+
{
779+
$mapping['type'] = 'depth';
780+
$mapping = $this->validateAndCompleteFieldMapping($mapping, $inherited, false, false);
781+
$this->depthMapping = $mapping['fieldName'];
782+
}
783+
770784
public function mapVersionName(array $mapping, ClassMetadata $inherited = null)
771785
{
772786
$mapping['type'] = 'versionname';
@@ -1118,6 +1132,7 @@ public function hasField($fieldName)
11181132
|| isset($this->inheritedFields[$fieldName])
11191133
|| $this->identifier === $fieldName
11201134
|| $this->localeMapping === $fieldName
1135+
|| $this->depthMapping === $fieldName
11211136
|| $this->node === $fieldName
11221137
|| $this->nodename === $fieldName
11231138
|| $this->versionNameField === $fieldName
@@ -1196,6 +1211,9 @@ public function getFieldNames()
11961211
if ($this->localeMapping) {
11971212
$fields[] = $this->localeMapping;
11981213
}
1214+
if ($this->depthMapping) {
1215+
$fields[] = $this->depthMapping;
1216+
}
11991217
if ($this->node) {
12001218
$fields[] = $this->node;
12011219
}
@@ -1448,6 +1466,10 @@ public function __sleep()
14481466
$serialized[] = 'localeMapping';
14491467
}
14501468

1469+
if ($this->depthMapping) {
1470+
$serialized[] = 'depthMapping';
1471+
}
1472+
14511473
if ($this->translator) {
14521474
$serialized[] = 'translator';
14531475
}

lib/Doctrine/ODM/PHPCR/Mapping/Driver/AnnotationDriver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
156156
} elseif ($fieldAnnot instanceof ODM\Locale) {
157157
$mapping = array_merge($mapping, (array) $fieldAnnot);
158158
$metadata->mapLocale($mapping);
159+
} elseif ($fieldAnnot instanceof ODM\Depth) {
160+
$mapping = array_merge($mapping, (array) $fieldAnnot);
161+
$metadata->mapDepth($mapping);
159162
} elseif ($fieldAnnot instanceof ODM\VersionName) {
160163
$mapping = array_merge($mapping, (array) $fieldAnnot);
161164
$metadata->mapVersionName($mapping);

lib/Doctrine/ODM/PHPCR/Mapping/Driver/XmlDriver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ public function loadMetadataForClass($className, ClassMetadata $class)
190190
$class->mapLocale(array('fieldName' => (string) $xmlRoot->locale->attributes()->name));
191191
}
192192

193+
if (isset($xmlRoot->depth)) {
194+
$class->mapDepth(array('fieldName' => (string) $xmlRoot->depth->attributes()->name));
195+
}
196+
193197
if (isset($xmlRoot->{'mixed-referrers'})) {
194198
foreach ($xmlRoot->{'mixed-referrers'} as $mixedReferrers) {
195199
$attributes = $mixedReferrers->attributes();

lib/Doctrine/ODM/PHPCR/Mapping/Driver/YamlDriver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ public function loadMetadataForClass($className, ClassMetadata $class)
201201
$class->mapLocale(array('fieldName' => $element['locale']));
202202
}
203203

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

205208
if (isset($element['mixedReferrers'])) {
206209
foreach ($element['mixedReferrers'] as $name => $attributes) {

lib/Doctrine/ODM/PHPCR/UnitOfWork.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,10 @@ public function getOrCreateDocuments($className, $nodes, array &$hints = array()
550550
$documentState[$class->parentMapping] = $this->getOrCreateProxyFromNode($node->getParent(), $locale);
551551
}
552552

553+
if ($class->depthMapping) {
554+
$documentState[$class->depthMapping] = $node->getDepth();
555+
}
556+
553557
foreach ($class->childMappings as $fieldName) {
554558
$mapping = $class->mappings[$fieldName];
555559
$documentState[$fieldName] = $node->hasNode($mapping['nodeName'])
@@ -2712,7 +2716,7 @@ private function executeMoves($documents)
27122716

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

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

2729+
if ($class->depthMapping) {
2730+
$class->setFieldValue($document, $class->depthMapping, $node->getDepth());
2731+
}
2732+
27252733
// update all cached children of the document to reflect the move (path id changes)
27262734
foreach ($this->documentIds as $childOid => $id) {
27272735
if (0 !== strpos($id, $sourcePath)) {

tests/Doctrine/Tests/ODM/PHPCR/Functional/BasicCrudTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPCR\Util\UUIDHelper;
1010

1111
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
12+
use PHPCR\Util\NodeHelper;
1213

1314
/**
1415
* @group functional
@@ -584,6 +585,23 @@ public function testVersionedDocument()
584585
$this->assertEquals($user->numbers, $userNew->numbers);
585586
}
586587

588+
public function testDepth()
589+
{
590+
$object = new DepthMappingObject();
591+
$object->id = '/functional/test';
592+
$this->dm->persist($object);
593+
$this->dm->flush();
594+
$this->dm->clear();
595+
596+
$object = $this->dm->find(null, '/functional/test');
597+
$this->assertEquals(2, $object->depth);
598+
599+
NodeHelper::createPath($this->dm->getPhpcrSession(), '/functional/newtest/foobar');
600+
$this->dm->move($object, '/functional/newtest/foobar/test');
601+
$this->dm->flush();
602+
$this->assertEquals(4, $object->depth);
603+
}
604+
587605
/**
588606
* Create a node with a bad name and explicitly persist it without
589607
* adding it to any parent's children collection.
@@ -762,3 +780,15 @@ class UserWithUuid extends User
762780
/** @PHPCRODM\Uuid */
763781
public $uuid;
764782
}
783+
784+
/**
785+
* @PHPCRODM\Document
786+
*/
787+
class DepthMappingObject
788+
{
789+
/** @PHPCRODM\Id */
790+
public $id;
791+
792+
/** @PHPCRODM\Depth */
793+
public $depth;
794+
}

tests/Doctrine/Tests/ODM/PHPCR/Functional/Mapping/AnnotationMappingTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ class Testclass
211211
public $node;
212212
/** @PHPCRODM\String */
213213
public $text;
214+
/** @PHPCRODM\Depth */
215+
public $depth;
214216
public $callback_run = 0;
215217

216218
/**

0 commit comments

Comments
 (0)