Browse Source

added DateTimeHelper::periodToIso()

Fabian Peter Hammerle 7 years ago
parent
commit
d73aa1b51b
2 changed files with 117 additions and 0 deletions
  1. 54 0
      DateTimeHelper.php
  2. 63 0
      tests/DateTimeHelperTest.php

+ 54 - 0
DateTimeHelper.php

@@ -107,4 +107,58 @@ class DateTimeHelper
             }
         }
     }
+
+    /**
+     * @param \DatePeriod|null $p
+     * @return string|null
+     */
+    public static function periodToIso(\DatePeriod $p = null)
+    {
+        if(is_null($p)) {
+            return null;
+        } else {
+            // Cave:
+            // (new \DatePeriod(
+            //      new \DateTime('2016-08-05T14:50:14Z'),
+            //      new \DateInterval('P1D'),
+            //      -1
+            //      )->recurrences == 1
+            if($p->recurrences <= 0) {
+                throw new \Exception(
+                    'conversion of periods with number of occurances'
+                      . ' being negative is not supported'
+                    );
+            }
+            $repetitions = -1;
+            foreach($p as $dt) {
+                $repetitions++;
+                // printf("%d. %s\n", $repetitions, $dt->format(\DateTime::ATOM));
+            }
+            switch($repetitions) {
+                case -1:
+                    // no valid date within period
+                    // e.g. new \DatePeriod(
+                    //     new \DateTime('2016-08-05T14:50:14+08:00'),
+                    //     new \DateInterval('PT1S'),
+                    //     new \DateTime('2016-08-05T14:50:14+08:00')
+                    //     )
+                    throw new \InvalidArgumentException(
+                        'given period does not contain any valid date'
+                        );
+                case 0:
+                    return sprintf(
+                        '%s/%s',
+                        $p->getStartDate()->format(\DateTime::ATOM),
+                        self::intervalToIso($p->getDateInterval())
+                        );
+                default:
+                    return sprintf(
+                        'R%d/%s/%s',
+                        $repetitions,
+                        $p->getStartDate()->format(\DateTime::ATOM),
+                        self::intervalToIso($p->getDateInterval())
+                        );
+            }
+        }
+    }
 }

+ 63 - 0
tests/DateTimeHelperTest.php

@@ -308,4 +308,67 @@ class DateTimeHelperTest extends \PHPUnit_Framework_TestCase
     {
         DateTimeHelper::intervalToIso($interval);
     }
+
+    public function periodToIsoProvider()
+    {
+        return [
+            [null, null],
+            [
+                new \DatePeriod(
+                    new \DateTime('2016-08-05T14:50:14+08:00'),
+                    new \DateInterval('P1D'),
+                    new \DateTime('2016-08-10T14:50:14+08:00')
+                    ),
+                'R4/2016-08-05T14:50:14+08:00/P0Y0M1DT0H0M0S',
+                ],
+            [
+                new \DatePeriod(
+                    new \DateTime('2016-08-05T14:50:14+08:00'),
+                    new \DateInterval('P5D'),
+                    new \DateTime('2016-08-10T14:50:14+08:00')
+                    ),
+                '2016-08-05T14:50:14+08:00/P0Y0M5DT0H0M0S',
+                ],
+            [
+                new \DatePeriod(
+                    new \DateTime('2016-08-05T14:50:14+08:00'),
+                    new \DateInterval('P1Y2M3DT4H5M6S'),
+                    new \DateTime('2017-10-08T18:55:20+08:00')
+                    ),
+                '2016-08-05T14:50:14+08:00/P1Y2M3DT4H5M6S',
+                ],
+            [
+                new \DatePeriod(
+                    new \DateTime('2016-08-05T14:50:14Z'),
+                    new \DateInterval('P1D'),
+                    0
+                    ),
+                '2016-08-05T14:50:14+00:00/P0Y0M1DT0H0M0S',
+                ],
+            [
+                new \DatePeriod(
+                    new \DateTime('2016-08-05T14:50:14Z'),
+                    new \DateInterval('PT5M'),
+                    3
+                    ),
+                'R3/2016-08-05T14:50:14+00:00/P0Y0M0DT0H5M0S',
+                ],
+            [
+                new \DatePeriod('R3/2016-08-05T14:50:14Z/PT5M'),
+                'R3/2016-08-05T14:50:14+00:00/P0Y0M0DT0H5M0S',
+                ],
+            [
+                new \DatePeriod('R4/2016-08-05T14:50:14Z/P1Y2M3DT4H5M6S'),
+                'R4/2016-08-05T14:50:14+00:00/P1Y2M3DT4H5M6S',
+                ],
+            ];
+    }
+
+    /**
+     * @dataProvider periodToIsoProvider
+     */
+    public function testPeriodToIso($period, $iso)
+    {
+        $this->assertSame($iso, DateTimeHelper::periodToIso($period));
+    }
 }