Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plural to singular conversion of "add" and "remove" methods #627

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -15,7 +15,8 @@
"php": ">=5.3.2",
"symfony/console": "~2.0",
"doctrine/common": ">=2.2.0,<2.5-dev",
"doctrine/mongodb": "1.0.*"
"doctrine/mongodb": "1.0.*",
"doctrine/inflector": "~1.0"
},
"require-dev": {
"symfony/yaml": "~2.0"
Expand Down
22 changes: 14 additions & 8 deletions lib/Doctrine/ODM/MongoDB/Tools/DocumentGenerator.php
Expand Up @@ -19,7 +19,7 @@

namespace Doctrine\ODM\MongoDB\Tools;

use Doctrine\Common\Util\Inflector;
use Doctrine\Common\Inflector\Inflector;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;

/**
Expand Down Expand Up @@ -701,12 +701,18 @@ private function generateDocumentFieldMappingProperties(ClassMetadataInfo $metad

private function generateDocumentStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null)
{
$methodName = $type . Inflector::classify($fieldName);

// TODO: This needs actual plural -> singular conversion
if (in_array($type, array('add', 'remove')) && substr($methodName, -1) == 's') {
$methodName = substr($methodName, 0, -1);
}
/* rewrite the field name in $reWrittenFieldName if it needs to be singularized,
* otherwise makes it equal to the original field name
* but keeps original $fieldName unchanged as it must be used as is by <fieldName> replacement.
*/
$reWrittenFieldName = ( in_array($type, array('add', 'remove')) )
? Inflector::singularize($fieldName)
: $fieldName;

$methodName = $type . Inflector::classify($reWrittenFieldName);
$variableName = Inflector::camelize($reWrittenFieldName);
$description = ucfirst($type) . ' ' . $variableName;

if ($this->hasMethod($methodName, $metadata)) {
return;
Expand All @@ -721,10 +727,10 @@ private function generateDocumentStubMethod(ClassMetadataInfo $metadata, $type,
$methodTypeHint = $typeHint && ! isset($types[$typeHint]) ? '\\' . $typeHint . ' ' : null;

$replacements = array(
'<description>' => ucfirst($type) . ' ' . $fieldName,
'<description>' => $description,
'<methodTypeHint>' => $methodTypeHint,
'<variableType>' => $variableType,
'<variableName>' => Inflector::camelize($fieldName),
'<variableName>' => $variableName,
'<methodName>' => $methodName,
'<fieldName>' => $fieldName,
);
Expand Down
14 changes: 11 additions & 3 deletions tests/Doctrine/ODM/MongoDB/Tests/Tools/DocumentGeneratorTest.php
Expand Up @@ -50,6 +50,10 @@ public function generateBookDocumentFixture()
'fieldName' => 'comments',
'targetDocument' => 'Doctrine\ODM\MongoDB\Tests\Tools\DocumentGeneratorComment'
));
$metadata->mapManyReference(array(
'fieldName' => 'searches',
'targetDocument' => 'Doctrine\ODM\MongoDB\Tests\Tools\DocumentGeneratorSearch'
));
$metadata->addLifecycleCallback('loading', 'postLoad');
$metadata->addLifecycleCallback('willBeRemoved', 'preRemove');
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
Expand Down Expand Up @@ -87,7 +91,10 @@ public function testGeneratedDocumentClass()
$this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getAuthor'), "DocumentGeneratorBook::getAuthor() missing.");
$this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getComments'), "DocumentGeneratorBook::getComments() missing.");
$this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'addComment'), "DocumentGeneratorBook::addComment() missing.");
$this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'removeComment'), "EntityGeneratorBook::removeComment() missing.");
$this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'removeComment'), "DocumentGeneratorBook::removeComment() missing.");
$this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'getSearches'), "DocumentGeneratorBook::getSearches() missing.");
$this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'addSearch'), "DocumentGeneratorBook::addSearch() missing.");
$this->assertTrue(method_exists($metadata->namespace . '\DocumentGeneratorBook', 'removeSearch'), "DocumentGeneratorBook::removeSearch() missing.");

$book->setName('Jonathan H. Wage');
$this->assertEquals('Jonathan H. Wage', $book->getName());
Expand All @@ -108,7 +115,7 @@ public function testDocumentUpdatingWorks()
{
$metadata = $this->generateBookDocumentFixture();
$metadata->mapField(array('fieldName' => 'test', 'type' => 'string'));

$this->generator->writeDocumentClass($metadata, $this->tmpDir);

$this->assertFileExists($this->tmpDir . "/" . $this->namespace . "/DocumentGeneratorBook.php");
Expand Down Expand Up @@ -186,4 +193,5 @@ public function testLoadPrefixedMetadata()
}

class DocumentGeneratorAuthor {}
class DocumentGeneratorComment {}
class DocumentGeneratorComment {}
class DocumentGeneratorSearch {}