|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Phootwork package. |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
* |
|
7
|
|
|
* @license MIT License |
|
8
|
|
|
* @copyright Thomas Gossmann |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace phootwork\lang\parts; |
|
11
|
|
|
|
|
12
|
|
|
use phootwork\lang\Text; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Text methods to perform string and Text object comparison |
|
16
|
|
|
* |
|
17
|
|
|
* @author Thomas Gossmann |
|
18
|
|
|
* @author Cristiano Cinotti |
|
19
|
|
|
*/ |
|
20
|
|
|
trait ComparisonPart { |
|
21
|
|
|
abstract protected function getString(): string; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Compares this string to another |
|
25
|
|
|
* |
|
26
|
|
|
* @param mixed $compare |
|
27
|
|
|
* |
|
28
|
|
|
* @return int |
|
29
|
|
|
* |
|
30
|
|
|
* @see \phootwork\lang\Comparable::compareTo() |
|
31
|
|
|
*/ |
|
32
|
2 |
|
public function compareTo($compare): int { |
|
33
|
2 |
|
return $this->compare($compare); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Compares this string to another string, ignoring the case |
|
38
|
|
|
* |
|
39
|
|
|
* @param mixed $compare |
|
40
|
|
|
* |
|
41
|
|
|
* @return int Return Values:<br> |
|
42
|
|
|
* < 0 if the object is less than comparison<br> |
|
43
|
|
|
* > 0 if the object is greater than comparison<br> |
|
44
|
|
|
* 0 if they are equal. |
|
45
|
|
|
*/ |
|
46
|
2 |
|
public function compareCaseInsensitive($compare): int { |
|
47
|
2 |
|
return $this->compare($compare, 'strcasecmp'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Compares this string to another |
|
52
|
|
|
* |
|
53
|
|
|
* @param string|Text $compare string to compare to |
|
54
|
|
|
* @param callable $callback |
|
55
|
|
|
* |
|
56
|
|
|
* @return int |
|
57
|
|
|
*/ |
|
58
|
4 |
|
public function compare($compare, callable $callback = null): int { |
|
59
|
4 |
|
if ($callback === null) { |
|
60
|
2 |
|
$callback = 'strcmp'; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
4 |
|
return $callback($this->getString(), (string) $compare); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Checks whether the string and the given object are equal |
|
68
|
|
|
* |
|
69
|
|
|
* @param mixed $string |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
2 |
|
public function equals($string): bool { |
|
74
|
2 |
|
return $this->compareTo($string) === 0; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Checks whether the string and the given object are equal ignoring the case |
|
79
|
|
|
* |
|
80
|
|
|
* @param mixed $string |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool |
|
83
|
|
|
*/ |
|
84
|
2 |
|
public function equalsIgnoreCase($string): bool { |
|
85
|
2 |
|
return $this->compareCaseInsensitive($string) === 0; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|