Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce
Range
component (#378)
Signed-off-by: azjezz <azjezz@protonmail.com>
- Loading branch information
Showing
23 changed files
with
1,212 additions
and
73 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,33 @@ | ||
<!-- | ||
This markdown file was generated using `docs/documenter.php`. | ||
Any edits to it will likely be lost. | ||
--> | ||
|
||
[*index](./../README.md) | ||
|
||
--- | ||
|
||
### `Psl\Range` Component | ||
|
||
#### `Functions` | ||
|
||
- [between](./../../src/Psl/Range/between.php#L17) | ||
- [from](./../../src/Psl/Range/from.php#L16) | ||
- [full](./../../src/Psl/Range/full.php#L14) | ||
- [to](./../../src/Psl/Range/to.php#L16) | ||
|
||
#### `Interfaces` | ||
|
||
- [LowerBoundRangeInterface](./../../src/Psl/Range/LowerBoundRangeInterface.php#L18) | ||
- [RangeInterface](./../../src/Psl/Range/RangeInterface.php#L14) | ||
- [UpperBoundRangeInterface](./../../src/Psl/Range/UpperBoundRangeInterface.php#L14) | ||
|
||
#### `Classes` | ||
|
||
- [BetweenRange](./../../src/Psl/Range/BetweenRange.php#L45) | ||
- [FromRange](./../../src/Psl/Range/FromRange.php#L36) | ||
- [FullRange](./../../src/Psl/Range/FullRange.php#L20) | ||
- [ToRange](./../../src/Psl/Range/ToRange.php#L22) | ||
|
||
|
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
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
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,143 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Range; | ||
|
||
use Generator; | ||
use Psl\Iter; | ||
|
||
/** | ||
* A `BetweenRange` is a range that contains all values between the given lower and upper bound. | ||
* | ||
* This range can serve as an Iterator, starting from the lower bound, and ending at the upper bound. | ||
* | ||
* Example: | ||
* | ||
* ```php | ||
* use Psl\Range; | ||
* | ||
* $range = new Range\BetweenRange(1, 10, inclusive: false); | ||
* | ||
* foreach ($range as $value) { | ||
* // $value will be 1, 2, 3, 4, 5, 6, 7, 8, 9 | ||
* } | ||
* | ||
* $range = new Range\BetweenRange(1, 10, inclusive: true); | ||
* | ||
* foreach ($range as $value) { | ||
* // $value will be 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 | ||
* } | ||
* ``` | ||
* | ||
* @template T of int|float | ||
* | ||
* @implements LowerBoundRangeInterface<T> | ||
* @implements UpperBoundRangeInterface<T> | ||
* | ||
* @see RangeInterface::contains() | ||
* @see LowerBoundRangeInterface::getLowerBound() | ||
* @see UpperBoundRangeInterface::getUpperBound() | ||
* @see UpperBoundRangeInterface::isUpperInclusive() | ||
* | ||
* @immutable | ||
*/ | ||
final class BetweenRange implements LowerBoundRangeInterface, UpperBoundRangeInterface | ||
{ | ||
/** | ||
* @param T $lower_bound | ||
* @param T $upper_bound | ||
* | ||
* @psalm-mutation-free | ||
*/ | ||
public function __construct( | ||
private readonly int|float $lower_bound, | ||
private readonly int|float $upper_bound, | ||
private readonly bool $upper_inclusive = false, | ||
) { | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @param T $value | ||
* | ||
* @psalm-mutation-free | ||
*/ | ||
public function contains(int|float $value): bool | ||
{ | ||
if ($value < $this->lower_bound) { | ||
return false; | ||
} | ||
|
||
if ($this->upper_inclusive) { | ||
return $value <= $this->upper_bound; | ||
} | ||
|
||
return $value < $this->upper_bound; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @return T | ||
* | ||
* @psalm-mutation-free | ||
*/ | ||
public function getLowerBound(): int|float | ||
{ | ||
return $this->lower_bound; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @return T | ||
* | ||
* @psalm-mutation-free | ||
*/ | ||
public function getUpperBound(): int|float | ||
{ | ||
return $this->upper_bound; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @psalm-mutation-free | ||
*/ | ||
public function isUpperInclusive(): bool | ||
{ | ||
return $this->upper_inclusive; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @return Iter\Iterator<int, T> | ||
* | ||
* @psalm-mutation-free | ||
* | ||
* @psalm-suppress ImpureMethodCall | ||
* @psalm-suppress InvalidReturnStatement | ||
* @psalm-suppress InvalidReturnType | ||
* @psalm-suppress InvalidOperand | ||
* @psalm-suppress MixedOperand | ||
* @psalm-suppress MixedAssignment | ||
*/ | ||
public function getIterator(): Iter\Iterator | ||
{ | ||
$lower = $this->lower_bound; | ||
$upper = $this->upper_bound; | ||
$inclusive = $this->upper_inclusive; | ||
|
||
return Iter\Iterator::from(static function () use ($lower, $upper, $inclusive): Generator { | ||
$to = $inclusive ? $upper : $upper - 1; | ||
|
||
for ($i = $lower; $i <= $to; $i++) { | ||
/** @var T $i */ | ||
yield $i; | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.