Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Uid] Add interface for getDateTime() and apply to relevant UIDs #47507

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

Closed
wants to merge 0 commits into from
Closed

[Uid] Add interface for getDateTime() and apply to relevant UIDs #47507

wants to merge 0 commits into from

Conversation

shrikeh
Copy link
Contributor

@shrikeh shrikeh commented Sep 7, 2022

Q A
Branch? 6.2
Bug fix? no
New feature? yes
Deprecations? no
Tickets N/A
License MIT
Doc PR Unclear how this would be documented.

Minor "feature" of better describing that certain flavours of UID have embedded DateTimeImmutable. The method getDateTime() is described in the interface, and applied to the relevant UID flavours. This allows usage of the functionality through simpler instanceof inspection, or as a typed property of a class representing an entity. A trivial example:

if ($uid instanceof DateTimeUidInterface) {
    return $uid->getDateTime();
}
// throw an Exception if not, or return a default DateTimeImmutable...

@nicolas-grekas
Copy link
Member

What about adding AbstractUid::getDateTime(): ?DateTimeImmutable instead?

@shrikeh
Copy link
Contributor Author

shrikeh commented Sep 7, 2022

What about adding AbstractUid::getDateTime(): ?DateTimeImmutable instead?

Potentially, though personally it would feel like an anti-pattern to me as the behaviour is specific to only certain implementations.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Sep 7, 2022

I'm not a fan of interfaces on value objects personally. I would prefer a new method on the abstract class because it keeps things simple, and it doesn't force any extra logic on consumers. Actually, nullable return values are easier to deal with thanks to nullsafe operators.

@derrabus
Copy link
Member

derrabus commented Sep 7, 2022

I like the interface, I must admit. It plays nicer with static analysis than a nullable type. We might want to find a better name though. TimeBasedUidInterface?

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Sep 7, 2022

AbstractTimedUid extends AbstractUid?

@nicolas-grekas
Copy link
Member

AbstractTimedUid extends AbstractUid?

Well, won't work since class Uuid already extends AbstractUid and not all UUIDs are time-based.

TimeBasedUidInterface is the correct name since we already have TimeBasedUuidFactory.

@nicolas-grekas
Copy link
Member

One more thing: we might want to declare TimeBasedUuidFactory::create() as returning Uuid&TimeBasedUidInterface

@carsonbot carsonbot changed the title Add interface for getDateTime() and apply to relevant UIDs [Uid] Add interface for getDateTime() and apply to relevant UIDs Sep 7, 2022
@shrikeh
Copy link
Contributor Author

shrikeh commented Sep 7, 2022

@nicolas-grekas is psalm failing on the intersection for TimeBasedUuidFactory::create()?

@nicolas-grekas
Copy link
Member

Looks like so 🤷
We're going to ignore it then.

@nicolas-grekas
Copy link
Member

Thank you @shrikeh.

nicolas-grekas added a commit that referenced this pull request Sep 7, 2022
…elevant UIDs (shrikeh)

This PR was squashed before being merged into the 6.2 branch.

Discussion
----------

[Uid] Add interface for `getDateTime()` and apply to relevant UIDs

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | N/A
| License       | MIT
| Doc PR        | Unclear how this would be documented.

Minor "feature" of better describing that certain flavours of UID have embedded DateTimeImmutable. The method `getDateTime()` is described in the interface, and applied to the relevant UID flavours. This allows usage of the functionality through simpler `instanceof` inspection, or as a typed property of a class representing an entity. A trivial example:

```php
if ($uid instanceof DateTimeUidInterface) {
    return $uid->getDateTime();
}
// throw an Exception if not, or return a default DateTimeImmutable...
```

Commits
-------

0f1bc36 [Uid] Add interface for `getDateTime()` and apply to relevant UIDs
@shrikeh shrikeh deleted the feature/add-interface-to-describe-uids-with-datetime-behaviour branch September 7, 2022 15:38
@fabpot fabpot mentioned this pull request Oct 24, 2022
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