Permalink
Browse files

Add YAML Support and tests

  • Loading branch information...
beberlei committed May 21, 2011
1 parent 8c046b4 commit 8fa9d69c73ce9a2391b135f0d38efa4b9f7bd2a2
View
@@ -4,3 +4,6 @@
[submodule "lib/vendor/Symfony/Component/Console"]
path = lib/vendor/Symfony/Component/Console
url = git://github.com/symfony/Console.git
+[submodule "lib/vendor/Symfony/Component/Yaml"]
+ path = lib/vendor/Symfony/Component/Yaml
+ url = git://github.com/symfony/Yaml.git
@@ -20,7 +20,8 @@
namespace Doctrine\ODM\CouchDB\Mapping\Driver;
use Doctrine\ODM\CouchDB\Mapping\ClassMetadata,
- SimpleXmlElement;
+ SimpleXmlElement,
+ Doctrine\ODM\CouchDB\Mapping\MappingException;
/**
* XmlDriver is a metadata driver that enables mapping through XML files.
@@ -69,7 +70,7 @@ public function loadMetadataForClass($className, ClassMetadata $class)
'jsonName' => (isset($fieldMapping['json-name'])) ? (string)$fieldMapping['json-name'] : null,
'indexed' => (isset($fieldMapping['indexed'])) ? (bool)$fieldMapping['indexed'] : false,
'type' => (isset($fieldMapping['type'])) ? (string)$fieldMapping['type'] : null,
- 'version' => (isset($fieldMapping['version'])) ? (bool)$fieldMapping['version'] : null,
+ 'isVersionField' => (isset($fieldMapping['version'])) ? true : null,
));
}
}
@@ -78,7 +79,6 @@ public function loadMetadataForClass($className, ClassMetadata $class)
foreach ($xmlRoot->id as $idElement) {
$class->mapField(array(
'fieldName' => (string)$idElement['name'],
- 'jsonName' => (isset($idElement['json-name'])) ? (string)$idElement['json-name'] : null,
'indexed' => (isset($idElement['indexed'])) ? (bool)$idElement['indexed'] : false,
'type' => (isset($idElement['type'])) ? (string)$idElement['type'] : null,
'id' => true,
@@ -19,7 +19,8 @@
namespace Doctrine\ODM\CouchDB\Mapping\Driver;
-use Doctrine\ODM\CouchDB\Mapping\ClassMetadata;
+use Doctrine\ODM\CouchDB\Mapping\ClassMetadata,
+ Doctrine\ODM\CouchDB\Mapping\MappingException;
/**
* The YamlDriver reads the mapping metadata from yaml schema files.
@@ -46,6 +47,92 @@ public function loadMetadataForClass($className, ClassMetadata $class)
{
$element = $this->getElement($className);
+ if ($element['type'] == 'document') {
+ $class->setCustomRepositoryClass(
+ (isset($element['repositoryClass'])) ? $element['repositoryClass'] : null
+ );
+ if (isset($element['indexed']) && $element['indexed'] == true) {
+ $class->indexed = true;
+ }
+ } else if ($element['type'] == 'embedded') {
+ $class->isEmbeddedDocument = true;
+ } else {
+ throw MappingException::classIsNotAValidDocument($className);
+ }
+
+ if (isset($element['id'])) {
+ foreach ($element['id'] AS $fieldName => $idElement) {
+ $class->mapField(array(
+ 'fieldName' => $fieldName,
+ 'indexed' => (isset($idElement['indexed'])) ? (bool)$idElement['indexed'] : false,
+ 'type' => (isset($idElement['type'])) ? $idElement['type'] : null,
+ 'id' => true,
+ 'strategy' => (isset($idElement['strategy'])) ? $idElement['strategy'] : null,
+ ));
+ }
+ }
+
+ if (isset($element['fields'])) {
+ foreach ($element['fields'] AS $fieldName => $fieldElement) {
+ $class->mapField(array(
+ 'fieldName' => $fieldName,
+ 'jsonName' => (isset($fieldElement['jsonName'])) ? $fieldElement['jsonName'] : null,
+ 'indexed' => (isset($fieldElement['indexed'])) ? (bool)$fieldElement['indexed'] : false,
+ 'type' => (isset($fieldElement['type'])) ? $fieldElement['type'] : null,
+ 'isVersionField' => (isset($fieldElement['version'])) ? true : null,
+ ));
+ }
+ }
+
+
+ if (isset($element['referenceOne'])) {
+ foreach ($element['referenceOne'] AS $field => $referenceOneElement) {
+ $class->mapManyToOne(array(
+ 'cascade' => (isset($referenceManyElement->cascade)) ? $this->getCascadeMode($referenceManyElement->cascade) : 0,
+ 'targetDocument' => (string)$referenceOneElement['targetDocument'],
+ 'fieldName' => $field,
+ 'jsonName' => (isset($referenceOneElement['jsonName'])) ? (string)$referenceOneElement['jsonName'] : null,
+ ));
+ }
+ }
+
+ if (isset($element['referenceMany'])) {
+ foreach ($element['referenceMany'] AS $field => $referenceManyElement) {
+ $class->mapManyToMany(array(
+ 'cascade' => (isset($referenceManyElement->cascade)) ? $this->getCascadeMode($referenceManyElement->cascade) : 0,
+ 'targetDocument' => (string)$referenceManyElement['targetDocument'],
+ 'fieldName' => $field,
+ 'jsonName' => (isset($referenceManyElement['jsonName'])) ? (string)$referenceManyElement['jsonName'] : null,
+ 'mappedBy' => (isset($referenceManyElement['mappedBy'])) ? (string)$referenceManyElement['mappedBy'] : null,
+ ));
+ }
+ }
+
+ if (isset($element['attachments'])) {
+ $class->mapAttachments($element['attachments']);
+ }
+
+ if (isset($element['embedOne'])) {
+ foreach ($element['embedOne'] AS $field => $embedOneElement) {
+ $class->mapEmbedded(array(
+ 'targetDocument' => (string)$embedOneElement['targetDocument'],
+ 'fieldName' => $field,
+ 'jsonName' => (isset($embedOneElement['jsonName'])) ? (string)$embedOneElement['jsonName'] : null,
+ 'embedded' => 'one',
+ ));
+ }
+ }
+
+ if (isset($element['embedMany'])) {
+ foreach ($element['embedMany'] AS $field => $embedManyElement) {
+ $class->mapEmbedded(array(
+ 'targetDocument' => (string)$embedManyElement['targetDocument'],
+ 'fieldName' => $field,
+ 'jsonName' => (isset($embedManyElement['jsonName'])) ? (string)$embedManyElement['jsonName'] : null,
+ 'embedded' => 'many',
+ ));
+ }
+ }
}
protected function loadMappingFile($file)
Submodule Yaml added at 9a5dc4
@@ -0,0 +1,14 @@
+<?php
+
+namespace Doctrine\Tests\ODM\CouchDB\Mapping;
+
+use Doctrine\ODM\CouchDB\Mapping\ClassMetadata;
+use Doctrine\ODM\CouchDB\Mapping\Driver\YamlDriver;
+
+class YamlDriverTest extends AbstractMappingDriverTest
+{
+ protected function loadDriver()
+ {
+ return new YamlDriver(array(__DIR__."/yml"));
+ }
+}
@@ -0,0 +1,23 @@
+Doctrine\Tests\Models\CMS\CmsUser:
+ type: document
+ id:
+ id: ~
+ fields:
+ status:
+ type: string
+ username:
+ type: string
+ name:
+ type: string
+ referenceOne:
+ rights:
+ targetDocument: CmsUserRights
+ referenceMany:
+ articles:
+ targetDocument: CmsArticle
+ mappedBy: user
+ groups:
+ targetDocument: CmsGroup
+ embedOne:
+ address: ~
+ attachments: attachments
View
@@ -14,4 +14,7 @@
$classLoader->register();
$classLoader = new ClassLoader('Documents', __DIR__);
+$classLoader->register();
+
+$classLoader = new ClassLoader('Symfony\Component', __DIR__ . '/../lib/vendor');
$classLoader->register();

0 comments on commit 8fa9d69

Please sign in to comment.