1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
<?php
namespace ipl\Stdlib;
/**
* Collection of utilities for traversables
*/
class Seq
{
/**
* Check if the traversable contains the given needle
*
* @param array|iterable $traversable
* @param mixed $needle Might also be a closure
* @param bool $caseSensitive Whether strings should be compared case-sensitive
*
* @return bool
*/
public static function contains($traversable, $needle, $caseSensitive = true)
{
return self::find($traversable, $needle, $caseSensitive)[0] !== null;
}
/**
* Search in the traversable for the given needle and return its key and value
*
* @param array|iterable $traversable
* @param mixed $needle Might also be a closure
* @param bool $caseSensitive Whether strings should be compared case-sensitive
*
* @return array An array with two entries, the first is the key, then the value. Both are null if nothing is found.
*/
public static function find($traversable, $needle, $caseSensitive = true)
{
$usesCallback = is_callable($needle);
if (! $usesCallback && $caseSensitive && is_array($traversable)) {
return [array_search($needle, $traversable, true), $needle];
}
if (! $caseSensitive && is_string($needle) && ! $usesCallback) {
$needle = strtolower($needle);
}
foreach ($traversable as $key => $item) {
$originalItem = $item;
if (! $caseSensitive && is_string($item)) {
$item = strtolower($item);
}
if ($usesCallback && $needle($item)) {
return [$key, $originalItem];
} elseif ($item === $needle) {
return [$key, $originalItem];
}
}
return [null, null];
}
/**
* Search in the traversable for the given needle and return its key
*
* @param array|iterable $traversable
* @param mixed $needle Might also be a closure
* @param bool $caseSensitive Whether strings should be compared case-sensitive
*
* @return mixed|null Null if nothing is found
*/
public static function findKey($traversable, $needle, $caseSensitive = true)
{
return self::find($traversable, $needle, $caseSensitive)[0];
}
/**
* Search in the traversable for the given needle and return its value
*
* @param array|iterable $traversable
* @param mixed $needle Might also be a closure
* @param bool $caseSensitive Whether strings should be compared case-sensitive
*
* @return mixed|null Null if nothing is found
*/
public static function findValue($traversable, $needle, $caseSensitive = true)
{
$usesCallback = is_callable($needle);
if (! $usesCallback && $caseSensitive && is_array($traversable)) {
return isset($traversable[$needle]) ? $traversable[$needle] : null;
}
if (! $caseSensitive && is_string($needle) && ! $usesCallback) {
$needle = strtolower($needle);
}
foreach ($traversable as $key => $item) {
if (! $caseSensitive && is_string($key)) {
$key = strtolower($key);
}
if ($usesCallback && $needle($key)) {
return $item;
} elseif ($key === $needle) {
return $item;
}
}
return null;
}
}
|