1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Seboettg\Collection\Lists; |
4
|
|
|
|
5
|
|
|
use Seboettg\Collection\Comparable\Comparable; |
6
|
|
|
use Stringable; |
7
|
|
|
|
8
|
|
|
final class Functions |
9
|
|
|
{ |
10
|
|
|
|
11
|
3 |
|
public static final function strval($value): string |
|
|
|
|
12
|
|
|
{ |
13
|
3 |
|
if (is_double($value)) { |
14
|
2 |
|
$str = \strval($value); |
15
|
2 |
|
if (strlen($str) == 1) { |
16
|
1 |
|
return sprintf("%1\$.1f",$value); |
17
|
|
|
} |
18
|
2 |
|
return \strval($value); |
19
|
|
|
} |
20
|
2 |
|
if (is_bool($value)) { |
21
|
1 |
|
return $value ? "true" : "false"; |
22
|
|
|
} |
23
|
2 |
|
return "$value"; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public static final function emptyList(): ListInterface |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
return new class() implements ListInterface { |
29
|
|
|
private $array = []; |
|
|
|
|
30
|
|
|
use ArrayListTrait; |
31
|
|
|
}; |
32
|
|
|
} |
33
|
|
|
|
34
|
36 |
|
public static final function listOf(...$elements): ListInterface |
|
|
|
|
35
|
|
|
{ |
36
|
36 |
|
return listFromArray($elements); |
37
|
|
|
} |
38
|
|
|
|
39
|
36 |
|
public static final function listFromArray($elements): ListInterface |
|
|
|
|
40
|
|
|
{ |
41
|
36 |
|
$list = emptyList(); |
42
|
36 |
|
$list->setArray(array_values($elements)); |
43
|
36 |
|
return $list; |
44
|
|
|
} |
45
|
|
|
|
46
|
5 |
|
public static function isScalarOrStringable($object) |
47
|
|
|
{ |
48
|
5 |
|
return is_scalar($object) |
49
|
5 |
|
|| method_exists($object, "__toString"); |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
public static function isComparable($object): bool |
53
|
|
|
{ |
54
|
2 |
|
return $object instanceof Comparable; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
function strval($value): string |
59
|
|
|
{ |
60
|
3 |
|
return Functions::strval($value); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
function array_unique(array $array): array |
64
|
|
|
{ |
65
|
|
|
return Functions::array_unique($array); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
function emptyList(): ListInterface |
69
|
|
|
{ |
70
|
42 |
|
return Functions::emptyList(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
function listOf(...$elements): ListInterface |
74
|
|
|
{ |
75
|
36 |
|
return Functions::listOf(...$elements); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
function listFromArray(array $elements): ListInterface |
79
|
|
|
{ |
80
|
36 |
|
return Functions::listFromArray($elements); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
function isScalarOrStringable($object): bool |
84
|
|
|
{ |
85
|
5 |
|
return Functions::isScalarOrStringable($object); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
function isComparable($object): bool |
89
|
|
|
{ |
90
|
|
|
return Functions::isComparable($object); |
91
|
|
|
} |