Skip to content
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

Added generics to IntervalData classes #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 36 additions & 20 deletions src/Time/IntervalData/DateIntervalData.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

/**
* Interval of dates with data bound to it.
* @template TData
*/
class DateIntervalData implements Equalable, Comparable, IntersectComparable, Pokeable
{
Expand All @@ -50,7 +51,7 @@ class DateIntervalData implements Equalable, Comparable, IntersectComparable, Po
private $data;

/**
* @param mixed|null $data
* @param TData $data
*/
final public function __construct(Date $start, Date $end, $data)
{
Expand All @@ -64,14 +65,17 @@ final public function __construct(Date $start, Date $end, $data)
}

/**
* @param mixed|null $data
* @return self
* @param TData $data
* @return self<TData>
*/
public static function createFromDateInterval(DateInterval $interval, $data): self
{
return new static($interval->getStart(), $interval->getEnd(), $data);
}

/**
* @return self<TData>
*/
public static function empty(): self
{
$interval = new static(new Date(), new Date(), null);
Expand All @@ -82,8 +86,8 @@ public static function empty(): self
}

/**
* @param mixed|null $data
* @return self
* @param TData $data
* @return self<TData>
*/
public static function all($data): self
{
Expand Down Expand Up @@ -124,11 +128,17 @@ public function shift(string $value): self
return new static($this->start->modify($value), $this->end->modify($value), $this->data);
}

/**
* @return static
*/
public function setStart(Date $start): self
{
return new static($start, $this->end, $this->data);
}

/**
* @return static
*/
public function setEnd(Date $end): self
{
return new static($this->start, $end, $this->data);
Expand Down Expand Up @@ -162,7 +172,7 @@ public function toDateInterval(): DateInterval
}

/**
* @return Date[]|mixed[] array of pairs (Date $date, mixed $data)
* @return list<array{0: Date, 1: TData}>
*/
public function toDateDataArray(): array
{
Expand Down Expand Up @@ -191,15 +201,15 @@ public function getEnd(): Date
}

/**
* @return Date[]
* @return array{0: Date, 1: Date}
*/
public function getStartEnd(): array
{
return [$this->start, $this->end];
}

/**
* @return mixed|null
* @return TData
*/
public function getData()
{
Expand All @@ -212,7 +222,7 @@ public function isEmpty(): bool
}

/**
* @param self $other
* @param self<TData> $other
* @return bool
*/
public function equals(Equalable $other): bool
Expand All @@ -223,7 +233,7 @@ public function equals(Equalable $other): bool
}

/**
* @param mixed|null $otherData
* @param TData $otherData
* @return bool
*/
public function dataEquals($otherData): bool
Expand All @@ -236,7 +246,7 @@ public function dataEquals($otherData): bool
}

/**
* @param self $other
* @param self<TData> $other
* @return int
*/
public function compare(Comparable $other): int
Expand All @@ -248,7 +258,7 @@ public function compare(Comparable $other): int
}

/**
* @param self $other
* @param self<TData> $other
* @return int
*/
public function compareIntersects(IntersectComparable $other): int
Expand Down Expand Up @@ -277,7 +287,7 @@ public function containsValue($date): bool
}

/**
* @param DateInterval|DateIntervalData $interval
* @param DateInterval|DateIntervalData<mixed> $interval
* @return bool
*/
public function contains($interval): bool
Expand All @@ -290,7 +300,7 @@ public function contains($interval): bool
}

/**
* @param DateInterval|DateIntervalData $interval
* @param DateInterval|DateIntervalData<mixed> $interval
* @return bool
*/
public function intersects($interval): bool
Expand All @@ -299,7 +309,7 @@ public function intersects($interval): bool
}

/**
* @param DateInterval|DateIntervalData $interval
* @param DateInterval|DateIntervalData<mixed> $interval
* @return bool
*/
public function touches($interval): bool
Expand All @@ -309,10 +319,13 @@ public function touches($interval): bool

// actions ---------------------------------------------------------------------------------------------------------

/**
* @return self<TData>
*/
public function intersect(DateInterval ...$items): self
{
$items[] = $this->toDateInterval();
/** @var self[] $items */
/** @var array<static> $items */
$items = Arr::sortComparable($items);

/** @var DateInterval $result */
Expand All @@ -328,6 +341,9 @@ public function intersect(DateInterval ...$items): self
return new static($result->getStart(), $result->getEnd(), $this->data);
}

/**
* @return DateIntervalDataSet<TData>
*/
public function subtract(DateInterval ...$items): DateIntervalDataSet
{
$intervals = [$this];
Expand Down Expand Up @@ -355,8 +371,8 @@ public function subtract(DateInterval ...$items): DateIntervalDataSet
// static ----------------------------------------------------------------------------------------------------------

/**
* @param self[] $intervals
* @return self[]
* @param list<static> $intervals
* @return list<static>
* @deprecated will be removed. use Arr::sortComparable() instead.
*/
public static function sort(array $intervals): array
Expand All @@ -365,8 +381,8 @@ public static function sort(array $intervals): array
}

/**
* @param self[] $intervals
* @return self[]
* @param list<static> $intervals
* @return list<static>
* @deprecated will be removed. use Arr::sortComparable() instead.
*/
public static function sortByStart(array $intervals): array
Expand Down
Loading