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

Conversation

mgonzalezbaile
Copy link
Contributor

Issue #1509

Notes: Per suggested by @malarzm here it is another approach from PR #1510 avoiding reflection to instantiate a Type.

@malarzm
Copy link
Member

malarzm commented Oct 18, 2016

I'm personally fine with such addition (also introducing this method in a trait wanders through my mind), if we're collectively fine (ping @alcaeus @jmikola @jwage) there are few things to do:

  1. License header is missing in new class
  2. I'd add a PHPDoc for a class mentioning why is it there (and why one should consider inlining the code anyway)
  3. Add documentation

Anyways thanks @mgonzalezbaile for throwing the idea :)

@mgonzalezbaile
Copy link
Contributor Author

I guess I should wait for confirmation before starting working on the task list given by @malarzm, right?

@malarzm
Copy link
Member

malarzm commented Oct 27, 2016

Pinged folks on IRC, please bear with us a little longer @mgonzalezbaile

$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 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.


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()?

@alcaeus
Copy link
Member

alcaeus commented Oct 27, 2016

I'm 👍 on moving this to a trait and 👍 in general as a fix to basically writing the logic twice for every type.

Since we've already talked about changing the types a bit for 2.0 (parameters for types come to mind, e.g. immutable=true/false for DateType), and I would like to add the closure handling to that list. Currently, a type closure can access all document data as well as the finished object during hydration and modify it at will. I know performance is a key issue here, but we also need to limit the scope a bit here. As it is, a type can do a lot of things during document hydration.

@mgonzalezbaile
Copy link
Contributor Author

I think I applied all your comments, let me know if something is missing :)

Copy link
Member

@malarzm malarzm left a comment

Choose a reason for hiding this comment

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

Last comments from my side :)

@@ -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


return sprintf('
$type = \%s::getType($typeIdentifier);
$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); ?

@mgonzalezbaile
Copy link
Contributor Author

@malarzm do you think we can merge?

@malarzm
Copy link
Member

malarzm commented Nov 22, 2016

Ah crap @mgonzalezbaile I'm sorry, this fell through the cracks due to intensive season in my normal work... Anyways when I was about to hit "Merge" button last time I realized that we need to change CustomType trait name... At that time I (and generally after brief chat on IRC: we) had no idea how to name it better and the only good proposition came from @jmikola to name if after method it provides: ClosureToPHP - do you maybe have some better idea?

Also we'll need to add a note in the docs so people will know such feature is in place, but this can be done by us in a follow up PR to speed things up.

@mgonzalezbaile
Copy link
Contributor Author

@malarzm just renamed the trait :-)

@malarzm malarzm added this to the 1.2 milestone Dec 7, 2016
@malarzm
Copy link
Member

malarzm commented Dec 7, 2016

@mgonzalezbaile thanks! I'll merge this prob later tonight

malarzm added a commit that referenced this pull request Dec 12, 2016
@malarzm
Copy link
Member

malarzm commented Dec 12, 2016

Merged manually in 44e1b92 - thanks @mgonzalezbaile!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants