-
Notifications
You must be signed in to change notification settings - Fork 2
✨ DateTimeCollection #140
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
Open
Edhrendal
wants to merge
1
commit into
steevanb:master
Choose a base branch
from
Edhrendal:feat-datetime_collection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
✨ DateTimeCollection #140
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/ObjectCollection/DateTime/ContainsDateTimeNullableValueTrait.php
This file contains hidden or 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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Steevanb\PhpCollection\ObjectCollection\DateTime; | ||
|
||
trait ContainsDateTimeNullableValueTrait | ||
{ | ||
public function containsDateTimeValue(\DateTimeInterface $dateTime): bool | ||
{ | ||
foreach ($this->toArray() as $value) { | ||
if ( | ||
$value === null | ||
|| ($dateTime->getTimestamp() + $dateTime->getOffset()) | ||
!== ($value->getTimestamp() + $value->getOffset()) | ||
) { | ||
continue; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** @return array<?\DateTimeInterface> */ | ||
abstract protected function toArray(): array; | ||
} |
27 changes: 27 additions & 0 deletions
27
src/ObjectCollection/DateTime/ContainsDateTimeValueTrait.php
This file contains hidden or 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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Steevanb\PhpCollection\ObjectCollection\DateTime; | ||
|
||
trait ContainsDateTimeValueTrait | ||
{ | ||
public function containsDateTimeValue(\DateTimeInterface $dateTime): bool | ||
{ | ||
foreach ($this->toArray() as $value) { | ||
if ( | ||
($dateTime->getTimestamp() + $dateTime->getOffset()) | ||
!== ($value->getTimestamp() + $value->getOffset()) | ||
) { | ||
continue; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** @return array<\DateTimeInterface> */ | ||
abstract protected function toArray(): array; | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
83 changes: 83 additions & 0 deletions
83
tests/Unit/ObjectCollection/DateTime/ContainsDateTimeValue/TraitUsageTest.php
This file contains hidden or 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,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Steevanb\PhpCollection\Tests\Unit\ObjectCollection\DateTime\ContainsDateTimeValue; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Steevanb\PhpCollection\ObjectCollection\DateTime\ContainsDateTimeNullableValueTrait; | ||
use Steevanb\PhpCollection\ObjectCollection\DateTime\ContainsDateTimeValueTrait; | ||
use Steevanb\PhpCollection\ObjectCollection\DateTimeCollection; | ||
use Steevanb\PhpCollection\ObjectCollection\DateTimeImmutableCollection; | ||
use Steevanb\PhpCollection\ObjectCollection\DateTimeImmutableNullableCollection; | ||
use Steevanb\PhpCollection\ObjectCollection\DateTimeInterfaceCollection; | ||
use Steevanb\PhpCollection\ObjectCollection\DateTimeInterfaceNullableCollection; | ||
use Steevanb\PhpCollection\ObjectCollection\DateTimeNullableCollection; | ||
|
||
/** | ||
* @covers \Steevanb\PhpCollection\ObjectCollection\DateTime\ContainsDateTimeValueTrait | ||
* @covers \Steevanb\PhpCollection\ObjectCollection\DateTime\ContainsDateTimeNullableValueTrait | ||
* @internal | ||
*/ | ||
final class TraitUsageTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideCollectionUsingTrait | ||
* @param class-string $class | ||
*/ | ||
public function testUsingTrait(string $class): void | ||
{ | ||
$reflectionCollection = new \ReflectionClass($class); | ||
static::assertContains( | ||
needle: ContainsDateTimeValueTrait::class, | ||
haystack: array_map( | ||
static fn (\ReflectionClass $reflectionTrait): string => $reflectionTrait->getName(), | ||
$reflectionCollection->getTraits() | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider provideCollectionUsingNullableTrait | ||
* @param class-string $class | ||
*/ | ||
public function testUsingNullableTrait(string $class): void | ||
{ | ||
$reflectionCollection = new \ReflectionClass($class); | ||
static::assertContains( | ||
needle: ContainsDateTimeNullableValueTrait::class, | ||
haystack: array_map( | ||
static fn (\ReflectionClass $reflectionTrait): string => $reflectionTrait->getName(), | ||
$reflectionCollection->getTraits() | ||
) | ||
); | ||
} | ||
|
||
/** @return iterable<string, array{class: class-string}> */ | ||
public static function provideCollectionUsingTrait(): iterable | ||
{ | ||
yield 'ContainsDateTimeValueTrait_is_used_by_DateTimeCollection' => [ | ||
'class' => DateTimeCollection::class, | ||
]; | ||
yield 'ContainsDateTimeValueTrait_is_used_by_DateTimeImmutableCollection' => [ | ||
'class' => DateTimeImmutableCollection::class, | ||
]; | ||
yield 'ContainsDateTimeValueTrait_is_used_by_DateTimeInterfaceCollection' => [ | ||
'class' => DateTimeInterfaceCollection::class, | ||
]; | ||
} | ||
|
||
/** @return iterable<string, array{class: class-string}> */ | ||
public static function provideCollectionUsingNullableTrait(): iterable | ||
{ | ||
yield 'ContainsDateTimeNullableValueTrait_is_used_by_DateTimeNullableCollection' => [ | ||
'class' => DateTimeNullableCollection::class, | ||
]; | ||
yield 'ContainsDateTimeNullableValueTrait_is_used_by_DateTimeImmutableNullableCollection' => [ | ||
'class' => DateTimeImmutableNullableCollection::class, | ||
]; | ||
yield 'ContainsDateTimeNullableValueTrait_is_used_by_DateTimeInterfaceNullableCollection' => [ | ||
'class' => DateTimeInterfaceNullableCollection::class, | ||
]; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
tests/Unit/ObjectCollection/DateTimeCollection/ContainsDateTimeValueTest.php
This file contains hidden or 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,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Steevanb\PhpCollection\Tests\Unit\ObjectCollection\DateTimeCollection; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Steevanb\PhpCollection\ObjectCollection\DateTimeCollection; | ||
|
||
/** | ||
* @covers \Steevanb\PhpCollection\ObjectCollection\DateTimeCollection::containsDateTimeValue | ||
* @internal | ||
*/ | ||
final class ContainsDateTimeValueTest extends TestCase | ||
{ | ||
public function testContainsDateTimeValue(): void | ||
{ | ||
$dateTimeA = new \DateTime(); | ||
$dateTimeCopy = $dateTimeA; | ||
// Asserting different timestamps | ||
$dateTimeB = (new \DateTime())->add(new \DateInterval('PT1S')); | ||
$dateTimeClone = clone $dateTimeA; | ||
$dateTimeSameTime = (new \DateTime())->setTimestamp($dateTimeA->getTimestamp()); | ||
|
||
$dateTimeCollection = new DateTimeCollection([$dateTimeA]); | ||
|
||
static::assertTrue($dateTimeCollection->containsDateTimeValue($dateTimeA)); | ||
static::assertTrue($dateTimeCollection->containsDateTimeValue($dateTimeCopy)); | ||
static::assertFalse($dateTimeCollection->containsDateTimeValue($dateTimeB)); | ||
static::assertTrue($dateTimeCollection->containsDateTimeValue($dateTimeClone)); | ||
static::assertTrue($dateTimeCollection->containsDateTimeValue($dateTimeSameTime)); | ||
} | ||
|
||
public function testContainsDateTimeValueWithTimezone(): void | ||
{ | ||
$dateTime = new \DateTime('2024-07-14 12:00:00'); | ||
$dateTimeB = clone $dateTime; | ||
$dateTimeC = clone $dateTime; | ||
$dateTimeD = clone $dateTime; | ||
|
||
// Setting timezones | ||
$dateTime->setTimezone(new \DateTimeZone('+0800')); | ||
$dateTimeB->setTimezone(new \DateTimeZone('+0600')); | ||
$dateTimeC->setTimezone(new \DateTimeZone('-0600')); | ||
$dateTimeD->setTimezone($dateTime->getTimezone()); | ||
|
||
$dateTimeCollection = new DateTimeCollection([$dateTime]); | ||
|
||
static::assertFalse($dateTimeCollection->containsDateTimeValue($dateTimeB)); | ||
static::assertFalse($dateTimeCollection->containsDateTimeValue($dateTimeC)); | ||
static::assertTrue($dateTimeCollection->containsDateTimeValue($dateTimeD)); | ||
} | ||
|
||
public function testContainsDateTimeValueAfterClone(): void | ||
{ | ||
$dateTime = new \DateTime(); | ||
|
||
$dateTimeCollection = (new DateTimeCollection([$dateTime])); | ||
$dateTimeCollectionClone = clone ($dateTimeCollection); | ||
|
||
static::assertTrue($dateTimeCollection->containsDateTimeValue($dateTime)); | ||
static::assertTrue($dateTimeCollectionClone->containsDateTimeValue($dateTime)); | ||
} | ||
|
||
public function testContainsDateTimeValueAfterMerge(): void | ||
{ | ||
$dateTime = new \DateTime(); | ||
|
||
static::assertTrue( | ||
(new DateTimeCollection([$dateTime])) | ||
->merge(new DateTimeCollection([new \DateTime()])) | ||
->containsDateTimeValue($dateTime) | ||
); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
tests/Unit/ObjectCollection/DateTimeCollection/ContainsTest.php
This file contains hidden or 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,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Steevanb\PhpCollection\Tests\Unit\ObjectCollection\DateTimeCollection; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Steevanb\PhpCollection\ObjectCollection\DateTimeCollection; | ||
|
||
/** | ||
* @covers \Steevanb\PhpCollection\ObjectCollection\DateTimeCollection::contains | ||
* @internal | ||
*/ | ||
final class ContainsTest extends TestCase | ||
{ | ||
public function testContains(): void | ||
{ | ||
$dateTimeA = new \DateTime(); | ||
$dateTimeCopy = $dateTimeA; | ||
$dateTimeB = new \DateTime(); | ||
$dateTimeClone = clone $dateTimeA; | ||
$dateTimeSameTime = (new \DateTime())->setTimestamp($dateTimeA->getTimestamp()); | ||
|
||
$dateTimeCollection = new DateTimeCollection([$dateTimeA]); | ||
|
||
static::assertTrue($dateTimeCollection->contains($dateTimeA)); | ||
static::assertTrue($dateTimeCollection->contains($dateTimeCopy)); | ||
static::assertFalse($dateTimeCollection->contains($dateTimeB)); | ||
static::assertFalse($dateTimeCollection->contains($dateTimeClone)); | ||
static::assertFalse($dateTimeCollection->contains($dateTimeSameTime)); | ||
} | ||
|
||
public function testContainsAfterClone(): void | ||
{ | ||
$dateTime = new \DateTime(); | ||
|
||
$dateTimeCollection = (new DateTimeCollection([$dateTime])); | ||
$dateTimeCollectionClone = clone ($dateTimeCollection); | ||
|
||
static::assertTrue($dateTimeCollection->contains($dateTime)); | ||
static::assertTrue($dateTimeCollectionClone->contains($dateTime)); | ||
} | ||
|
||
public function testContainsAfterMerge(): void | ||
{ | ||
$dateTime = new \DateTime(); | ||
|
||
static::assertTrue( | ||
(new DateTimeCollection([$dateTime])) | ||
->merge(new DateTimeCollection([new \DateTime()])) | ||
->contains($dateTime) | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
je me demande à quel point on peut ne faire qu'un seul trait, qui gère tous les cas.
ici on pourrait faire un $value instanceof \DateTime, et ne plus se soucier du nullable ?
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.
C'est pour qu'une collection "stricte" n'ait pas besoin de faire une vérification complémentaire.