-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce the comparison component (#428)
- Loading branch information
Showing
30 changed files
with
649 additions
and
2 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,33 @@ | ||
<!-- | ||
This markdown file was generated using `docs/documenter.php`. | ||
Any edits to it will likely be lost. | ||
--> | ||
|
||
[*index](./../README.md) | ||
|
||
--- | ||
|
||
### `Psl\Comparison` Component | ||
|
||
#### `Functions` | ||
|
||
- [compare](./../../src/Psl/Comparison/compare.php#L19) | ||
- [equal](./../../src/Psl/Comparison/equal.php#L13) | ||
- [greater](./../../src/Psl/Comparison/greater.php#L13) | ||
- [greater_or_equal](./../../src/Psl/Comparison/greater_or_equal.php#L13) | ||
- [less](./../../src/Psl/Comparison/less.php#L13) | ||
- [less_or_equal](./../../src/Psl/Comparison/less_or_equal.php#L13) | ||
- [not_equal](./../../src/Psl/Comparison/not_equal.php#L13) | ||
- [sort](./../../src/Psl/Comparison/sort.php#L17) | ||
|
||
#### `Interfaces` | ||
|
||
- [Comparable](./../../src/Psl/Comparison/Comparable.php#L12) | ||
- [Equable](./../../src/Psl/Comparison/Equable.php#L10) | ||
|
||
#### `Enums` | ||
|
||
- [Order](./../../src/Psl/Comparison/Order.php#L7) | ||
|
||
|
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Comparison; | ||
|
||
use Psl\Comparison\Exception\IncomparableException; | ||
|
||
/** | ||
* @template T | ||
*/ | ||
interface Comparable | ||
{ | ||
/** | ||
* @param T $other | ||
* | ||
* @optionallyThrows IncomparableException - In case you want to bail out on specific comparisons. | ||
*/ | ||
public function compare(mixed $other): Order; | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Comparison; | ||
|
||
/** | ||
* @template T | ||
*/ | ||
interface Equable | ||
{ | ||
/** | ||
* @param T $other | ||
*/ | ||
public function equals(mixed $other): bool; | ||
} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Comparison\Exception; | ||
|
||
use InvalidArgumentException as InvalidArgumentRootException; | ||
use Psl\Exception\ExceptionInterface; | ||
use Psl\Str; | ||
|
||
use function get_debug_type; | ||
|
||
class IncomparableException extends InvalidArgumentRootException implements ExceptionInterface | ||
{ | ||
public static function fromValues(mixed $a, mixed $b, string $additionalInfo = ''): self | ||
{ | ||
return new self( | ||
Str\format( | ||
'Unable to compare "%s" with "%s"%s', | ||
get_debug_type($a), | ||
get_debug_type($b), | ||
$additionalInfo ? ': ' . $additionalInfo : '.', | ||
) | ||
); | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Comparison; | ||
|
||
enum Order : int | ||
{ | ||
case Less = -1; | ||
case Equal = 0; | ||
case Greater = 1; | ||
} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Comparison; | ||
|
||
/** | ||
* @template T | ||
* | ||
* @param T $a | ||
* @param T $b | ||
* | ||
* This function can compare 2 values of a similar type. | ||
* When the type happens to be mixed or never, it will fall back to PHP's internal comparison rules: | ||
* | ||
* @link https://www.php.net/manual/en/language.operators.comparison.php | ||
* @link https://www.php.net/manual/en/types.comparisons.php | ||
*/ | ||
function compare(mixed $a, mixed $b): Order | ||
{ | ||
if ($a instanceof Comparable) { | ||
return $a->compare($b); | ||
} | ||
|
||
return Order::from($a <=> $b); | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Comparison; | ||
|
||
/** | ||
* @template T | ||
* | ||
* @param T $a | ||
* @param T $b | ||
*/ | ||
function equal(mixed $a, mixed $b): bool | ||
{ | ||
return compare($a, $b) === Order::Equal; | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Comparison; | ||
|
||
/** | ||
* @template T | ||
* | ||
* @param T $a | ||
* @param T $b | ||
*/ | ||
function greater(mixed $a, mixed $b): bool | ||
{ | ||
return compare($a, $b) === Order::Greater; | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Comparison; | ||
|
||
/** | ||
* @template T | ||
* | ||
* @param T $a | ||
* @param T $b | ||
*/ | ||
function greater_or_equal(mixed $a, mixed $b): bool | ||
{ | ||
$order = compare($a, $b); | ||
|
||
return $order === Order::Equal || $order === Order::Greater; | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Comparison; | ||
|
||
/** | ||
* @template T | ||
* | ||
* @param T $a | ||
* @param T $b | ||
*/ | ||
function less(mixed $a, mixed $b): bool | ||
{ | ||
return compare($a, $b) === Order::Less; | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Comparison; | ||
|
||
/** | ||
* @template T | ||
* | ||
* @param T $a | ||
* @param T $b | ||
*/ | ||
function less_or_equal(mixed $a, mixed $b): bool | ||
{ | ||
$order = compare($a, $b); | ||
|
||
return $order === Order::Equal || $order === Order::Less; | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Comparison; | ||
|
||
/** | ||
* @template T | ||
* | ||
* @param T $a | ||
* @param T $b | ||
*/ | ||
function not_equal(mixed $a, mixed $b): bool | ||
{ | ||
return compare($a, $b) !== Order::Equal; | ||
} |
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Comparison; | ||
|
||
/** | ||
* @template T | ||
* | ||
* @param T $a | ||
* @param T $b | ||
* | ||
* This method can be used as a sorter callback function for Comparable items. | ||
* | ||
* Vec\sort($list, Comparable\sort(...)) | ||
*/ | ||
function sort($a, $b): int | ||
{ | ||
return compare($a, $b)->value; | ||
} |
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
Oops, something went wrong.