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

__toString() in UuidInterface #111

Closed
danizord opened this issue Mar 27, 2016 · 10 comments
Closed

__toString() in UuidInterface #111

danizord opened this issue Mar 27, 2016 · 10 comments

Comments

@danizord
Copy link

Oh, I've heard on Twitter something about 4.0 :P

So, speaking about that, there's something that always bother me. I like to cast UUID objects to string for persistence and representation, but there's no __toString() method in UuidInterface.

Ok, I could typehint against Uuid class, but if I do that, PHPStorm complains that return type of Uuid::uuid4() (UuidInterface) is not compatible with the expected type (Uuid).

So, could you add __toString() method in UuidInterface for next 4.0 version?

@jmauerhan
Copy link
Contributor

It's typically best in OOP to pass around objects rather than scalar values, so using the object instead of the string is a good idea, and you can typehint against the UuidInterface instead of Uuid, and you won't get that error message.

UuidInterface already has toString which is what Uuid::__toString calls. Maybe that will help? :)

@ramsey
Copy link
Owner

ramsey commented Mar 27, 2016

No promises right now, but I'll consider the implications of adding __toString() to the UuidInterface to ensure that all UUIDs can be treated like a string.

@ramsey ramsey added this to the Version 4.0.0 milestone Mar 27, 2016
@danizord
Copy link
Author

Hey @jmauerhan

It's typically best in OOP to pass around objects rather than scalar values, so using the object instead of the string is a good idea.

Yeah, I pass objects around, but for serialization and representation in HTTP context I need to cast it to string. That's why all value objects that can be serialized to string tend to have a __toString() method.

you can typehint against the UuidInterface instead of Uuid, and you won't get that error message

But if I do that, I can't cast it to string because I can't make sure that __toString() method is implemented.

UuidInterface already has toString which is what Uuid::__toString calls. Maybe that will help? :)

That's what I do currently, but (string) $id just feels better :P so I'd like to have it possible in 4.0.

@nicholasruunu
Copy link

This would potentially fix a problem I have with api-platform where it doesn't know what to do with UuidInterface return values.
Would also possibly simplify integrations similar types of projects.

@sserbin
Copy link

sserbin commented Apr 11, 2018

No promises right now, but I'll consider the implications of adding __toString() to the UuidInterface to ensure that all UUIDs can be treated like a string.

@ramsey any update on this? Would be nice to finally have this

@ramsey
Copy link
Owner

ramsey commented Apr 11, 2018

I'll add __toString() to version 4, but I can't add it to the interface on version 3.x, since that might break a lot of implementations.

I plan to make a big push to finish up all the things I want to implement for version 4 over the next couple of months. I'd like to have it out by the end of June.

@ramsey ramsey closed this as completed in 5cadea8 Aug 12, 2018
@Yamakasi
Copy link

I'm just looking into this as well, what is the right usage for Typehinting when you have a HEX instead if a dashed UUID ?

@ramsey
Copy link
Owner

ramsey commented Aug 17, 2018

When you cast the Uuid object to a string, it is presented in the dashed, hexadecimal format. For type-hinting, it is a string.

@Yamakasi
Copy link

Yamakasi commented Aug 18, 2018

OK, the class itself doesn't check for a valid string, that could be done on the construct, etc so you can typehint Uuid.

Or is it too much overhead to do that ? Could be added simply and is all in your library then.

@uphlewis
Copy link

uphlewis commented Nov 8, 2018

@Yamakasi see Ramsey\Uuid\Exception\InvalidUuidStringException which gets thrown when you attempt to use Uuid::fromString() on an invalid Uuid

ramsey added a commit that referenced this issue Dec 15, 2019
ramsey added a commit that referenced this issue Dec 15, 2019
ramsey added a commit that referenced this issue Dec 17, 2019
ramsey added a commit that referenced this issue Jan 2, 2020
ramsey added a commit that referenced this issue Jan 8, 2020
ramsey added a commit that referenced this issue Jan 13, 2020
ramsey added a commit that referenced this issue Jan 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants