-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[VarDumper] Add period caster #23668
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
Conversation
13d4018
to
3e25472
Compare
4c9a9ee
to
cce0530
Compare
If we only want start/end/interval to be nicely dumped, that would already work as is right? Im not sure about the fancy period formatting :) though i like your creativity 👍 |
agree with @ro0NL, not really convinced myself sorry... |
a22f12d
to
0f377e5
Compare
Ok, I made tests green and I updated format. Example with end date (1) and with recurrences (2):
I kept tooltip with the dates included in the period. I find this useful, and you? |
if (\PHP_VERSION_ID >= 70107) { // see https://bugs.php.net/bug.php?id=74639 | ||
$format = 'Y-m-d'.('000000' === $p->getStartDate()->format('His') && '000' === $p->getDateInterval()->format('%h%i%s') ? '' : ' H:i:s'); | ||
foreach (clone $p as $i => $d) { | ||
$dates[] = sprintf('%s) %s', $i + 1, $d->format($format)); |
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.
this list can become insanely long... not sure we should do it.
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.
Limit to x first items would be better? If yes, showing the first 12 items seem good to me.
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.
dunno.. i would leave it out and keep the virtual period property.
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.
Mmh, an overview of dates period can be very useful I think. Let's wait for other opinions ;)
self::formatInterval($p->getDateInterval()), | ||
$p->getStartDate()->format('Y-m-d H:i:s'), | ||
$p->include_start_date ? 'included' : 'excluded', | ||
($end = $p->getEndDate()) ? 'to '.$end->format('Y-m-d H:i:s') : 'for '.$p->recurrences.'x' |
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.
, recurring x time/s
?
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.
Updated
0f377e5
to
8d25558
Compare
Fail not related. |
8d25558
to
39f45a2
Compare
foreach (clone $p as $i => $d) { | ||
$dates[] = sprintf('%s) %s', $i + 1, $d->format('Y-m-d H:i:s')); | ||
if (11 === $i) { | ||
$dates[] = '...'; |
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.
what about x more
(see iterator_count)
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.
Yes, good idea. But iterator_count()
loops on all dates, what we want to avoid. We can find x
with recurrences - limit
in recurrences case and (end - start - limit) / interval
in date case. WDYT?
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.
I added x more
;) I compared iterator_count()
and my method with 1M of reccurences:
- 4160.1 ms with
iterator_count()
- 0.1 ms with my method
self::formatInterval($p->getDateInterval()), | ||
$p->getStartDate()->format('Y-m-d H:i:s'), | ||
$p->include_start_date ? 'included' : 'excluded', | ||
($end = $p->getEndDate()) ? 'to '.$end->format('Y-m-d H:i:s') : 'recurring '.$p->recurrences.' time/s' |
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.
maybe add 1 === $p->recurrences ? 'recurring once' : ...
case for time/s :)
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.
I don't see the interest of this. Always having a digital format is the simplest, no?
($end = $p->getEndDate()) ? 'to '.$end->format('Y-m-d H:i:s') : 'recurring '.$p->recurrences.' time/s' | ||
); | ||
|
||
$p = array(Caster::PREFIX_VIRTUAL.'period' => new ConstStub($period, implode(PHP_EOL, $dates))); |
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.
"\n" instead of PHP_EOL
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.
Updated (Why exactly do you ask this change?)
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.
Purely out of interest, I too would like to know why the change. Is it simply a Symfony convention or is there an another underlying reason for it?
84f73dd
to
915fd4a
Compare
Can you review this PR please? :) |
is the screenshot up to date? |
Screenshot updated in PR description. |
@@ -20,6 +20,8 @@ | ||
*/ | ||
class DateCaster | ||
{ | ||
const PERIOD_LIMIT = 12; |
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.
why 12? I would have set 3 myself :)
the const should be removed (as we cannot make it private)
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.
It's arbitrary but monthly periods can be displayed for a whole year with a limit of 12.
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.
3 still looks enough to me, nobody will look at 12 values...
@@ -80,6 +82,41 @@ public static function castTimeZone(\DateTimeZone $timeZone, array $a, Stub $stu | ||
return $filter & Caster::EXCLUDE_VERBOSE ? $z : $z + $a; | ||
} | ||
|
||
public static function castPeriod(\DatePeriod $p, array $a, Stub $stub, $isNested, $filter) | ||
{ | ||
if (defined('HHVM_VERSION_ID') || \PHP_VERSION_ID < 50605) { |
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.
what happens before 5.6.5?
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.
getStartDate()
, getDateInterval()
and getEndDate()
methods were added in 5.6.5.
$dates = array(); | ||
if (\PHP_VERSION_ID >= 70107) { // see https://bugs.php.net/bug.php?id=74639 | ||
foreach (clone $p as $i => $d) { | ||
if (self::PERIOD_LIMIT === $i) { |
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.
if (3 === $i) {
$now = new \DateTimeImmutable(); | ||
$dates[] = sprintf('%s more', ($end = $p->getEndDate()) | ||
? ceil(($end->format('U.u') - $d->format('U.u')) / ($now->add($p->getDateInterval())->format('U.u') - $now->format('U.u'))) | ||
: $p->recurrences - self::PERIOD_LIMIT |
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.
$p->recurrences - $i
if (\PHP_VERSION_ID >= 70107) { // see https://bugs.php.net/bug.php?id=74639 | ||
foreach (clone $p as $i => $d) { | ||
if (self::PERIOD_LIMIT === $i) { | ||
// avoids iterator_count() function which can be expensive |
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.
not sure about the comment: nobody was wondering about iterator_count until read :)
915fd4a
to
4c4c398
Compare
Changes done + screenshot updated. |
Thank you @maidmaid. |
This PR was merged into the 3.4 branch. Discussion ---------- [VarDumper] Add period caster | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #23591 (comment) | License | MIT | Doc PR | / Result:  Commits ------- 4c4c398 Add period caster
@maidmaid this is segfaulting on PHP 5.6, see https://travis-ci.org/symfony/symfony/jobs/269610022 |
Ok, I check that now. |
This PR was merged into the 3.4 branch. Discussion ---------- [VarDumper] Fix segfault in period caster | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #23668 (comment) | License | MIT | Doc PR | / [This segfault](https://bugs.php.net/bug.php?id=71635) was fixed in [5.6.20](http://www.php.net/ChangeLog-5.php#5.6.20) and in [7.0.5](http://www.php.net/ChangeLog-7.php#7.0.5). Issue in action here: https://3v4l.org/DhOdT#v565 Commits ------- d84e9c8 Fix segfault in period caster
This PR was merged into the 4.0-dev branch. Discussion ---------- [VarDumper] Prepare period caster for 4.0 | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #23668 | License | MIT | Doc PR | / Commits ------- fd7352e Prepare period caster for 4.0
Result: