@@ -29,6 +29,11 @@ public function dataProviderISO()
29
29
['P%yY%mM%dDT%hH%iM ' , 'P10Y2M3DT16H5M ' , 'P10Y2M3DT16H5M ' ],
30
30
['P%yY%mM%dDT%hH ' , 'P10Y2M3DT16H ' , 'P10Y2M3DT16H ' ],
31
31
['P%yY%mM%dD ' , 'P10Y2M3D ' , 'P10Y2M3DT0H ' ],
32
+ ['%RP%yY%mM%dD ' , '-P10Y2M3D ' , '-P10Y2M3DT0H ' ],
33
+ ['%RP%yY%mM%dD ' , '+P10Y2M3D ' , '+P10Y2M3DT0H ' ],
34
+ ['%RP%yY%mM%dD ' , '+P10Y2M3D ' , 'P10Y2M3DT0H ' ],
35
+ ['%rP%yY%mM%dD ' , '-P10Y2M3D ' , '-P10Y2M3DT0H ' ],
36
+ ['%rP%yY%mM%dD ' , 'P10Y2M3D ' , 'P10Y2M3DT0H ' ],
32
37
];
33
38
34
39
return $ data ;
@@ -50,15 +55,15 @@ public function testNormalize()
50
55
*/
51
56
public function testNormalizeUsingFormatPassedInContext ($ format , $ output , $ input )
52
57
{
53
- $ this ->assertEquals ($ output , $ this ->normalizer ->normalize (new \ DateInterval ($ input ), null , [DateIntervalNormalizer::FORMAT_KEY => $ format ]));
58
+ $ this ->assertEquals ($ output , $ this ->normalizer ->normalize ($ this -> getInterval ($ input ), null , [DateIntervalNormalizer::FORMAT_KEY => $ format ]));
54
59
}
55
60
56
61
/**
57
62
* @dataProvider dataProviderISO
58
63
*/
59
64
public function testNormalizeUsingFormatPassedInConstructor ($ format , $ output , $ input )
60
65
{
61
- $ this ->assertEquals ($ output , (new DateIntervalNormalizer ($ format ))->normalize (new \ DateInterval ($ input )));
66
+ $ this ->assertEquals ($ output , (new DateIntervalNormalizer ($ format ))->normalize ($ this -> getInterval ($ input )));
62
67
}
63
68
64
69
public function testNormalizeInvalidObjectThrowsException ()
@@ -84,15 +89,15 @@ public function testDenormalize()
84
89
*/
85
90
public function testDenormalizeUsingFormatPassedInContext ($ format , $ input , $ output )
86
91
{
87
- $ this ->assertDateIntervalEquals (new \ DateInterval ( $ output ), $ this ->normalizer ->denormalize ($ input , \DateInterval::class, null , [DateIntervalNormalizer::FORMAT_KEY => $ format ]));
92
+ $ this ->assertDateIntervalEquals ($ this -> getInterval ( $ input ), $ this ->normalizer ->denormalize ($ input , \DateInterval::class, null , [DateIntervalNormalizer::FORMAT_KEY => $ format ]));
88
93
}
89
94
90
95
/**
91
96
* @dataProvider dataProviderISO
92
97
*/
93
98
public function testDenormalizeUsingFormatPassedInConstructor ($ format , $ input , $ output )
94
99
{
95
- $ this ->assertDateIntervalEquals (new \ DateInterval ( $ output ), (new DateIntervalNormalizer ($ format ))->denormalize ($ input , \DateInterval::class));
100
+ $ this ->assertDateIntervalEquals ($ this -> getInterval ( $ input ), (new DateIntervalNormalizer ($ format ))->denormalize ($ input , \DateInterval::class));
96
101
}
97
102
98
103
public function testDenormalizeExpectsString ()
@@ -124,4 +129,20 @@ private function assertDateIntervalEquals(\DateInterval $expected, \DateInterval
124
129
{
125
130
$ this ->assertEquals ($ expected ->format ('%RP%yY%mM%dDT%hH%iM%sS ' ), $ actual ->format ('%RP%yY%mM%dDT%hH%iM%sS ' ));
126
131
}
132
+
133
+ private function getInterval ($ data )
134
+ {
135
+ if ('- ' === $ data [0 ]) {
136
+ $ interval = new \DateInterval (substr ($ data , 1 ));
137
+ $ interval ->invert = 1 ;
138
+
139
+ return $ interval ;
140
+ }
141
+
142
+ if ('+ ' === $ data [0 ]) {
143
+ return new \DateInterval (substr ($ data , 1 ));
144
+ }
145
+
146
+ return new \DateInterval ($ data );
147
+ }
127
148
}
0 commit comments