Skip to content

Commit ac3a8ec

Browse files
author
Thorsten Suckow-Homberg
committed
refactor: add contracts for determining comparability and equality
1 parent 0037af2 commit ac3a8ec

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

src/Core/Contract/Comparable.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* conjoon
5+
* php-lib-conjoon
6+
* Copyright (C) 2022 Thorsten Suckow-Homberg https://github.com/conjoon/php-lib-conjoon
7+
*
8+
* Permission is hereby granted, free of charge, to any person
9+
* obtaining a copy of this software and associated documentation
10+
* files (the "Software"), to deal in the Software without restriction,
11+
* including without limitation the rights to use, copy, modify, merge,
12+
* publish, distribute, sublicense, and/or sell copies of the Software,
13+
* and to permit persons to whom the Software is furnished to do so,
14+
* subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included
17+
* in all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
23+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25+
* USE OR OTHER DEALINGS IN THE SOFTWARE.
26+
*/
27+
28+
declare(strict_types=1);
29+
30+
namespace Conjoon\Core\Contract;
31+
32+
use Conjoon\Core\Exception\InvalidTypeException;
33+
34+
/**
35+
* Provides a contract for the ordering of an instance of the implementing class.
36+
*/
37+
interface Comparable
38+
{
39+
/**
40+
* Compares this instance with the $target and returns -1, 0 or 1 if this instance is
41+
* less than, equal, or greater than the $target.
42+
*
43+
* @param object $target
44+
*
45+
* @return int
46+
*
47+
* @throws InvalidTypeException if the specified $target cannot be compared with an instance of this class.
48+
*/
49+
public function compareTo(object $target): int;
50+
}

src/Core/Contract/Equatable.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* conjoon
5+
* php-lib-conjoon
6+
* Copyright (C) 2022 Thorsten Suckow-Homberg https://github.com/conjoon/php-lib-conjoon
7+
*
8+
* Permission is hereby granted, free of charge, to any person
9+
* obtaining a copy of this software and associated documentation
10+
* files (the "Software"), to deal in the Software without restriction,
11+
* including without limitation the rights to use, copy, modify, merge,
12+
* publish, distribute, sublicense, and/or sell copies of the Software,
13+
* and to permit persons to whom the Software is furnished to do so,
14+
* subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included
17+
* in all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
23+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25+
* USE OR OTHER DEALINGS IN THE SOFTWARE.
26+
*/
27+
28+
declare(strict_types=1);
29+
30+
namespace Conjoon\Core\Contract;
31+
32+
use Conjoon\Core\Exception\InvalidTypeException;
33+
34+
/**
35+
* Provides a contract for comparing instances of implementing classes for equality.
36+
*/
37+
interface Equatable
38+
{
39+
/**
40+
* Compares this instance with the $target for equality and returns true if both instances should
41+
* be treated as equal, otherwise false.
42+
*
43+
* @param object $target
44+
*
45+
* @return bool
46+
*
47+
* @throws InvalidTypeException if the specified $target cannot be compared for equality with an instance
48+
* of this class.
49+
*/
50+
public function equals(object $target): bool;
51+
}

0 commit comments

Comments
 (0)