CarbonPeriodFactory.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of the Carbon package.
  5. *
  6. * (c) Brian Nesbitt <brian@nesbot.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Tests\CarbonPeriod\Fixtures;
  12. use Carbon\CarbonPeriod;
  13. class CarbonPeriodFactory
  14. {
  15. /**
  16. * @template T of CarbonPeriod
  17. *
  18. * @param class-string<T> $periodClass
  19. *
  20. * @return T
  21. */
  22. public static function withStartIntervalEnd(string $periodClass)
  23. {
  24. return $periodClass::create(
  25. '2012-07-01 17:30:00',
  26. 'P3DT5H',
  27. '2012-07-15 11:15:00'
  28. );
  29. }
  30. /**
  31. * @template T of CarbonPeriod
  32. *
  33. * @param class-string<T> $periodClass
  34. *
  35. * @return T
  36. */
  37. public static function withEvenDaysFilter(string $periodClass)
  38. {
  39. $period = $periodClass::create(
  40. '2012-07-01',
  41. 'P3D',
  42. '2012-07-22',
  43. $periodClass::EXCLUDE_END_DATE
  44. );
  45. return $period->addFilter(function ($date) {
  46. return $date->day % 2 == 0;
  47. });
  48. }
  49. /**
  50. * @template T of CarbonPeriod
  51. *
  52. * @param class-string<T> $periodClass
  53. *
  54. * @return T
  55. */
  56. public static function withCounter(string $periodClass, &$counter)
  57. {
  58. $counter = 0;
  59. $period = $periodClass::create(
  60. '2012-10-01',
  61. 3
  62. );
  63. return $period->addFilter(function () use (&$counter) {
  64. $counter++;
  65. return true;
  66. });
  67. }
  68. /**
  69. * @template T of CarbonPeriod
  70. *
  71. * @param class-string<T> $periodClass
  72. *
  73. * @return T
  74. */
  75. public static function withStackFilter(string $periodClass)
  76. {
  77. $period = $periodClass::create(
  78. '2001-01-01'
  79. );
  80. $stack = [
  81. true, false, true, $periodClass::END_ITERATION,
  82. false, false, true, true, $periodClass::END_ITERATION,
  83. ];
  84. return $period->addFilter(function () use (&$stack) {
  85. return array_shift($stack);
  86. });
  87. }
  88. }