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

Add CustomType class to let use convertToPHPValue method instead of closureToPHP (no reflection) #1513

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/Doctrine/ODM/MongoDB/Hydrator/HydratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ private function generateHydratorClass(ClassMetadata $class, $hydratorClassName,
if (isset(\$data['%1\$s']) || (! empty(\$this->class->fieldMappings['%2\$s']['nullable']) && array_key_exists('%1\$s', \$data))) {
\$value = \$data['%1\$s'];
if (\$value !== null) {
\$typeIdentifier = \$this->class->fieldMappings['%2\$s']['type'];
%3\$s
} else {
\$return = null;
Expand Down
18 changes: 18 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Types/CustomType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add licensing, you can copy it from any other file

namespace Doctrine\ODM\MongoDB\Types;

class CustomType extends Type
{
/**
* @return string Redirects to the method convertToPHPValue from child class
*/
final public function closureToPHP()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(also introducing this method in a trait wanders through my mind)

@malarzm: A trait may allow users more flexibility than forcing them to extend a new CustomType base class.

{
$fqcn = self::class;

return sprintf('
$type = \%s::getType($typeIdentifier);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to call Doctrine\ODM\MongoDB\Types\Type::getType() directly here, or do we expect that it might be overridden (the method isn't final)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't expect anyone to override the main method but we can't slap final

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH, we're always using Doctrine\ODM\MongoDB\Types\Type::getType() directly to fetch a type, so I'd do the same here.

$return = $type->convertToPHPValue($value);', $fqcn);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this return sprintf('$return = \%s::getType($typeIdentifier)->convertToPHPValue($value);', Type::class); ?

}
}
38 changes: 15 additions & 23 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/CustomTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\ODM\MongoDB\Tests\Functional;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Types\CustomType;
use Doctrine\ODM\MongoDB\Types\Type;

class CustomTypeTest extends \Doctrine\ODM\MongoDB\Tests\BaseTest
Expand Down Expand Up @@ -40,7 +41,7 @@ public function testConvertToDatabaseValueExpectsArray()
}
}

class DateCollectionType extends Type
class DateCollectionType extends CustomType
{
// Note: this method is called by PersistenceBuilder
public function convertToDatabaseValue($value)
Expand All @@ -62,37 +63,28 @@ public function convertToDatabaseValue($value)
return $value;
}

// Note: this method is never called for non-identifier fields
public function convertToPHPValue($value)
{
if ($value === null) {
return null;
}

if (!is_array($value)) {
throw new CustomTypeException('Array expected.');
}

$converter = Type::getType('date');

$value = array_map(function($date) use ($converter) {
return $converter->convertToPHPValue($date);
}, array_values($value));

return $value;
return array_map(
function ($v) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a particular reason to not rely on Doctrine\ODM\MongoDB\Types::convertToPHPValue() here, as the original code was doing with $converter->convertToPHPValue()?

if ($v instanceof \MongoDate) {
$date = new \DateTime();
$date->setTimestamp($v->sec);

return $date;
} else {
return new \DateTime($v);
}
},
$value
);
}

// Note: this method is never called
public function closureToMongo()
{
return '$return = array_map(function($v) { if ($v instanceof \DateTime) { $v = $v->getTimestamp(); } else if (is_string($v)) { $v = strtotime($v); } return new \MongoDate($v); }, $value);';
}

// Note: this code ends up in the generated hydrator class (via HydratorFactory)
public function closureToPHP()
{
return '$return = array_map(function($v) { if ($v instanceof \MongoDate) { $date = new \DateTime(); $date->setTimestamp($v->sec); return $date; } else { return new \DateTime($v); } }, $value);';
}
}

class CustomTypeException extends \Exception
Expand Down