-
-
Notifications
You must be signed in to change notification settings - Fork 508
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
Add CustomType class to let use convertToPHPValue method instead of closureToPHP (no reflection) #1513
Changes from 1 commit
07fa621
3498590
6e2be94
18e14e8
fd178d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Doctrine\ODM\MongoDB\Types; | ||
|
||
class CustomType extends Type | ||
{ | ||
/** | ||
* @return string Redirects to the method convertToPHPValue from child class | ||
*/ | ||
final public function closureToPHP() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to call There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TBH, we're always using |
||
$return = $type->convertToPHPValue($value);', $fqcn); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you make this |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a particular reason to not rely on |
||
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 | ||
|
There was a problem hiding this comment.
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