-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArrayStructure.php
85 lines (75 loc) · 2.39 KB
/
ArrayStructure.php
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
<?php
namespace Ephrin\Structures;
/**
* Class ArrayStructure
* @package Ephrin\Glutton\MongoDB\Detector
*/
class ArrayStructure
{
/**
* @param array|\ArrayAccess|\Countable $value
* @return bool
* @throws \InvalidArgumentException
*/
public static function isSequentialFastest($value)
{
if (is_array($value) || ($value instanceof \Countable && $value instanceof \ArrayAccess)) {
for ($i = count($value) - 1; $i >= 0; $i--) {
if (!isset($value[$i]) && !array_key_exists($i, $value)) {
return false;
}
}
return true;
} else {
throw new \InvalidArgumentException(
sprintf('Data type "%s" is not supported by method %s', gettype($value), __METHOD__)
);
}
}
public static function isSequentialSimple(array $value)
{
return true !== boolval(array_diff_key($value, (new \SplFixedArray(count($value)))->toArray()));
}
/**
* @param array|\ArrayAccess|\Countable $value
* @return bool
* @throws \InvalidArgumentException
*/
public static function isSequentialExotic($value)
{
if (is_array($value) || ($value instanceof \Countable && $value instanceof \ArrayAccess)) {
$count = count($value);
$half = (int)floor($count / 2);
for ($m = 0; $m < $half; $m++) {
if (isset($value[$m]) && isset($value[$half + $m])) {
continue;
} elseif (!array_key_exists($m, $value) || !array_key_exists($half + $m, $value)) {
return false;
}
}
return !boolval($count % 2 && !isset($value[$count - 1]) && !array_key_exists($count - 1, $value));
} else {
throw new \InvalidArgumentException(
sprintf('Data type "%s" is not supported by method %s', gettype($value), __METHOD__)
);
}
}
/**
* Reverse to \Ephrin\Structures\ArrayStructure::isSequential
* @param $value
* @return bool
*/
public static function isHash($value)
{
return !self::isSequentialFastest($value);
}
/**
* Another one
* @param $value
* @return bool
*/
public static function isAssoc($value)
{
return array_keys($value) !== range(0, count($value) - 1);
}
}