-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #197 from michalananapps/copy-DatePeriod
bug: #196 Introduce a custom DatePeriodFilter that safely copies DatePeriod objects without modifying their readonly properties.
- Loading branch information
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace DeepCopy\TypeFilter\Date; | ||
|
||
use DatePeriod; | ||
use DeepCopy\TypeFilter\TypeFilter; | ||
|
||
/** | ||
* @final | ||
*/ | ||
class DatePeriodFilter implements TypeFilter | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
* | ||
* @param DatePeriod $element | ||
* | ||
* @see http://news.php.net/php.bugs/205076 | ||
*/ | ||
public function apply($element) | ||
{ | ||
$options = 0; | ||
if (PHP_VERSION_ID >= 80200 && $element->include_end_date) { | ||
$options |= DatePeriod::INCLUDE_END_DATE; | ||
} | ||
if (!$element->include_start_date) { | ||
$options |= DatePeriod::EXCLUDE_START_DATE; | ||
} | ||
|
||
if ($element->getEndDate()) { | ||
return new DatePeriod($element->getStartDate(), $element->getDateInterval(), $element->getEndDate(), $options); | ||
} | ||
|
||
if (PHP_VERSION_ID >= 70217) { | ||
$recurrences = $element->getRecurrences(); | ||
} else { | ||
$recurrences = $element->recurrences - $element->include_start_date; | ||
} | ||
|
||
return new DatePeriod($element->getStartDate(), $element->getDateInterval(), $recurrences, $options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
tests/DeepCopyTest/TypeFilter/Date/DatePeriodFilterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace DeepCopyTest\TypeFilter\Date; | ||
|
||
use DateInterval; | ||
use DatePeriod; | ||
use DateTime; | ||
use DeepCopy\TypeFilter\Date\DateIntervalFilter; | ||
use DeepCopy\TypeFilter\Date\DatePeriodFilter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \DeepCopy\TypeFilter\Date\DatePeriodFilter | ||
*/ | ||
class DatePeriodFilterTest extends TestCase | ||
{ | ||
public function test_it_deep_copies_a_DatePeriod() | ||
{ | ||
$object = new DatePeriod(new DateTime(), new DateInterval('P2D'), 3); | ||
|
||
$filter = new DatePeriodFilter(); | ||
|
||
$copy = $filter->apply($object); | ||
|
||
$this->assertEquals($object, $copy); | ||
$this->assertNotSame($object, $copy); | ||
} | ||
|
||
public function test_it_deep_copies_a_DatePeriod_with_exclude_start_date() | ||
{ | ||
$object = new DatePeriod(new DateTime(), new DateInterval('P2D'), 3, DatePeriod::EXCLUDE_START_DATE); | ||
|
||
$filter = new DatePeriodFilter(); | ||
|
||
$copy = $filter->apply($object); | ||
|
||
$this->assertEquals($object, $copy); | ||
$this->assertNotSame($object, $copy); | ||
} | ||
|
||
/** | ||
* @requires PHP 8.2 | ||
*/ | ||
public function test_it_deep_copies_a_DatePeriod_with_include_end_date() | ||
{ | ||
$object = new DatePeriod(new DateTime(), new DateInterval('P2D'), 3, DatePeriod::INCLUDE_END_DATE); | ||
|
||
$filter = new DatePeriodFilter(); | ||
|
||
$copy = $filter->apply($object); | ||
|
||
$this->assertEquals($object, $copy); | ||
$this->assertNotSame($object, $copy); | ||
} | ||
|
||
public function test_it_deep_copies_a_DatePeriod_with_end_date() | ||
{ | ||
$object = new DatePeriod(new DateTime(), new DateInterval('P2D'), new DateTime('+2 days')); | ||
|
||
$filter = new DatePeriodFilter(); | ||
|
||
$copy = $filter->apply($object); | ||
|
||
$this->assertEquals($object, $copy); | ||
$this->assertNotSame($object, $copy); | ||
} | ||
} |