class-learndash-arr.php
3.87 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* This class provides the easy way to operate arrays.
*
* @since 4.5.0
*
* @package LearnDash
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Learndash_Arr' ) ) {
/**
* Array manipulation class.
*
* @since 4.5.0
*/
class Learndash_Arr {
/**
* Returns an array with specified keys only.
*
* @since 4.5.0
*
* @param array<int|string,mixed> $array Array.
* @param array<int|string> $keys Keys.
*
* @return array<int|string,mixed>
*/
public static function only( array $array, array $keys ): array {
return array_intersect_key( $array, array_flip( $keys ) );
}
/**
* Returns an array with specified keys removed.
*
* @since 4.5.0
*
* @param array<int|string,mixed>|ArrayAccess|ArrayObject $array Array.
* @param array<int|string> $keys Keys.
*
* @return array<int|string,mixed>
*/
public static function except( $array, array $keys ): array {
return static::forget( $array, $keys );
}
/**
* Deletes a value from array by the passed key(s).
*
* @since 4.5.0
*
* @param array<int|string,mixed>|ArrayAccess|ArrayObject $array Array.
* @param array<int|string> $keys Keys.
*
* @return array<int|string,mixed>
*/
public static function forget( $array, array $keys ): array {
if ( count( $keys ) === 0 ) {
return (array) $array;
}
foreach ( $keys as $key ) {
// If the exact key exists in the top-level, remove it.
if ( static::exists( $array, $key ) ) {
unset( $array[ $key ] );
continue;
}
// Check if the key is using the dot-notation.
if ( false === mb_strpos( (string) $key, '.' ) ) {
continue;
}
// If we are dealing with dot-notation, recursively handle it.
$parts = explode( '.', (string) $key );
$key = array_shift( $parts );
if ( static::exists( $array, $key ) && static::accessible( $array[ $key ] ) ) {
$array[ $key ] = static::forget(
$array[ $key ],
array( implode( '.', $parts ) )
);
if ( count( $array[ $key ] ) === 0 ) {
unset( $array[ $key ] );
}
}
}
return (array) $array;
}
/**
* Returns a value from array by the passed key or the default value if not found.
*
* @since 4.5.0
*
* @param array<int|string,mixed>|ArrayAccess|ArrayObject $array Array.
* @param string|int $key Key.
* @param mixed $default Default value. Default null.
*
* @return mixed
*/
public static function get( $array, $key, $default = null ) {
if ( ! static::accessible( $array ) ) {
return $default;
}
if ( static::exists( $array, $key ) ) {
return $array[ $key ];
}
$key = (string) $key;
if ( strpos( $key, '.' ) === false ) {
return $array[ $key ] ?? $default;
}
foreach ( explode( '.', $key ) as $segment ) {
if ( static::accessible( $array ) && static::exists( $array, $segment ) ) {
$array = $array[ $segment ];
} else {
return $default;
}
}
return $array;
}
/**
* Returns true if the given value is array accessible, false otherwise.
*
* @since 4.5.0
*
* @param mixed $value Value.
*
* @return bool
*/
public static function accessible( $value ): bool {
return is_array( $value ) || $value instanceof ArrayAccess;
}
/**
* Returns true if the given key exists in the array, false otherwise.
*
* @since 4.5.0
*
* @param array<int|string,mixed>|ArrayAccess|ArrayObject $array Array.
* @param int|string $key Key.
*
* @return bool
*/
public static function exists( $array, $key ): bool {
if ( $array instanceof ArrayAccess ) {
return $array->offsetExists( $key );
}
return array_key_exists( $key, $array );
}
}
}