Skip to content

Commit 2b22df9

Browse files
committed
Added depth mapping
1 parent c581594 commit 2b22df9

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
@@ -256,6 +256,13 @@ class ClassMetadata implements ClassMetadataInterface
256256
*/
257257
public $localeMapping;
258258

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

776+
public function mapDepth(array $mapping, ClassMetadata $inherited = null)
777+
{
778+
$mapping['type'] = 'depth';
779+
$mapping = $this->validateAndCompleteFieldMapping($mapping, $inherited, false, false);
780+
$this->depthMapping = $mapping['fieldName'];
781+
}
782+
769783
public function mapVersionName(array $mapping, ClassMetadata $inherited = null)
770784
{
771785
$mapping['type'] = 'versionname';
@@ -1117,6 +1131,7 @@ public function hasField($fieldName)
11171131
|| isset($this->inheritedFields[$fieldName])
11181132
|| $this->identifier === $fieldName
11191133
|| $this->localeMapping === $fieldName
1134+
|| $this->depthMapping === $fieldName
11201135
|| $this->node === $fieldName
11211136
|| $this->nodename === $fieldName
11221137
|| $this->versionNameField === $fieldName
@@ -1198,6 +1213,9 @@ public function getFieldNames()
11981213
if ($this->localeMapping) {
11991214
$fields[] = $this->localeMapping;
12001215
}
1216+
if ($this->depthMapping) {
1217+
$fields[] = $this->depthMapping;
1218+
}
12011219
if ($this->node) {
12021220
$fields[] = $this->node;
12031221
}
@@ -1450,6 +1468,10 @@ public function __sleep()
14501468
$serialized[] = 'localeMapping';
14511469
}
14521470

1471+
if ($this->depthMapping) {
1472+
$serialized[] = 'depthMapping';
1473+
}
1474+
14531475
if ($this->translator) {
14541476
$serialized[] = 'translator';
14551477
}

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
@@ -543,6 +543,10 @@ public function getOrCreateDocuments($className, $nodes, array &$hints = array()
543543
$documentState[$class->parentMapping] = $this->getOrCreateProxyFromNode($node->getParent(), $locale);
544544
}
545545

546+
if ($class->depthMapping) {
547+
$documentState[$class->depthMapping] = $node->getDepth();
548+
}
549+
546550
foreach ($class->childMappings as $fieldName) {
547551
$mapping = $class->mappings[$fieldName];
548552
$documentState[$fieldName] = $node->hasNode($mapping['nodeName'])
@@ -2703,7 +2707,7 @@ private function executeMoves($documents)
27032707

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

2706-
// update fields nodename and parentMapping if they exist in this type
2710+
// update fields nodename, parentMapping and depth if they exist in this type
27072711
$node = $this->session->getNode($targetPath); // get node from session, document class might not map it
27082712
if ($class->nodename) {
27092713
$class->setFieldValue($document, $class->nodename, $node->getName());
@@ -2713,6 +2717,10 @@ private function executeMoves($documents)
27132717
$class->setFieldValue($document, $class->parentMapping, $this->getOrCreateProxyFromNode($node->getParent(), $this->getCurrentLocale($document, $class)));
27142718
}
27152719

2720+
if ($class->depthMapping) {
2721+
$class->setFieldValue($document, $class->depthMapping, $node->getDepth());
2722+
}
2723+
27162724
// update all cached children of the document to reflect the move (path id changes)
27172725
foreach ($this->documentIds as $childOid => $id) {
27182726
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)